Skip to content

Commit

Permalink
Add enemy animations
Browse files Browse the repository at this point in the history
  • Loading branch information
jorgebaptista committed Feb 28, 2022
1 parent 9d82d9d commit d6dd94f
Show file tree
Hide file tree
Showing 9 changed files with 106 additions and 32 deletions.
114 changes: 93 additions & 21 deletions can-you-survive/Enemy.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@ Enemy::Enemy(Vector2f position)
goal_PositionX = m_Position.x;
goal_PositionY = m_Position.y;

m_Sprite.setPosition(m_Position);

// load all correct textures
m_TextureUp.loadFromFile("graphics/enemy/up.png");
m_TextureDown.loadFromFile("graphics/enemy/down.png");
Expand All @@ -23,6 +21,12 @@ Enemy::Enemy(Vector2f position)
// create rect to navigate through the spriresheet
rectSourceSprite = sf::IntRect(0, 0, 64, 64);

m_Sprite.setTexture(m_TextureRight);
m_Sprite = Sprite(m_TextureRight, rectSourceSprite);

m_Sprite.setOrigin(32, 32);
m_Sprite.setPosition(m_Position);

m_animateTimer = 0;
}

Expand All @@ -31,7 +35,7 @@ void Enemy::Movement(float elapsedTime, float totalTime, Vector2f mapBounds)
{
//Decide where to move randomly, unless player is within sight,
//in which case, attempt to move to player.

//Check if enough time has passed to allow movement
if (moveTime <= totalTime)
{
Expand All @@ -51,7 +55,7 @@ void Enemy::Movement(float elapsedTime, float totalTime, Vector2f mapBounds)
// check direction
switch (randnum)
{
// if up
// if up
case 1:
// if going up goes outside of map
if (m_Position.y - speed < 0)
Expand Down Expand Up @@ -124,21 +128,40 @@ void Enemy::Movement(float elapsedTime, float totalTime, Vector2f mapBounds)

}

// add the delta time to animate timer
m_animateTimer += elapsedTime;

//move based on randnum
if (randnum == 1)
{
//go up
if (m_Position.y > goal_PositionY)
{
m_Position.y -= speed * elapsedTime;
m_Sprite.setRotation(270);
m_Texture.loadFromFile("graphics/polar.png");
m_Sprite.setTexture(m_Texture);

// every 0.2 seconds
if (m_animateTimer > 0.2f)
{
// if rect is at the right most of the sprite sheet reset it
if (rectSourceSprite.left == 320) rectSourceSprite.left = 0;
// else just jump to the right one
else rectSourceSprite.left += 64;

// set the sprite to the correct texture and rect properties
m_Sprite = Sprite(m_TextureUp, rectSourceSprite);

// set the animate timer back to 0
m_animateTimer = 0;
}
}
//Used to ensure character doesn't move past boundry
//Used to ensure character doesn't move past boundary
else
{
m_Position.y = goal_PositionY;

// reset the sprite and rect back to first one not animated
rectSourceSprite = sf::IntRect(0, 0, 64, 64);
m_Sprite = Sprite(m_TextureUp, rectSourceSprite);
}
}
if (randnum == 2)
Expand All @@ -147,14 +170,30 @@ void Enemy::Movement(float elapsedTime, float totalTime, Vector2f mapBounds)
if (m_Position.y < goal_PositionY)
{
m_Position.y += speed * elapsedTime;
m_Sprite.setRotation(90);
m_Texture.loadFromFile("graphics/polar.png");
m_Sprite.setTexture(m_Texture);

// every 0.2 seconds
if (m_animateTimer > 0.2f)
{
// if rect is at the right most of the sprite sheet reset it
if (rectSourceSprite.left == 320) rectSourceSprite.left = 0;
// else just jump to the right one
else rectSourceSprite.left += 64;

// set the sprite to the correct texture and rect properties
m_Sprite = Sprite(m_TextureDown, rectSourceSprite);

// set the animate timer back to 0
m_animateTimer = 0;
}
}
//Used to ensure character doesn't move past boundry
//Used to ensure character doesn't move past boundary
else
{
m_Position.y = goal_PositionY;

// reset the sprite and rect back to first one not animated
rectSourceSprite = sf::IntRect(0, 0, 64, 64);
m_Sprite = Sprite(m_TextureDown, rectSourceSprite);
}
}
if (randnum == 3)
Expand All @@ -163,14 +202,30 @@ void Enemy::Movement(float elapsedTime, float totalTime, Vector2f mapBounds)
if (m_Position.x > goal_PositionX)
{
m_Position.x -= speed * elapsedTime;
m_Sprite.setRotation(0);
m_Texture.loadFromFile("graphics/polarflip.png");
m_Sprite.setTexture(m_Texture);

// every 0.2 seconds
if (m_animateTimer > 0.2f)
{
// if rect is at the right most of the sprite sheet reset it
if (rectSourceSprite.left == 320) rectSourceSprite.left = 0;
// else just jump to the right one
else rectSourceSprite.left += 64;

// set the sprite to the correct texture and rect properties
m_Sprite = Sprite(m_TextureLeft, rectSourceSprite);

// set the animate timer back to 0
m_animateTimer = 0;
}
}
//Used to ensure character doesn't move past boundry
//Used to ensure character doesn't move past boundary
else
{
m_Position.x = goal_PositionX;

// reset the sprite and rect back to first one not animated
rectSourceSprite = sf::IntRect(0, 0, 64, 64);
m_Sprite = Sprite(m_TextureLeft, rectSourceSprite);
}
}
if (randnum == 4)
Expand All @@ -179,20 +234,37 @@ void Enemy::Movement(float elapsedTime, float totalTime, Vector2f mapBounds)
if (m_Position.x < goal_PositionX)
{
m_Position.x += speed * elapsedTime;
m_Sprite.setRotation(0);
m_Texture.loadFromFile("graphics/polar.png");
m_Sprite.setTexture(m_Texture);

// every 0.2 seconds
if (m_animateTimer > 0.2f)
{
// if rect is at the right most of the sprite sheet reset it
if (rectSourceSprite.left == 320) rectSourceSprite.left = 0;
// else just jump to the right one
else rectSourceSprite.left += 64;

// set the sprite to the correct texture and rect properties
m_Sprite = Sprite(m_TextureRight, rectSourceSprite);

// set the animate timer back to 0
m_animateTimer = 0;
}
}
//Used to ensure character doesn't move past boundry
//Used to ensure character doesn't move past boundary
else
{
m_Position.x = goal_PositionX;

// reset the sprite and rect back to first one not animated
rectSourceSprite = sf::IntRect(0, 0, 64, 64);
m_Sprite = Sprite(m_TextureRight, rectSourceSprite);
}
}

//Depending on number, change position


m_Sprite.setOrigin(32, 32);
m_Sprite.setPosition(m_Position);
}

Expand Down Expand Up @@ -281,7 +353,7 @@ void Enemy::MoveTowards(float elapsedTime, float totalTime, Vector2f pPosition)
}
moveTime = totalTime + 1;
}

}
//std::cout << randnum << std::endl;

Expand Down
6 changes: 6 additions & 0 deletions can-you-survive/Player.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,12 @@ Player::Player(Vector2f position)

// create rect to navigate through the spriresheet
rectSourceSprite = sf::IntRect(0, 0, 64, 64);

m_Sprite.setTexture(m_TextureRight);
m_Sprite = Sprite(m_TextureRight, rectSourceSprite);

m_Sprite.setOrigin(32, 32);
m_Sprite.setPosition(m_Position);

m_animateTimer = 0;
}
Expand Down
5 changes: 0 additions & 5 deletions can-you-survive/PolarBear.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,6 @@ PolarBear::PolarBear(Vector2f position)

moveTime = 0;
m_damage = 5;

m_Texture.loadFromFile("graphics/polar.png");
m_Sprite.setTexture(m_Texture);
m_Sprite.setOrigin(32, 32);
m_Sprite.setPosition(m_Position);
}

//Used to spawn new polarbear when they die
Expand Down
1 change: 0 additions & 1 deletion can-you-survive/PolarBear.h
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,6 @@ class PolarBear
Sprite m_Sprite;

//Polar bear texture
Texture m_Texture;

Texture m_TextureUp;
Texture m_TextureDown;
Expand Down
12 changes: 7 additions & 5 deletions can-you-survive/SetupUI.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ void Engine::setupUI()
"1. Play Game" <<
"\n2. About" <<
"\n3. Instructions" <<
"\n4. References" <<
"\n4. Credits" <<
"\n5. Quit";
aboutStream <<
"This game is about the theme of global warming, staring a polar bear attempting" <<
Expand All @@ -44,10 +44,12 @@ void Engine::setupUI()
"\nSpace - Pause" <<
"\nEscape - Closes game (if still in main menu, will only return to play menu)";
referenceStream <<
"ice.png and snow.png - digit1024 on opengameart.com, originally called ice block (snow modified ice)" <<
"\npolar.png and polarflip - rapid punches on opengameart.com, part of Galapagos Penguin and Polar Bear" <<
"\nfishland.png and fishsea.png - skylerb on opengameart.com, part of 02 The Rescue Assets COMP 1501A" <<
"\n";
"Programmers:" <<
"\nDesmond Delaney"<<
"\nJorge Baptista" <<
"\n" <<
"\n Artist:" <<
"\n Isabelle Rellinghaus";
playText.setString(playStream.str());
playText.setCharacterSize(75);
playText.setFillColor(Color::White);
Expand Down
Binary file added can-you-survive/graphics/enemy/down.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added can-you-survive/graphics/enemy/left.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added can-you-survive/graphics/enemy/right.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added can-you-survive/graphics/enemy/up.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit d6dd94f

Please sign in to comment.