-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMonster.cpp
More file actions
159 lines (149 loc) · 3.49 KB
/
Monster.cpp
File metadata and controls
159 lines (149 loc) · 3.49 KB
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
#include "main.h"
// constructor
Monster::Monster(MONSTER Type) : type(Type), direction(DOWN), trapped(false), rotation(false), alive(true), rotateAngle(0.0f), trappedBubble(-1) {
setPosition(0.0f, 0.0f);
initTextureImage();
}
// draw mothods
void Monster::drawMonster()
{
setTextureID(direction);
glPushMatrix();
drawRotate();
glEnable(GL_TEXTURE_2D);
glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
glBindTexture(GL_TEXTURE_2D, textureID);
glBegin(GL_POLYGON);
glTexCoord2f(0.0f, 1.0f); glVertex2f(getPositionX() + 0.0f, getPositionY() + 0.2f);
glTexCoord2f(1.0f, 1.0f); glVertex2f(getPositionX() + 0.2f, getPositionY() + 0.2f);
glTexCoord2f(1.0f, 0.0f); glVertex2f(getPositionX() + 0.2f, getPositionY() + 0.0f);
glTexCoord2f(0.0f, 0.0f); glVertex2f(getPositionX() + 0.0f, getPositionY() + 0.0f);
glEnd();
glDisable(GL_TEXTURE_2D);
glPopMatrix();
}
void Monster::drawRotate()
{
if (isTrapped ()) {
rotateAngle += 5.0f;
glTranslatef(getPositionX() + 0.085f, getPositionY() + 0.085f, 1.0f);
glRotatef(rotateAngle, 0.0f, 0.0f, 1.0f);
glScalef(0.65f, 0.65f, 0.65f);
glTranslatef(-getPositionX() - 0.085f, -getPositionY() - 0.085f, 0.0f);
}
}
// texture methods
void Monster::initTextureImage()
{
switch (type) {
case(ROBOT):
leftImage = Texture(_MONSTER, _ROBOT, _LEFT);
rightImage = Texture(_MONSTER, _ROBOT, _RIGHT);
break;
case(CREATURE):
leftImage = Texture(_MONSTER, _CREATURE, _LEFT);
rightImage = Texture(_MONSTER, _CREATURE, _RIGHT);
break;
case(GHOST):
leftImage = Texture(_MONSTER, _GHOST, _LEFT);
rightImage = Texture(_MONSTER, _GHOST, _RIGHT);
break;
}
leftImage.initTexture();
rightImage.initTexture();
}
void Monster::setTextureID(KEY dir)
{
if (dir == LEFT) {
textureID = leftImage.getTextureID();
}
else textureID = rightImage.getTextureID();
}
// reflect monster's collision with a bubble
void Monster::trap(int key)
{
trapped = true;
trappedBubble = key;
}
bool Monster::isTrapped()
{
return trapped;
}
bool Monster::isAlive()
{
return alive;
}
void Monster::kill()
{
alive = false;
}
void Monster::free()
{
trapped = false;
rotateAngle = 0.0f;
direction = DOWN;
trappedBubble = -1;
}
/* change monster's x, y coordinates
when monster is out of the window, change the position of player
*/
void Monster::updatePosition()
{
// out of the left side
if ((direction == KEY::LEFT) && (getPositionX() + 0.01f < -1.2f)) {
setPositionX(1.05f);
}
// out of the right side
else if ((direction == KEY::RIGHT) && (getPositionX() + 0.16f > 1.2f)) {
setPositionX(-1.18f);
}
// out of the bottom side
if (getPositionY() < -1.2f) {
setPositionY(1.0f);
}
}
void Monster::setDirection(KEY drt)
{
direction = drt;
}
KEY Monster::getDirection()
{
return direction;
}
MONSTER Monster::getType()
{
return type;
}
int Monster::getTrappedBubble()
{
if (!isTrapped()) {
cout << "This method should not be called" << endl;
return -1;
}
return trappedBubble;
}
Hitbox Monster::getHitbox()
{
float xLeft, xRight, yBottom, yTop;
switch (type) {
case(ROBOT):
xLeft = getPositionX() + 0.03f;
xRight = getPositionX() + 0.14f;
yBottom = getPositionY();
yTop = getPositionY() + 0.14f;
break;
case(CREATURE):
xLeft = getPositionX() + 0.03f;
xRight = getPositionX() + 0.16f;
yBottom = getPositionY();
yTop = getPositionY() + 0.14f;
break;
case(GHOST):
xLeft = getPositionX() + 0.04f;
xRight = getPositionX() + 0.16f;
yBottom = getPositionY();
yTop = getPositionY() + 0.16f;
break;
}
return Hitbox(xLeft, xRight, yBottom, yTop);
}