-
Notifications
You must be signed in to change notification settings - Fork 0
/
monster.cpp
98 lines (83 loc) · 1.66 KB
/
monster.cpp
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
#include <cassert>
#include <iostream>
#include "entities.hpp"
#include "game.hpp"
#include "util.hpp"
using namespace sf;
using namespace std;
const float SPEED = 160;
const float EAT_FRAME_DURATION = 3;
static sf::Texture frames[5];
void Monster::preload()
{
frames[0] = loadTexture("images/monster1.png");
frames[1] = loadTexture("images/monster2.png");
frames[2] = loadTexture("images/monster3.png");
frames[3] = loadTexture("images/monster4.png");
frames[4] = loadTexture("images/monster5.png");
}
Monster::Monster(Game *game)
{
this->game = game;
assert(game);
sprite.setTexture(frames[0]);
state = CHASING;
time = 0;
eatingTime = -1;
}
void Monster::update(float deltaTime)
{
time += deltaTime;
switch (state) {
case CHASING:
sprite.setTexture(frames[(int) (time * 2) % 2], true);
// move
{
Vector2f dir = game->player.getPosition() - getPosition();
Vector2f velocity = normalize(dir) * SPEED;
sprite.move(velocity * deltaTime);
}
break;
case EATING:
sprite.setTexture(frames[2], true);
break;
case CELEBRATING:
sprite.setTexture(frames[3 + (int) (time * 2) % 2], true);
break;
}
if (eatingTime > 0) {
float diff = time - eatingTime;
if (diff > EAT_FRAME_DURATION) {
state = CELEBRATING;
}
}
}
void Monster::collide(Actor &actor)
{
if ((state == CHASING) && getBounds().intersects(actor.getBounds())) {
cerr << "EATEN" << endl;
state = EATING;
eatingTime = time;
game->finish();
}
}
void Monster::jump()
{
// nothing to do
}
void Monster::bigJump()
{
// nothing to do
}
void Monster::knock()
{
// nothing to do
}
void Monster::knockHard()
{
// nothing to do
}
void Monster::slowDown()
{
// nothing to do
}