-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPlayer_1.cpp
More file actions
293 lines (258 loc) · 8.41 KB
/
Player_1.cpp
File metadata and controls
293 lines (258 loc) · 8.41 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
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
#include "Player.h"
#include <iostream>
#include <cmath>
/*
struct state_of_game {
struct tilemap *map;
struct tilemap *lose;
struct tilemap *win;
};
static struct tilemap *create_death_screen(void) {
struct tilemap *map = create_screen(STATIC_SCREEN_WIDTH, STATIC_SCREEN_HEIGHT);
draw_message(map, 6, 2, "YOU DIED");
draw_message(map, 1, 3, "Press R to restart");
draw_message(map, 3, 4, "or ESC to exit");
for (size_t y = 0; y < map->height; y++) {
for (size_t x = 0; x < map->width; x++) {
int r = rand();
if (r/2 % 7 == 0) {
tilemap_set_tile(map, x, y, 1, r & 1 ? TILE_BONES_1 : TILE_BONES_2);
}
}
}
tilemap_refresh(map);
return map;
}*/
bool Player::Moved() const
{
if(coords.x == old_coords.x && coords.y == old_coords.y)
return false;
else
return true;
}
void Player::ProcessInput(MovementDir dir, Level &cur_lev)
{
int move_dist = move_speed * 1;
//std::cout << 20 - coords.y / tileSize << " " << coords.x / tileSize << std::endl;
//std::cout << 20 - coords.y / tileSize << " " << WINDOW_HEIGHT - coords.y << std::endl;
switch(dir)
{
case MovementDir::UP:
old_coords.y = coords.y;
coords.y += move_dist;
break;
case MovementDir::DOWN:
old_coords.y = coords.y;
coords.y -= move_dist;
break;
case MovementDir::LEFT:
old_coords.x = coords.x;
coords.x -= move_dist;
break;
case MovementDir::RIGHT:
old_coords.x = coords.x;
coords.x += move_dist;
break;
default:
break;
}
/* int x_0 = (coords.x + tileSize / 2) / tileSize;
int y_0 = (coords.y + tileSize / 2) / tileSize;
for (int y = -1; y <= 1; y++) {
for (int x = -1; x <= 1; x++) {
int box_x = (x_0 + x) * tileSize;
int box_y = (y_0 + y) * tileSize;
double y_h = coords.y < box_y ? std::max(0, coords.y + tileSize - box_y) : std::min(0, coords.y - box_y - tileSize);
double x_h = coords.x < box_x ? std::max(0, coords.x + tileSize - box_x) : std::min(0, coords.x - box_x - tileSize);
if (cur_lev.level_arr[20 - y_0][x_0] == '#')
{
std::cout << "y_h = " << y_h << " x_h = " << x_h << std::endl;
if (fabs(x_h) < fabs(y_h))
{
coords.x -= x_h;
}
else
{
coords.y -= y_h;
}
}
else if (cur_lev.level_arr[20 - y_0][x_0] == '_')
{
/* if (fmin(fabs(hx), fabs(hy)) > 0) {
state.state = s_game_over;
want_redraw = 1;
}
break;
*/ /* Попадание в пустое место
КОГДА_НИБУДЬ Я ЭТО НАПИШУ
*/
/* } else if (cur_lev.level_arr[20 - y_0][x_0] == 'x')
{
}
}
}*/
//tilemap_refresh(state.map);
/*
int x_n = (coords.x + tileSize/2) / tileSize;
int y_n = (coords.y + tileSize/2) / tileSize;
std::cout << "\n\n";
for (int y = -1; y <= 1; y++) {
for (int x = -1; x <= 1; x++) {
int box_x = (x + x_n) * tileSize;
int box_y = (y + y_n) * tileSize;
int b_h = tileSize, b_w = tileSize;
if (cur_lev.level_arr[20 - (y + y_n + 1)][x + x_n] != '#')
{
b_h /= 2;
}
if ((cur_lev.level_arr[20 - (y + y_n)][x + x_n - 1] != '#') &&
(cur_lev.level_arr[20 - (y + y_n)][x + x_n + 1] == '#'))
{
b_w /= 2;
box_x += tileSize / 2;
} else if ((cur_lev.level_arr[21 - (y + y_n)][x + x_n - 1] == '#') &&
(cur_lev.level_arr[20 - (y + y_n)][x + x_n + 1] != '#'))
{
b_w /= 2;
}
double x0 = coords.x, y0 = coords.y;
double hy = ((y0 < box_y ? std::fmax(0, y0 + tileSize - box_y) : std::fmin(0, y0 - box_y - b_h)));
double hx = ((x0 < box_x ? std::fmax(0, x0 + tileSize - box_x) : std::fmin(0, x0 - box_x - b_w)));
std::cout << y << " " << x << " :: " << 21 - (y + y_n) << " "<< x + x_n << " " << cur_lev.level_arr[21 - (y + y_n)][x + x_n] << std:: endl;
if (cur_lev.level_arr[21 - (y + y_n)][x + x_n] == '#')
{
if (fabs(hx) < fabs(hy))
{
coords.x -= hx;
} else
{
coords.y -= hy;
}
break;
}
}
}
*/
/*int x = coords.x / tileSize;
int y = coords.y / tileSize;
//std::cout << x << " " << y << " " <<cur_lev.level_arr[y][x] << std::endl;
int box_x = x * tileSize;
int box_y = y * tileSize;
//std::cout << coords.y << " " << box_y << " " << coords.x << " " << box_x << std::endl;
double y_h = coords.y < box_y ? std::max(0, coords.y + tileSize - box_y) : std::min(0, coords.y - box_y - tileSize);
double x_h = coords.x < box_x ? std::max(0, coords.x + tileSize - box_x) : std::min(0, coords.x - box_x - tileSize);
if (cur_lev.level_arr[20 - y][x] == '#')
{
if (fabs(x_h) < fabs(y_h))
{
coords.x -= x_h;
}
else
{
coords.y -= y_h;
}
}
x = (coords.x + tileSize - 1) / tileSize;
y = coords.y / tileSize;
box_x = x * tileSize;
box_y = y * tileSize;
y_h = coords.y < box_y ? std::max(0, coords.y + tileSize - box_y) : std::min(0, coords.y - box_y - tileSize);
x_h = coords.x < box_x ? std::max(0, coords.x + tileSize - box_x) :std:: min(0, coords.x - box_x - tileSize);
if (cur_lev.level_arr[20 - y][x] == '#')
{
if (fabs(x_h) < fabs(y_h))
{
coords.x -= x_h;
}
else
{
coords.y -= y_h;
}
}
x = (coords.x) / tileSize;
y = (coords.y + tileSize - 1) / tileSize;
box_x = x * tileSize;
box_y = y * tileSize;
y_h = coords.y < box_y ? std::max(0, coords.y + tileSize - box_y) : std::min(0, coords.y - box_y - tileSize);
x_h = coords.x < box_x ? std::max(0, coords.x + tileSize - box_x) : std::min(0, coords.x - box_x - tileSize);
if (cur_lev.level_arr[20 - y][x] == '#')
{
if (fabs(x_h) < fabs(y_h))
{
coords.x -= x_h;
}
else
{
coords.y -= y_h;
}
}
x = (coords.x - tileSize) / tileSize;
y = coords.y / tileSize;
box_x = x * tileSize;
box_y = y * tileSize;
y_h = coords.y < box_y ? std::max(0, coords.y + tileSize - box_y) : std::min(0, coords.y - box_y - tileSize);
x_h = coords.x < box_x ? std::max(0, coords.x + tileSize - box_x) :std:: min(0, coords.x - box_x - tileSize);
if (cur_lev.level_arr[20 - y][x] == '#')
{
if (fabs(x_h) < fabs(y_h))
{
coords.x -= x_h;
}
else
{
coords.y -= y_h;
}
}
x = (coords.x) / tileSize;
y = (coords.y - tileSize) / tileSize;
box_x = x * tileSize;
box_y = y * tileSize;
y_h = coords.y < box_y ? std::max(0, coords.y + tileSize - box_y) : std::min(0, coords.y - box_y - tileSize);
x_h = coords.x < box_x ? std::max(0, coords.x + tileSize - box_x) : std::min(0, coords.x - box_x - tileSize);
if (cur_lev.level_arr[20 - y][x] == '#')
{
if (fabs(x_h) < fabs(y_h))
{
coords.x -= x_h;
}
else
{
coords.y -= y_h;
}
}
*/
}
Pixel blend(Pixel a, Pixel b) {
return {
(a.r*(255 - b.a) + b.r*b.a)/255,
(a.g*(255 - b.a) + b.g*b.a)/255,
(a.b*(255 - b.a) + b.b*b.a)/255,
(a.r*(255 - b.a) + b.a)/255
};
}
void Player::Draw(Image &screen)
{
/*if(Moved())
{
for(int y = old_coords.y; y < old_coords.y + tileSize; ++y)
{
for(int x = old_coords.x; x < old_coords.x + tileSize; ++x)
{
screen.PutPixel(x, y, backgroundColor);
}
}
old_coords = coords;
}*/
Image hero("./resources/Dungeon_Character.png");
double xscale = 1/3.;
double yscale = 1/3.;
for (size_t y = 0; y < tileSize; y++)
{
for (size_t x = 0; x < tileSize; x++)
{
auto x1 = hero.GetPixel(3 * 16 + x * xscale, 3 * 16 - (int)(y * yscale) - 1);
auto x2 = screen.GetPixel(x + coords.x, y + coords.y);
screen.PutPixel(x + coords.x, y + coords.y, blend(x2, x1));
}
}
}