-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTurtle.cpp
More file actions
126 lines (112 loc) · 2.61 KB
/
Copy pathTurtle.cpp
File metadata and controls
126 lines (112 loc) · 2.61 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
#pragma once
#include "Turtle.h"
enum STATES { MOVE = 0, VULNERABLE, SURPRISE, FALL};
Turtle::Turtle(RenderWindow* window,int heading=1,float vx=TURTLE_SPEED) : Object(window)
{
vy = 0;
collusion = false;
frame = 7;
state = MOVE;
sprite.setTexture(textures[frame]);
if(heading==1) //which pipe to spawn
pos = Vector2f(80,250);
else
pos= Vector2f(870, 250);
sprite.setPosition(pos);
this->heading = heading;
this->vx = heading*vx;
animationTimer.restart();
}
void Turtle::fall(void)
{
//Turtle falls straight
frame = 11;
state = FALL;
sprite.setTexture(textures[frame]);
dead = true;
vx = 0;
vy = 10;
}
void Turtle::vulnerable(void)
{
//Turtle jumps up and after reached the ground stay straight for 8 seconds.
if (state!=VULNERABLE)
{
state = VULNERABLE;
frame = 11;
sprite.setTexture(textures[frame]);
vy = -10.8;
vulnerableTime.restart();
}
}
void Turtle::update()
{
//Updates movements of turtle
if (state==SURPRISE)
{
sprite.setTexture(textures[frame]);
if(surpriseTime.getElapsedTime().asSeconds()>0.5)
{
state=MOVE;
frame = 7;
}
}
else
{
updatePhysics();
}
if(state==VULNERABLE && vulnerableTime.getElapsedTime().asSeconds()>8)
{
state = MOVE;
}
if (state==MOVE)
{
Vector2f position;
float elapsed1 = animationTimer.getElapsedTime().asSeconds();
position = sprite.getPosition();
if (heading == 1) {
sprite.setOrigin(sprite.getLocalBounds().width / 2.f, 0.f);
sprite.setScale(1.f, 1.f);
}
else {
sprite.setOrigin(sprite.getLocalBounds().width / 2.f, 0.f);
sprite.setScale(-1.f, 1.f);
}
sprite.setTexture(textures[frame]);
if (elapsed1 > 0.5)
{
if (frame < 9) {
frame++;
}
else
{
frame = 7;
}
animationTimer.restart();
}
}
draw();
}
void Turtle::surprised(void)
{
//Called when turtle is collided with another turtle
if (state != VULNERABLE)
{
state = SURPRISE;
frame = 10;
surpriseTime.restart();
}
}
void Turtle::updatePhysics()
{
//Updates gravitional forces on turtle
vy += 0.5;
if (vy > VELOCITY_Y && state!=FALL)
{
vy = VELOCITY_Y;
}
if (state==VULNERABLE)
sprite.move(Vector2f(0, vy));
else
sprite.move(Vector2f(vx, vy));
}