-
Notifications
You must be signed in to change notification settings - Fork 0
/
hero.h
195 lines (184 loc) · 6.25 KB
/
hero.h
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
#pragma once
#include <SFML/Audio.hpp>
#include <SFML/Graphics.hpp>
#include <bits/stdc++.h>
#include "effect.h"
using namespace std;
using namespace sf;
class Hero{
private:
int x;
int y;
int height;
int direction;
int life;
bool alive;
bool wanaJump;
float acceleration;
bool isUndead;
Music music;
bool isPlaymusic;
Clock clock;
Time time;
Animation animation[6]; // 0.RightStand 1.LeftStand 2.RightMove 3.LeftMove 4.RightJump 5.LeftJump
public:
friend class HealthPoint;
friend class Angel;
Hero(){
acceleration = 2;
x = 200;
y = 600;
life = 4;
isPlaymusic = false;
wanaJump = false;
isUndead = false;
animation[0].scaleAnimation(1.0f,0.7f);
animation[1].scaleAnimation(1.0f,0.7f);
animation[2].scaleAnimation(1.0f,0.7f);
animation[3].scaleAnimation(1.0f,0.7f);
animation[4].scaleAnimation(1.0f,0.7f);
animation[5].scaleAnimation(1.0f,0.7f);
animation[0].setAnimation(path_RightStand , 0.009f);
animation[1].setAnimation(path_LeftStand, 0.009f);
animation[2].setAnimation(path_RightMove,0.3f);
animation[3].setAnimation(path_LeftMove,0.3f);
animation[4].setAnimation(path_RightJump,0.04f);
animation[5].setAnimation(path_LeftJump,0.04f);
}
bool getHeroIsUndead(){
return isUndead;
}
void setHeroIsUndead(bool para){
isUndead = para;
}
bool getHeroAlive(){
return alive;
}
void setHeroAlive(bool x){
alive = x;
}
int getHeroDirection(){
return direction;
}
void setHeroDirection(int para){
direction = para;
}
int getHeroLife(){
return life;
}
void setHeroLife(int para){
life = para;
}
void setHeroPos(int x, int y){
this -> x = x;
this -> y = y;
}
II getHeroPos(){
return make_pair(x,y);
}
void processDeath(){
if (y > Height) alive = false;
if (alive == false){
music.openFromFile("path/die.wav");
music.setVolume(100);
music.play();
wanaJump = false;
direction = 0;
setHeroPos(300,450);
alive = true;
life--;
isUndead = true;
clock.restart();
}
time = clock.getElapsedTime();
if (time.asSeconds() > 2.0f && isUndead){
isUndead = false;
}
}
bool isReactBlock(){
for(int i=0;i<5;i++){
int Y = groundz[i].getGroundzPosY();
int X1 = groundz[i].getGroundzPosX().first;
int X2 = groundz[i].getGroundzPosX().second;
if (y > Y && y < Y+100 && x > X1 && x < X2) return true;
}
return false;
}
void OutOfOder(){
if (x < 0) x = Width;
if (x > Width) x = 0;
}
void gravityFall(){
bool isFall = true;
if (wanaJump == false){
for(int i=0;i<5;i++){
int X1 = groundz[i].getGroundzPosX().first;
int X2 = groundz[i].getGroundzPosX().second;
int Y = groundz[i].getGroundzPosY();
if ( ( x > X1 && x < X2 && y >= Y && y <= Y + 20) ){
isFall = false;
y = groundz[i].getGroundzPosY();
}
}
}
if (isFall){
y = y + acceleration*1.5;
}
}
void gravityJump(){
if (getHeroPos().second < height) wanaJump = false;
if (wanaJump){
setHeroPos(getHeroPos().first , getHeroPos().second - 6);
}
}
Sprite getSprite(){
return animation[direction].getAnimationSprite();
}
/* Activities */
void moveRight(){
direction = 2;
if (isReactBlock() == false) setHeroPos(getHeroPos().first + 2 ,getHeroPos().second);
}
void moveLeft(){
direction = 3;
if (isReactBlock() == false) setHeroPos(getHeroPos().first - 2 ,getHeroPos().second);
}
void jump(){
for(int i=0;i<5;i++){
int X1 = groundz[i].getGroundzPosX().first;
int X2 = groundz[i].getGroundzPosX().second;
int Y = groundz[i].getGroundzPosY();
if (x > X1 && x < X2 && y == Y) wanaJump = true, isPlaymusic = false;
}
if (y == groundz[0].getGroundzPosY()) height = 450;
if (y == groundz[1].getGroundzPosY()) height = 350;
if (y == groundz[3].getGroundzPosY()) height = 250;
if ((direction == 2 || direction == 0)) direction = 4;
if ((direction == 1 || direction == 3)) direction = 5;
if (isPlaymusic == false){
music.openFromFile("path/jump.wav");
music.setVolume(10);
music.play();
isPlaymusic = true;
}
}
/*--------------------*/
void draw(){
bool isFall = true;
for(int i=0;i<5;i++){
int X1 = groundz[i].getGroundzPosX().first;
int X2 = groundz[i].getGroundzPosX().second;
int Y = groundz[i].getGroundzPosY();
if (y == Y && x > X1 && x < X2) isFall = false;
}
if (isFall == false && direction == 4) direction = 0;
if (isFall == false && direction == 5) direction = 1;
if (direction == 2 && isFall) direction = 4;
if (direction == 3 && isFall) direction = 5;
animation[direction].update(x,y-40);
animation[direction].draw();
if (direction % 2 == 0 && direction != 4) direction = 0;
if ( direction % 2 == 1 && direction != 5) direction = 1;
}
};
Hero hero;