-
Notifications
You must be signed in to change notification settings - Fork 0
/
movingGround.cpp
128 lines (117 loc) · 3.49 KB
/
movingGround.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
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
#include "movingGround.h"
movingGround::movingGround(int curLevel, int Lx, int Rx, int y, int lx, int rx, int ly, int ry, string dire)
{
//ctor
mVelX = 0; mVelY = 0;
lim_lx = lx; lim_rx = rx;
lim_ly = ly; lim_ry = ry;// cout << ly << ' ' << ry << '\n';
d = dire;
mBox.x = Lx; mBox.w = Rx - Lx;
mBox.y = y; mBox.h = TILE_HEIGHT;
dir = 1;
LT = Lx / TILE_WIDTH + (LEVEL_WIDTH[curLevel] / TILE_WIDTH) * (y / TILE_HEIGHT); RT = Rx / TILE_WIDTH - 1 + (LEVEL_WIDTH[curLevel] / TILE_WIDTH) * (y / TILE_HEIGHT);
}
bool movingGround::onmvGround(Klee* klee){
SDL_Rect kleeBox = klee->getKleeBox();
if(kleeBox.x + kleeBox.w >= mBox.x && kleeBox.x <= mBox.x + mBox.w && kleeBox.y + kleeBox.h == mBox.y){
return true;
}
return false;
}
void movingGround::move(Tile* tiles[], Klee* klee){
if(d == "lr"){
//cout << LT % (7168 / 32) << ' ' << RT % (7168 / 32) << '\n';
bool ck = 0;
if(onmvGround(klee)){
ck = 1;
}
mVelX = dir * (GROUND_VEL + del);
int diff_x = klee->getKleeBox().x - tiles[LT]->getBox().x;
int x = tiles[LT]->getBox().x + mVelX;
int y = tiles[LT]->getBox().y;
int x_ = x;
if(x < lim_lx){
//tiles[LT]->updateBox(lim_lx, y);
dir = -dir;
x_ = lim_lx;
}
x = tiles[RT]->getBox().x + mVelX; y = tiles[RT]->getBox().y;
if(x > lim_rx - TILE_WIDTH){
tiles[RT]->updateBox(lim_rx - TILE_WIDTH, y);
dir = -dir;
x_ = lim_rx - TILE_WIDTH;
}
if(x_ != lim_rx - TILE_WIDTH){
x_ = max(lim_lx, tiles[LT]->getBox().x + mVelX);
for(int i = LT; i <= RT; i++){
tiles[i]->updateBox(x_, y); //cout << x_ << ' ';
x_ += TILE_WIDTH;
}
//cout << '\n';
}
else{
//cout << x_ << ' ';
for(int i = RT - 1; i >= LT; i--){
tiles[i]->updateBox(x_ - TILE_WIDTH, y);
//cout << x_ - TILE_WIDTH << ' ';
x_ -= TILE_WIDTH;
}
//cout << '\n';
}
mBox.x = tiles[LT]->getBox().x;
if(ck){
int new_x = mBox.x + diff_x;
int new_y = mBox.y - klee->getKleeBox().h;
klee->updatemvGround(new_x, new_y);
}
}
else{
bool ck = 0;
mVelY = dir * (GROUND_VEL + del);
if(onmvGround(klee)){
ck = 1;
}
int y = tiles[LT]->getBox().y + mVelY;
if(y < lim_ly){
y = lim_ly; dir = -dir;
}
if(y + TILE_HEIGHT > lim_ry){
y = lim_ry - TILE_HEIGHT; dir = -dir;
}
for(int i = LT; i <= RT; i++){
int x = tiles[i]->getBox().x;
tiles[i]->updateBox(x, y);
}
mBox.y = tiles[LT]->getBox().y;
//cout << mBox.x << ' ' << mBox.y << '\n';
if(ck){
int new_y = mBox.y - klee->getKleeBox().h; int new_x = klee->getKleeBox().x;
klee->updatemvGround(new_x, new_y);
}
// mBox.x = LT * TILE_WIDTH;
}
}
int movingGround::getLT(){
return LT;
}
int movingGround::getRT(){
return RT;
}
int movingGround::getLx(){
return mBox.x;
}
int movingGround::getRx(){
return mBox.x + mBox.w;
}
SDL_Rect movingGround::getBox(){
return mBox;
}
int movingGround::getly(){
return lim_ly;
}
int movingGround::getry(){
return lim_ry;
}
void movingGround::setDeltaVel(int delta){
del = delta;
}