-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTurtle.cpp
More file actions
247 lines (246 loc) · 7.16 KB
/
Turtle.cpp
File metadata and controls
247 lines (246 loc) · 7.16 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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
#include"Headers/Turtle.h"
#include"Headers/Map.h"
#include"Headers/Mario.h"
using namespace std;
using namespace sf;
const int dead_timer=20;
Turtle::Turtle(){
sprite.setTexture(GenTexture::getTexture("Turtle.png"),true);
sprite.setTextureRect(IntRect(0,0,TurtleWidth,TurtleHeight));
type=Enemy;
state=Alive;
hspeed=TurtleHSpeed;
height=TurtleHeight;
width=TurtleWidth;
}
void Turtle::walkAnimation(){
if(dua--==0){
dua=TurtleAnimationDuration;
sprite.setTextureRect(IntRect((anipos++%2)*TurtleWidth,0,TurtleWidth,TurtleHeight));
}
}
void Turtle::rwalkAnimation(){
if(dua--==0){
dua=TurtleAnimationDuration;
sprite.setTextureRect(IntRect((anipos++%2+1)*TurtleWidth,0,-TurtleWidth,TurtleHeight));
}
}
void Turtle::draw(){
sprite.setPosition(Vector2f(px,py));
window->draw(sprite);
}
void Turtle::update(){
switch(state){
case Alive:{
Vector2f pos=mario->pos;
if(mario->vspeed>0){
if(checkCollision(pos.x,pos.y,DOWN)){
startDead();
mario->vspeed=EnemyJump;
}
}else{
if(checkCollision(pos.x,pos.y,ALL)){
mario->startDead();
}
}
//更新位置
vspeed=min(vspeed+Gravity,MaxVSpeed);
if(vspeed>0){
if(checkCollision(DOWN,Brick_)){
vspeed=0;
}
}
if(hspeed>0){
rwalkAnimation();
if(checkCollision(RIGHT,Brick_)){
dua=0;
hspeed=-hspeed;
}
}else if(hspeed<0){
walkAnimation();
if(checkCollision(LEFT,Brick_)){
dua=0;
hspeed=-hspeed;
}
}
//更新位置
px+=hspeed;
py+=vspeed;
break;
}
case Shrink:{
Vector2f pos=mario->pos;
if(checkCollision(pos.x,pos.y,ALL)){
if(pos.x<px){
hspeed=TurtleSlideSpeed;
}else{
hspeed=-TurtleSlideSpeed;
}
state=Dying;
deadTimer=dead_timer;
}
sprite.setTextureRect(IntRect(TurtleWidth*3,TurtleHeight-CellSize,TurtleWidth,CellSize));
break;
}
case Dying:{
Vector2f pos=mario->pos;
vspeed=min(vspeed+Gravity,MaxVSpeed);
sprite.setTextureRect(IntRect(TurtleWidth*3,TurtleHeight-CellSize,TurtleWidth,CellSize));
if(vspeed>0){
if(checkCollision(DOWN,Brick_)){
vspeed=0;
}
}
if(deadTimer>0){
deadTimer--;
}else{
if(checkCollision(pos.x,pos.y,ALL)){
mario->startDead();
}
Vector2f pos;
for(auto& it:marioMap->enemies){
pos=it->getPosition();
if(it->type==Geezer_){
if(checkCollision(pos.x,pos.y,ALL)){
it->startDead2();
}
}else if(it->type==Turtle_){
if(checkCollision(pos.x,pos.y,ALL,TurtleWidth,TurtleHeight)){
it->startDead2();
}
}
}
}
if(hspeed>0){
if(checkCollision(RIGHT,Brick_)){
dua=0;
hspeed=-hspeed;
}
}else if(hspeed<0){
if(checkCollision(LEFT,Brick_)){
dua=0;
hspeed=-hspeed;
}
}
px+=hspeed;
py+=vspeed;
break;
}
case DeadBounce:{
if(deadBounceTimer){
deadBounceTimer--;
vspeed=min(vspeed+Gravity,MaxVSpeed);
if(vspeed<0){
sprite.setTextureRect(IntRect(0,0,CellSize,TurtleHeight));
}else{
sprite.setTextureRect(IntRect(0,TurtleHeight,CellSize,-TurtleHeight));
}
py+=vspeed;
}else{
state=Dead;
}
break;
}
case Dead:
break;
}
}
void Turtle::startDead(){
if(state!=Dying&&state!=Dead&&state!=DeadBounce){
state=Shrink;
hspeed=0;
py+=TurtleHeight-CellSize;
}
}
void Turtle::startDead2(){
if(state!=Dying&&state!=Dead&&state!=DeadBounce){
deadBounceTimer=DeadTime;
state=DeadBounce;
hspeed=0;
py+=TurtleHeight-CellSize;
vspeed=DeadJump;
}
}
void Turtle::setPos(int x,int y){
px=x*CellSize;
py=y*CellSize+CellSize-TurtleHeight;
}
Vector2f Turtle::getPosition(){
return Vector2f(px,py);
}
bool Turtle::checkCollision(float x,float y,Direc direct,int objWidth,int objHeight){
int height=TurtleHeight;
if(state==Alive){
height=TurtleHeight;
}else{
height=CellSize;
}
switch(direct){
case UP:
if(x-px>=-objWidth&&x-px<=TurtleWidth&&y-py>=0&&y-py<=height){
return true;
}
break;
case DOWN:
if(x-px>=-objWidth&&x-px<=TurtleWidth&&y-py<=0&&y-py>=-objHeight){
return true;
}
break;
case LEFT:
if(x-px>0&&x-px<TurtleWidth&&y-py>-objHeight&&y-py<height){
return true;
}
break;
case RIGHT:
if(x-px<0&&x-px>-objWidth&&y-py>-objHeight&&y-py<height){
return true;
}
break;
case ALL:
if(x-px>=-objWidth&&x-px<=TurtleWidth&&y-py>=-objHeight&&y-py<=height){
return true;
}
break;
}
return false;
}
bool Turtle::checkCollision(Direc direct,Type type){
if(direct==DOWN){
if(type==Brick_){
for(auto& v:marioMap->m){
for(auto& it:v){
if(it.type==Brick_){//砖块检测方向正好相反
if(checkCollision(it.entity->px*CellSize,it.entity->py*CellSize,UP)){
return true;
}
}
}
}
}
}else if(direct==LEFT){
if(type==Brick_){
for(auto& v:marioMap->m){
for(auto& it:v){
if(it.type==Brick_){
if(checkCollision(it.entity->px*CellSize,it.entity->py*CellSize,RIGHT)){
return true;
}
}
}
}
}
}else if(direct==RIGHT){
if(type==Brick_){
for(auto& v:marioMap->m){
for(auto& it:v){
if(it.type==Brick_){
if(checkCollision(it.entity->px*CellSize,it.entity->py*CellSize,LEFT)){
return true;
}
}
}
}
}
}
return false;
}