#include "birdlike.hpp"

#include "main.hpp"
#include "playstate.hpp"
#include "stage.hpp" // for shadow
//#include "stadium.hpp" // for tiles

EnemyBirdlike::EnemyBirdlike(const KungFlowGame *game, const PlayState *playState, e_Team team, const Vector2 &position, float zPosition) : Enemy(game, playState, e_EnemyType_Birdlike, team, position, zPosition, Vector2(0), 0) {

  bodySprite = new Sprite();
  bodySprite->LoadFile("res/gfx/badguy.png", e_FilterMode_Nearest);
  bodyObject = new Object(&playState->GetWorldNode(), bodySprite);
  bodyObject->spatial.z = 0.0f;
  bodyObject->spatial.scale = Vector2(1.0f, 1.0f);
  bodyObject->spatial.orientation = playState->GetViewOrientation();
  bodyObject->spatial.orientation = -0.125f * pi; // artistic

  goingBack = false;
}

EnemyBirdlike::~EnemyBirdlike() {
  delete bodyObject;
  delete bodySprite;
}

/*
void EnemyBirdlike::GetSpriteBounds(Vector2 &center, Vector2 &radius) const {
  //center = position;
  Spatial tmpSpatial = bodyObject->GetDerivedSpatial();
  center = tmpSpatial.position;// * GetRadius();
  radius = tmpSpatial.scale * 0.5f;// * GetRadius();
}
*/

void EnemyBirdlike::OnCollide(const StageEntity* entity) {
  Expire();
}

void EnemyBirdlike::OnStepPhysicsTime() {

  if (!goingBack) {
    movement = Vector2(1, 0);
  } else {
    movement = Vector2(-1, 0);
  }

  if (position.coords[0] > 10) goingBack = true;
  if (position.coords[0] < -10) Expire();

  // one way version
  //if (position.coords[0] > 20) expired = true;

  position += movement * playState->GetPhysicsTimeStep_ms() * 0.001f * _PHYSICSSCALE;

}

void EnemyBirdlike::PostStepPhysicsTime() {
  bodyObject->spatial.position = position - zPosition;
}

void EnemyBirdlike::Draw(const Viewport &viewport, const Camera &camera) const {
  //playState->GetStage()->DrawShadow(position, zPosition, GetRadius(), 1.0f);//Draw(viewport, camera);
  playState->GetStage()->DrawShadow(GetPosition(), GetZPosition(), GetRadius() * 1.2f, 1.0f);
  bodyObject->Draw(viewport, camera);
}
