void CShTPSCharacter::Initialize(const CShIdentifier & levelIdentifier, const CShIdentifier & characterIdentifier, CShTPSGun * defaultGun) { m_characterIdentifier = characterIdentifier; //set the position of the Character to the position of the sprite or the model if 3d if(m_3d) { m_Position = ShObject::GetPosition2(m_pModel); ShObject::SetPosition(m_pSprite, m_Position.m_x, m_Position.m_y, 1.0f); /*float rotation = ShObject::GetRotation(m_pModel).GetZ(); m_Direction = CShVector2(std::acos(rotation),std::asin(rotation));*/ } else { m_Position = ShObject::GetPosition2(m_pSprite); /*float rotation = ShObject::GetRotation(m_pSprite).GetZ(); m_Direction = CShVector2(std::asin(rotation),std::acos(rotation));*/ } m_Direction = CShVector2(0.0f,1.0f); m_pGun = defaultGun; m_pGun->Initialize(levelIdentifier, this); m_originalDirection = CShVector2(m_Direction.m_x,m_Direction.m_y); m_originalPosition = CShVector2(m_Position.m_x,m_Position.m_y); }
void CShTPSEnemy::Update(float dt) { if(m_pGun->GetCoolDown() < m_pGun->GetFireRate()) { m_pGun->AddToCoolDown(dt); } if(HasReachedTarget()) // If the enemy isn't on its target's location, he will go to it, else he will stay { m_CurrentState = e_state_idle; } else { m_CurrentState = e_state_attack; float length = CShVector2(m_Target - m_Position).GetLength(); m_Direction = CShVector2((m_Target.m_x - m_Position.m_x)/length , (m_Target.m_y - m_Position.m_y)/length); ShCharacterController::SetWalkDirection(m_pCharacterController, m_Direction); ShCharacterController::SetWalkSpeed(m_pCharacterController, m_Speed); } switch(m_CurrentState) { case e_state_idle: ShCharacterController::SetWalkSpeed(m_pCharacterController, 0.0f); if(m_3d) { ShEntity3::AnimationPlay(m_pModel, m_pAnimIdle,true); } break; case e_state_attack: m_Position = ShCharacterController::GetPosition(m_pCharacterController); ShObject::SetPositionX(m_pSprite,m_Position.m_x); ShObject::SetPositionY(m_pSprite,m_Position.m_y); if(m_3d) { ShEntity3::AnimationPlay(m_pModel, m_pAnimRun,true); //TODO changer animation en attaque si ça marche un jour ShEntity3::SetRotation(m_pModel, CShEulerAngles(0.0f, 0.0f, std::atan2(m_Direction.m_y,m_Direction.m_x ) + 1.5f )); // 1.5f : valeur piffée pour que les enemis regardent bien le joueur ShObject::SetPositionX(m_pModel,m_Position.m_x); ShObject::SetPositionY(m_pModel,m_Position.m_y); } ShEntity2::SetRotation(m_pSprite, CShEulerAngles(0.0f, 0.0f, std::atan2(m_Direction.m_y,m_Direction.m_x ) - 1.5f )); // 1.5f : valeur piffée pour que les enemis regardent bien le joueur //ShEntity2::SetRotation(m_pSprite, CShEulerAngles(0.0f, 0.0f, shAcosf(m_Direction.m_x/m_Direction.m_y))); break; } ShCharacterController::Update(m_pCharacterController); }
/** * Called after engine initialization */ void OnPostInitialize(void) { CShIdentifier levelIdentifier("memory"); bool loaded = ShLevel::Load(levelIdentifier); SH_ASSERT(loaded); // // Create Camera g_pCamera = ShCamera::Create(GID(global), GID(camera_free), false); SH_ASSERT(NULL != g_pCamera); ShCamera::SetPosition(g_pCamera, CShVector3(0, 0.0f, 1000.0f)); ShCamera::SetTarget(g_pCamera, CShVector3(0.0f, 0.0f, 0.0f)); ShCamera::SetFarPlaneDistance(g_pCamera, 3000.0f); ShCamera::SetViewport(g_pCamera, 256*WIDTH, 256*HEIGHT); ShCamera::SetProjectionOrtho(g_pCamera); ShCamera::SetCurrent2D(g_pCamera); ratio = CShVector2((256*WIDTH)/(float)ShDisplay::GetWidth(), (256*HEIGHT)/(float)ShDisplay::GetHeight()); g_pWinEntity = ShEntity2::Find(levelIdentifier, CShIdentifier("sprite_memory_win_001")); SH_ASSERT(shNULL != g_pWinEntity); ShEntity2::SetShow(g_pWinEntity, false); // // Create all sprites for (int i = 0; i < HEIGHT; ++i) { for (int j = 0; j < WIDTH; ++j) { CShVector3 pos; pos.m_x = (256.0f * j) - (128.0f + (((WIDTH/2)-1) * 256.0f)); pos.m_y = (256.0f * i) - (128.0f + (((HEIGHT/2)-1) * 256.0f)); int c = i*WIDTH+j; aCards[c].pEntityRecto = ShEntity2::Create(levelIdentifier, CShIdentifier(), GID(layer_default), CShIdentifier("memory"), aIdentifier[c/2], pos, CShEulerAngles(0.0f, 0.0f, 0.0f), CShVector3(1.0f, 1.0f, 1.0f)); SH_ASSERT(shNULL != aCards[c].pEntityRecto); aCards[c].pEntityVerso = ShEntity2::Create(levelIdentifier, CShIdentifier(), GID(layer_default), CShIdentifier("memory"), CShIdentifier("verso"), pos, CShEulerAngles(0.0f, 0.0f, 0.0f), CShVector3(1.0f, 1.0f, 1.0f)); SH_ASSERT(shNULL != aCards[c].pEntityVerso); aCards[c].type = c/2; } } for (int i = 0; i < PIECES; ++i) { ShEntity2::SetShow(aCards[i].pEntityRecto, false); } shuffle(); }
/*explicit*/ Animation::Animation (const CShIdentifier& levelId, const CShString& fileName, const bool& loopable, int nbImages, int fps, const CShVector3& position) : m_aImages() , m_nbImages(nbImages) , m_currentImage(1) , m_levelIdentifier(levelId) , m_fileName(fileName) , m_fAnimSpeed(1.f/fps) , m_bLoop(loopable) , m_Pos(position) , m_Rot() , m_Sca(CShVector3(1.0f, 1.0f, 1.0f)) , m_Piv(CShVector2(0.5f, 0.5f)) , m_elapsedTime(0.0f) , m_currentState(STOP) { // // Load the images ShEntity2* currentEntity = NULL; int nbLoop = 1; CShIdentifier entityIdentifier; CShIdentifier levelEntityIdentifier; while(nbLoop <= nbImages) { CShString id = fileName + "_" + ((nbLoop<100)?CShString("0") + ((nbLoop<10)?CShString("0"):CShString("")):CShString("")) + CShString::FromInt(nbLoop); entityIdentifier = CShIdentifier(id); levelEntityIdentifier = CShIdentifier(CShString::FromInt(s_NumberAnim) + id); currentEntity = ShEntity2::Create(m_levelIdentifier , levelEntityIdentifier , CShIdentifier("layer_default") , CShIdentifier("ggj") , entityIdentifier , m_Pos , CShEulerAngles() , CShVector3(1.0f, 1.0f, 1.0f) ); ShEntity2::SetShow(currentEntity, false); nbLoop++; m_aImages.Add(currentEntity); } s_NumberAnim++; }
/*virtual*/ void GameStateIntro::OnTouchDown(int iTouch, float positionX, float positionY) { if(ShEntity2::Includes(nextBtn, CShVector2(positionX, positionY))) { ShEntity2::SetShow(ecrans[currentScreen], false); currentScreen++; if(currentScreen >= ecrans.GetCount()) { ShLevel::Release(); GameStateManager::PushState(new GameStateLevel()); GameStateManager::SwapState(); GameStateManager::PopState(); return; } else { SH_TRACE("PASSAGE"); ShEntity2::SetShow(ecrans[currentScreen], true); } } }
// Two animation : idle and run ShAnimation * pAnimationWarriorStop = shNULL; ShAnimation * pAnimationWarriorRun = shNULL; // The three inputs to move, turn left and turn right ShInput * g_pInputUp = shNULL; ShInput * g_pInputLeft = shNULL; ShInput * g_pInputRight = shNULL; // The character controller ShCharacterController * g_pCharacterController = shNULL; // The speed and direction float g_speed = 0.0f; CShVector2 g_direction = CShVector2(0.0f, -10.0f); /** * Called before engine initialization */ void OnPreInitialize(void) { // nothing here } /** * Called after engine initialization */ void OnPostInitialize(void) { CShIdentifier levelIdentifier("character_controller"); // this is the level name