void Shape::Update(float dt) { if (_jumpTimeout > 0.0f) { _jumpTimeout -= dt; } b2Vec2 currentVelocity = GetBody()->GetLinearVelocity(); // ################# if (! _onGround) { // SetSpriteFrame(12); SetSpriteFrame(39); _spriteFrameDelay = 0.0f; } else if (MathUtil::FuzzyEquals(currentVelocity.x, 0.0f)) { SetSpriteFrame(10); _spriteFrameDelay = 0.0f; } else { if (!IsSpriteAnimPlaying()) { // PlaySpriteAnimation(0.1f, SAT_Loop, 1, 11); PlaySpriteAnimation(0.02f, SAT_Loop, 10, 30); } } if ((currentVelocity.x < 0.0f) && _facingRight) { FlipLeft(); } else if ((currentVelocity.x > 0.0f) && !_facingRight) { FlipRight(); } // ################# if (_poweringUp) { return; } float maxVel = theTuning.GetFloat("BlockyMaxSpeed"); float xVector = 0.0f; if (theController.IsConnected()) { bool dpad_left_down; bool dpad_right_down; dpad_left_down = theController.IsButtonDown(XINPUT_GAMEPAD_DPAD_LEFT); dpad_right_down = theController.IsButtonDown(XINPUT_GAMEPAD_DPAD_RIGHT); if (dpad_left_down) { xVector = -1.0f; } else if (dpad_right_down) { xVector = 1.0f; } else if (dpad_left_down && dpad_right_down) { xVector = 0.0f; } else { xVector = theController.GetLeftThumbVec2().X; } } if (theInput.IsKeyDown(ANGEL_KEY_RIGHTARROW)) { xVector = 1.0f; } if (theInput.IsKeyDown(ANGEL_KEY_LEFTARROW)) { xVector = -1.0f; } if (theInput.IsKeyDown(ANGEL_KEY_RIGHTARROW) && theInput.IsKeyDown(ANGEL_KEY_LEFTARROW)) { xVector = 0.0f; } float desiredVelocity = xVector * maxVel; float velocityChange = desiredVelocity - currentVelocity.x; float impulse = GetBody()->GetMass() * velocityChange; ApplyLinearImpulse(Vector2(impulse, 0), Vector2()); PhysicsActor::Update(dt); }
/** * Override from Actor::Render * Handles rendering */ void CharActor::Render() { // Variable initializations float delay = 0.1f; spriteAnimationType animType = SAT_None; int startFrame = 0; int endFrame = 0; String animName = String(); // Set animation for each movement direction if(m_moving && !m_collFlags.wall) { m_idleAnim = false; #pragma region Walk North if((m_nextPosition.Y > _position.Y) & (m_nextPosition.X == _position.X)) { if(_currentAnimName.compare("WalkUp") != 0) { ClearSpriteInfo(); LoadSpriteFrames("Resources/Images/animations/chars/arth/walk/up/walkUp_00.png"); delay = 0.1f; animType = SAT_Loop; startFrame = 0; endFrame = 5; animName = "WalkUp"; } } #pragma endregion Walk North #pragma region Walk North-East if((m_nextPosition.X > _position.X) & (m_nextPosition.Y > _position.Y)) { if(_currentAnimName.compare("WalkUpRight") != 0) { ClearSpriteInfo(); LoadSpriteFrames("Resources/Images/animations/chars/arth/walk/upRight/walkUpRight_00.png"); delay = 0.1f; animType = SAT_Loop; startFrame = 0; endFrame = 5; animName = "WalkUpRight"; } } #pragma endregion Walk North-East #pragma region Walk East if((m_nextPosition.X > _position.X) & (m_nextPosition.Y == _position.Y)) { if(_currentAnimName.compare("WalkRight") != 0) { ClearSpriteInfo(); LoadSpriteFrames("Resources/Images/animations/chars/arth/walk/right/walkRight_00.png"); delay = 0.1f; animType = SAT_Loop; startFrame = 0; endFrame = 5; animName = "WalkRight"; } } #pragma endregion Walk East #pragma region Walk South-East if((m_nextPosition.X > _position.X) & (m_nextPosition.Y < _position.Y)) { if(_currentAnimName.compare("WalkDownRight") != 0) { ClearSpriteInfo(); LoadSpriteFrames("Resources/Images/animations/chars/arth/walk/downRight/walkDownRight_00.png"); delay = 0.1f; animType = SAT_Loop; startFrame = 0; endFrame = 5; animName = "WalkDownRight"; } } #pragma endregion Walk South-East #pragma region Walk South if((m_nextPosition.Y < _position.Y) & (m_nextPosition.X == _position.X)) { if(_currentAnimName.compare("WalkDown") != 0) { ClearSpriteInfo(); LoadSpriteFrames("Resources/Images/animations/chars/arth/walk/down/walkDown_00.png"); delay = 0.1f; animType = SAT_Loop; startFrame = 0; endFrame = 5; animName = "WalkDown"; } } #pragma endregion Walk South #pragma region Walk South-West if((m_nextPosition.X < _position.X) & (m_nextPosition.Y < _position.Y)) { if(_currentAnimName.compare("WalkDownLeft") != 0) { ClearSpriteInfo(); LoadSpriteFrames("Resources/Images/animations/chars/arth/walk/downLeft/walkDownLeft_00.png"); delay = 0.1f; animType = SAT_Loop; startFrame = 0; endFrame = 5; animName = "WalkDownLeft"; } } #pragma endregion Walk South-West #pragma region Walk West if((m_nextPosition.X < _position.X) & (m_nextPosition.Y == _position.Y)) { if(_currentAnimName.compare("WalkLeft") != 0) { ClearSpriteInfo(); LoadSpriteFrames("Resources/Images/animations/chars/arth/walk/left/walkLeft_00.png"); delay = 0.1f; animType = SAT_Loop; startFrame = 0; endFrame = 5; animName = "WalkLeft"; } } #pragma endregion Walk West #pragma region Walk North-West if((m_nextPosition.X < _position.X) & (m_nextPosition.Y > _position.Y)) { if(_currentAnimName.compare("WalkUpLeft") != 0) { ClearSpriteInfo(); LoadSpriteFrames("Resources/Images/animations/chars/arth/walk/upLeft/walkUpLeft_00.png"); delay = 0.1f; animType = SAT_Loop; startFrame = 0; endFrame = 5; animName = "WalkUpLeft"; } } #pragma endregion Walk North-West } // Set idle animation if(!m_moving || m_collFlags.wall) { m_idleness+=theWorld.GetDT(); if(m_idleness < IDLE_TIME) { if(!m_idleAnim) { switch(m_direction) { case NORTH: if(_currentAnimName.compare("LookAroundUp") != 0) { ClearSpriteInfo(); _currentAnimName = ""; SetSprite("Resources/Images/animations/chars/arth/stand/lookAroundUp/lookAroundUp_00.png"); } break; case NORTHEAST: if(_currentAnimName.compare("LookAroundUpRight") != 0) { ClearSpriteInfo(); _currentAnimName = ""; SetSprite("Resources/Images/animations/chars/arth/stand/lookAroundUpRight/lookAroundUpRight_00.png"); } break; case EAST: if(_currentAnimName.compare("LookAroundRight") != 0) { ClearSpriteInfo(); _currentAnimName = ""; SetSprite("Resources/Images/animations/chars/arth/stand/lookAroundRight/lookAroundRight_00.png"); } break; case SOUTHEAST: if(_currentAnimName.compare("LookAroundDownRight") != 0) { ClearSpriteInfo(); _currentAnimName = ""; SetSprite("Resources/Images/animations/chars/arth/stand/lookAroundDownRight/lookAroundDownRight_00.png"); } break; case SOUTH: if(_currentAnimName.compare("LookAroundDown") != 0) { ClearSpriteInfo(); _currentAnimName = ""; SetSprite("Resources/Images/animations/chars/arth/stand/lookAroundDown/lookAroundDown_00.png"); } break; case SOUTHWEST: if(_currentAnimName.compare("LookAroundDownLeft") != 0) { ClearSpriteInfo(); _currentAnimName = ""; SetSprite("Resources/Images/animations/chars/arth/stand/lookAroundDownLeft/lookAroundDownLeft_00.png"); } break; case WEST: if(_currentAnimName.compare("LookAroundLeft") != 0) { ClearSpriteInfo(); _currentAnimName = ""; SetSprite("Resources/Images/animations/chars/arth/stand/lookAroundLeft/lookAroundLeft_00.png"); } break; case NORTHWEST: if(_currentAnimName.compare("LookAroundUpLeft") != 0) { ClearSpriteInfo(); _currentAnimName = ""; SetSprite("Resources/Images/animations/chars/arth/stand/lookAroundUpLeft/lookAroundUpLeft_00.png"); } break; } } } else { m_idleness=0.0f; m_idleAnim = true; switch(m_direction) { case NORTH: if(_currentAnimName.compare("LookAroundUp") != 0) { ClearSpriteInfo(); LoadSpriteFrames("Resources/Images/animations/chars/arth/stand/lookAroundUp/lookAroundUp_00.png"); delay = 0.2f; animType = SAT_OneShot; startFrame = 0; endFrame = 9; animName = "LookAroundUp"; } break; case NORTHEAST: if(_currentAnimName.compare("LookAroundUpRight") != 0) { ClearSpriteInfo(); LoadSpriteFrames("Resources/Images/animations/chars/arth/stand/lookAroundUpRight/lookAroundUpRight_00.png"); delay = 0.2f; animType = SAT_OneShot; startFrame = 0; endFrame = 9; animName = "LookAroundUpRight"; } break; case EAST: if(_currentAnimName.compare("LookAroundRight") != 0) { ClearSpriteInfo(); LoadSpriteFrames("Resources/Images/animations/chars/arth/stand/lookAroundRight/lookAroundRight_00.png"); delay = 0.2f; animType = SAT_OneShot; startFrame = 0; endFrame = 9; animName = "LookAroundRight"; } break; case SOUTHEAST: if(_currentAnimName.compare("LookAroundDownRight") != 0) { ClearSpriteInfo(); LoadSpriteFrames("Resources/Images/animations/chars/arth/stand/lookAroundDownRight/lookAroundDownRight_00.png"); delay = 0.2f; animType = SAT_OneShot; startFrame = 0; endFrame = 9; animName = "LookAroundDownRight"; } break; case SOUTH: if(_currentAnimName.compare("LookAroundDown") != 0) { ClearSpriteInfo(); LoadSpriteFrames("Resources/Images/animations/chars/arth/stand/lookAroundDown/lookAroundDown_00.png"); delay = 0.2f; animType = SAT_OneShot; startFrame = 0; endFrame = 9; animName = "LookAroundDown"; } break; case SOUTHWEST: if(_currentAnimName.compare("LookAroundDownLeft") != 0) { ClearSpriteInfo(); LoadSpriteFrames("Resources/Images/animations/chars/arth/stand/lookAroundDownLeft/lookAroundDownLeft_00.png"); delay = 0.2f; animType = SAT_OneShot; startFrame = 0; endFrame = 9; animName = "LookAroundDownLeft"; } break; case WEST: if(_currentAnimName.compare("LookAroundLeft") != 0) { ClearSpriteInfo(); LoadSpriteFrames("Resources/Images/animations/chars/arth/stand/lookAroundLeft/lookAroundLeft_00.png"); delay = 0.2f; animType = SAT_OneShot; startFrame = 0; endFrame = 9; animName = "LookAroundLeft"; } break; case NORTHWEST: if(_currentAnimName.compare("LookAroundUpLeft") != 0) { ClearSpriteInfo(); LoadSpriteFrames("Resources/Images/animations/chars/arth/stand/lookAroundUpLeft/lookAroundUpLeft_00.png"); delay = 0.2f; animType = SAT_OneShot; startFrame = 0; endFrame = 9; animName = "LookAroundUpLeft"; } break; } } } if((_currentAnimName.compare(animName) != 0) & !IsSpriteAnimPlaying()) { PlaySpriteAnimation( delay, //amount of time between frames animType, //other options are SAT_PingPong and SAT_OneShot startFrame, //starting frame endFrame, //ending frame animName.c_str() //name of the animation so you can get the event when it finishes ); } CollidingActor::Render(); }
void CharActor::Update(float dt) { float delay = 0.1f; spriteAnimationType animType = SAT_None; int startFrame = 0; int endFrame = 0; String animName = String(); Vector2 direction = Vector2::Zero; if(m_movingNorth) { direction.Y += 1.0f; } if(m_movingEast) { direction.X += 1.0f; } if(m_movingSouth) { direction.Y -= 1.0f; } if(m_movingWest) { direction.X -= 1.0f; } direction != Vector2::Zero ? m_moving=true : m_moving=false; if(m_moving) { float angle_rad = MathUtil::AngleFromVector(direction); //bug fix along axes: direction.X = direction.X == 0 ? 0 : (m_movementSpeed*dt)*cos(angle_rad); direction.Y = direction.Y == 0 ? 0 : ((m_movementSpeed/2)*dt)*sin(angle_rad); m_idleness = 0.0f; } Vector2 newPosition = _position + direction; bool collType = false;//thePixelArthGame.m_wColl->isColliding(*m_mask, newPosition-(GetSize()/2)); //std::cout << "moving: " << m_moving << " collType: " << collType << std::endl; //bool collType = false; if(m_moving) { if((newPosition.Y > _position.Y) & (newPosition.X == _position.X)) { m_direction = NORTH; } else if((newPosition.X > _position.X) & (newPosition.Y > _position.Y)) { m_direction = NORTHEAST; } else if((newPosition.X > _position.X) & (newPosition.Y == _position.Y)) { m_direction = EAST; } else if((newPosition.X > _position.X) & (newPosition.Y < _position.Y)) { m_direction = SOUTHEAST; } else if((newPosition.Y < _position.Y) & (newPosition.X == _position.X)) { m_direction = SOUTH; } else if((newPosition.X < _position.X) & (newPosition.Y < _position.Y)) { m_direction = SOUTHWEST; } else if((newPosition.X < _position.X) & (newPosition.Y == _position.Y)) { m_direction = WEST; } else if((newPosition.X < _position.X) & (newPosition.Y > _position.Y)) { m_direction = NORTHWEST; } } if(m_moving && !collType) { m_idleAnim = false; #pragma region Walk North if((newPosition.Y > _position.Y) & (newPosition.X == _position.X)) { if(_currentAnimName.compare("WalkUp") != 0) { ClearSpriteInfo(); LoadSpriteFrames("Resources/Images/animations/chars/arth/walk/up/walkUp_00.png"); delay = 0.1f; animType = SAT_Loop; startFrame = 0; endFrame = 5; animName = "WalkUp"; } } #pragma endregion Walk North #pragma region Walk North-East if((newPosition.X > _position.X) & (newPosition.Y > _position.Y)) { if(_currentAnimName.compare("WalkUpRight") != 0) { ClearSpriteInfo(); LoadSpriteFrames("Resources/Images/animations/chars/arth/walk/upRight/walkUpRight_00.png"); delay = 0.1f; animType = SAT_Loop; startFrame = 0; endFrame = 5; animName = "WalkUpRight"; } } #pragma endregion Walk North-East #pragma region Walk East if((newPosition.X > _position.X) & (newPosition.Y == _position.Y)) { if(_currentAnimName.compare("WalkRight") != 0) { ClearSpriteInfo(); LoadSpriteFrames("Resources/Images/animations/chars/arth/walk/right/walkRight_00.png"); delay = 0.1f; animType = SAT_Loop; startFrame = 0; endFrame = 5; animName = "WalkRight"; } } #pragma endregion Walk East #pragma region Walk South-East if((newPosition.X > _position.X) & (newPosition.Y < _position.Y)) { if(_currentAnimName.compare("WalkDownRight") != 0) { ClearSpriteInfo(); LoadSpriteFrames("Resources/Images/animations/chars/arth/walk/downRight/walkDownRight_00.png"); delay = 0.1f; animType = SAT_Loop; startFrame = 0; endFrame = 5; animName = "WalkDownRight"; } } #pragma endregion Walk South-East #pragma region Walk South if((newPosition.Y < _position.Y) & (newPosition.X == _position.X)) { if(_currentAnimName.compare("WalkDown") != 0) { ClearSpriteInfo(); LoadSpriteFrames("Resources/Images/animations/chars/arth/walk/down/walkDown_00.png"); delay = 0.1f; animType = SAT_Loop; startFrame = 0; endFrame = 5; animName = "WalkDown"; } } #pragma endregion Walk South #pragma region Walk South-West if((newPosition.X < _position.X) & (newPosition.Y < _position.Y)) { if(_currentAnimName.compare("WalkDownLeft") != 0) { ClearSpriteInfo(); LoadSpriteFrames("Resources/Images/animations/chars/arth/walk/downLeft/walkDownLeft_00.png"); delay = 0.1f; animType = SAT_Loop; startFrame = 0; endFrame = 5; animName = "WalkDownLeft"; } } #pragma endregion Walk South-West #pragma region Walk West if((newPosition.X < _position.X) & (newPosition.Y == _position.Y)) { if(_currentAnimName.compare("WalkLeft") != 0) { ClearSpriteInfo(); LoadSpriteFrames("Resources/Images/animations/chars/arth/walk/left/walkLeft_00.png"); delay = 0.1f; animType = SAT_Loop; startFrame = 0; endFrame = 5; animName = "WalkLeft"; } } #pragma endregion Walk West #pragma region Walk North-West if((newPosition.X < _position.X) & (newPosition.Y > _position.Y)) { if(_currentAnimName.compare("WalkUpLeft") != 0) { ClearSpriteInfo(); LoadSpriteFrames("Resources/Images/animations/chars/arth/walk/upLeft/walkUpLeft_00.png"); delay = 0.1f; animType = SAT_Loop; startFrame = 0; endFrame = 5; animName = "WalkUpLeft"; } } #pragma endregion Walk North-West } /*else if(collType == 0) //make enum for collType { }*/ if(!m_moving || collType) { m_idleness+=dt; if(m_idleness < IDLE_TIME) { if(!m_idleAnim) { switch(m_direction) { case NORTH: if(_currentAnimName.compare("LookAroundUp") != 0) { ClearSpriteInfo(); _currentAnimName = ""; //CLAMPMODES: GL_CLAMP or GL_REPEAT //FILTERMODES: GL_NEAREST, GL_LINEAR, GL_NEAREST_MIPMAP_NEAREST, GL_LINEAR_MIPMAP_NEAREST, GL_NEAREST_MIPMAP_LINEAR, or //GL_LINEAR_MIPMAP_LINEAR SetSprite("Resources/Images/animations/chars/arth/stand/lookAroundUp/lookAroundUp_00.png"/*, 0, GL_CLAMP, GL_LINEAR_MIPMAP_NEAREST*/); } break; case NORTHEAST: if(_currentAnimName.compare("LookAroundUpRight") != 0) { ClearSpriteInfo(); _currentAnimName = ""; SetSprite("Resources/Images/animations/chars/arth/stand/lookAroundUpRight/lookAroundUpRight_00.png"); } break; case EAST: if(_currentAnimName.compare("LookAroundRight") != 0) { ClearSpriteInfo(); _currentAnimName = ""; SetSprite("Resources/Images/animations/chars/arth/stand/lookAroundRight/lookAroundRight_00.png"); } break; case SOUTHEAST: if(_currentAnimName.compare("LookAroundDownRight") != 0) { ClearSpriteInfo(); _currentAnimName = ""; SetSprite("Resources/Images/animations/chars/arth/stand/lookAroundDownRight/lookAroundDownRight_00.png"); } break; case SOUTH: if(_currentAnimName.compare("LookAroundDown") != 0) { ClearSpriteInfo(); _currentAnimName = ""; SetSprite("Resources/Images/animations/chars/arth/stand/lookAroundDown/lookAroundDown_00.png"); } break; case SOUTHWEST: if(_currentAnimName.compare("LookAroundDownLeft") != 0) { ClearSpriteInfo(); _currentAnimName = ""; SetSprite("Resources/Images/animations/chars/arth/stand/lookAroundDownLeft/lookAroundDownLeft_00.png"); } break; case WEST: if(_currentAnimName.compare("LookAroundLeft") != 0) { ClearSpriteInfo(); _currentAnimName = ""; SetSprite("Resources/Images/animations/chars/arth/stand/lookAroundLeft/lookAroundLeft_00.png"); } break; case NORTHWEST: if(_currentAnimName.compare("LookAroundUpLeft") != 0) { ClearSpriteInfo(); _currentAnimName = ""; SetSprite("Resources/Images/animations/chars/arth/stand/lookAroundUpLeft/lookAroundUpLeft_00.png"); } break; } } } else { m_idleness=0.0f; m_idleAnim = true; switch(m_direction) { case NORTH: if(_currentAnimName.compare("LookAroundUp") != 0) { ClearSpriteInfo(); LoadSpriteFrames("Resources/Images/animations/chars/arth/stand/lookAroundUp/lookAroundUp_00.png"); delay = 0.2f; animType = SAT_OneShot; startFrame = 0; endFrame = 9; animName = "LookAroundUp"; } break; case NORTHEAST: if(_currentAnimName.compare("LookAroundUpRight") != 0) { ClearSpriteInfo(); LoadSpriteFrames("Resources/Images/animations/chars/arth/stand/lookAroundUpRight/lookAroundUpRight_00.png"); delay = 0.2f; animType = SAT_OneShot; startFrame = 0; endFrame = 9; animName = "LookAroundUpRight"; } break; case EAST: if(_currentAnimName.compare("LookAroundRight") != 0) { ClearSpriteInfo(); LoadSpriteFrames("Resources/Images/animations/chars/arth/stand/lookAroundRight/lookAroundRight_00.png"); delay = 0.2f; animType = SAT_OneShot; startFrame = 0; endFrame = 9; animName = "LookAroundRight"; } break; case SOUTHEAST: if(_currentAnimName.compare("LookAroundDownRight") != 0) { ClearSpriteInfo(); LoadSpriteFrames("Resources/Images/animations/chars/arth/stand/lookAroundDownRight/lookAroundDownRight_00.png"); delay = 0.2f; animType = SAT_OneShot; startFrame = 0; endFrame = 9; animName = "LookAroundDownRight"; } break; case SOUTH: if(_currentAnimName.compare("LookAroundDown") != 0) { ClearSpriteInfo(); LoadSpriteFrames("Resources/Images/animations/chars/arth/stand/lookAroundDown/lookAroundDown_00.png"); delay = 0.2f; animType = SAT_OneShot; startFrame = 0; endFrame = 9; animName = "LookAroundDown"; } break; case SOUTHWEST: if(_currentAnimName.compare("LookAroundDownLeft") != 0) { ClearSpriteInfo(); LoadSpriteFrames("Resources/Images/animations/chars/arth/stand/lookAroundDownLeft/lookAroundDownLeft_00.png"); delay = 0.2f; animType = SAT_OneShot; startFrame = 0; endFrame = 9; animName = "LookAroundDownLeft"; } break; case WEST: if(_currentAnimName.compare("LookAroundLeft") != 0) { ClearSpriteInfo(); LoadSpriteFrames("Resources/Images/animations/chars/arth/stand/lookAroundLeft/lookAroundLeft_00.png"); delay = 0.2f; animType = SAT_OneShot; startFrame = 0; endFrame = 9; animName = "LookAroundLeft"; } break; case NORTHWEST: if(_currentAnimName.compare("LookAroundUpLeft") != 0) { ClearSpriteInfo(); LoadSpriteFrames("Resources/Images/animations/chars/arth/stand/lookAroundUpLeft/lookAroundUpLeft_00.png"); delay = 0.2f; animType = SAT_OneShot; startFrame = 0; endFrame = 9; animName = "LookAroundUpLeft"; } break; } } } if((_currentAnimName.compare(animName) != 0) & !IsSpriteAnimPlaying()) { PlaySpriteAnimation( delay, //amount of time between frames animType, //other options are SAT_PingPong and SAT_OneShot startFrame, //starting frame endFrame, //ending frame animName.c_str() //name of the animation so you can get the event when it finishes ); } if(m_moving & !collType) { //_position = newPosition; //TODO: check for memory leak //GetBody()->SetLinearVelocity(b2Vec2(direction.X, direction.Y)); //ApplyForce(dt, newPosition); //BUGGY //MoveTo(newPosition, dt); } GetBody()->SetLinearVelocity(b2Vec2(direction.X, direction.Y)); Actor::Update(dt); }