summary refs log tree commit diff stats
path: root/src/components/player_sprite.cpp
blob: 452a9407b541d389745f6e2d9b4b3bbcf16ab29e (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#include "player_sprite.h"

PlayerSpriteComponent::PlayerSpriteComponent() : sprite("res/Starla.png")
{
  
}

void PlayerSpriteComponent::render(Game&, Entity& entity, Texture& buffer)
{
  animFrame++;
  
  int frame = 0;
  if (isMoving)
  {
    frame += 2;
    
    if (animFrame % 20 < 10)
    {
      frame += 2;
    }
  }
  
  if (facingLeft)
  {
    frame++;
  }
  
  double alpha = 1.0;
  if (dying && (animFrame % 4 < 2))
  {
    alpha = 0.0;
  }
  
  Rectangle src_rect {frame*10, 0, 10, 12};
  Rectangle dst_rect {(int) entity.position.first, (int) entity.position.second, entity.size.first, entity.size.second};
  buffer.blit(sprite, src_rect, dst_rect, alpha);
}

void PlayerSpriteComponent::receive(Game&, Entity&, const Message& msg)
{
  if (msg.type == Message::Type::walkLeft)
  {
    facingLeft = true;
    isMoving = true;
  } else if (msg.type == Message::Type::walkRight)
  {
    facingLeft = false;
    isMoving = true;
  } else if (msg.type == Message::Type::stopWalking)
  {
    isMoving = false;
  } else if (msg.type == Message::Type::die)
  {
    dying = true;
    isMoving = false;
  } else if (msg.type == Message::Type::stopDying)
  {
    dying = false;
  }
}