/////////////////////////////////////////////////////////////////////// // HUD arrow pointing to planets, powerups // // Jon Rust // // Charles Emory Myers - Bug fixes // // All Content © 2011 DigiPen (USA) Corporation, all rights reserved. //////////////////////////////////////////////////////////////////////// #include "Precompiled.h" #include "HUDArrow.h" #include "GameplaySystem.h" #include "ComponentManager.h" #include "Player.h" HUDArrow::HUDArrow() { sprite = (Sprite*)ComponentManager::instance("Sprite", NULL); goalPos = NULL; playerPos = NULL; owner = NULL; } HUDArrow::~HUDArrow() { if(owner) owner->RemoveComponent(this); if(GAME) GAME->hudItemList.erase(this); if( sprite ) ComponentManager::instance.Destroy( sprite->GetId() ); } void HUDArrow::Initialize() { //Add the hud item to the hud item list if (GAME) GAME->hudItemList.push_back(this); //Initialize the sprite sprite->Initialize(); sprite->position.x = GRAPHICS->width/2-sprite->frameSize.x/2; sprite->position.y = GRAPHICS->height/2-sprite->frameSize.y/2; center.x = GRAPHICS->width/2-sprite->frameSize.x/2; center.y = GRAPHICS->height/2-sprite->frameSize.y/2; radius = min(GRAPHICS->width/2, GRAPHICS->height/2) - max((int)sprite->frameSize.x, (int)sprite->frameSize.y); //Set the goal and player positions if (owner) goalPos = &(((Body*)owner->GetComponent(CT_Body))->position); if (player) playerPos = &(player->body->position); } void HUDArrow::Serialize(ISerializer& stream) { sprite->Serialize(stream); } void HUDArrow::Update(float dt) { if (owner && player) { goalPos = &(((Body*)owner->GetComponent(CT_Body))->position); playerPos = &(player->body->position); //Rotate the arrow to point at the goal Vector2 playerToGoal = *goalPos-*playerPos; float distance = sqrt(LenSquared(playerToGoal)); Normalize(playerToGoal); //Feels like the math here is really messed up... but, hey, it works! playerToGoal.x *= -1; sprite->position = center + ((float)-radius)*playerToGoal; sprite->rotation = atan2(playerToGoal.y, playerToGoal.x); //Make arrow disappear if the goal is close if (distance < 200) sprite->color.w = 0; else sprite->color.w = 1; } } void HUDArrow::ReceiveMessage(Message* m) { //ensures the arrow is always visible, no matter the resolution if( m->type == MSG_SetResolution ) { if (sprite) { Vector2 data = ((DataMessage*)m)->data; center.x = data.x/2-sprite->frameSize.x/2; center.y = data.y/2-sprite->frameSize.y/2; radius = min((int)data.x/2, (int)data.y/2) - max((int)sprite->frameSize.x, (int)sprite->frameSize.y); sprite->position.x = data.x/2-sprite->frameSize.x/2; sprite->position.y = data.y/2-sprite->frameSize.y/2; } } if( m->type == MSG_UpdateHUD ) { Update( 0.0f ); } }