-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathPlayer.cpp
More file actions
164 lines (141 loc) · 3.87 KB
/
Player.cpp
File metadata and controls
164 lines (141 loc) · 3.87 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
#include "Player.h"
#include <iostream>
#include <list>
#include "Render.h"
using namespace std;
void Player::moveX(const int amount) {
const int except = x + amount;
if (collide(except, y)) return;
previousX = x;
x = except;
}
void Player::moveY(const int amount) {
const int except = y + amount;
if (collide(x, except)) return;
previousY = y;
y = except;
}
/**
*
* pivot(x,y)에서 회전 공식을 사용한 변환:
*
*
* 90도 시계 방향으로 회전한 점 (x1, y1)는
* (y1, -x1)입니다. - (1) 행렬 변환Q
*
* 점 (x1, y1)를 피벗 (x, y)을 기준으로 이동시키면
* (x1 - x, y1 - y)가 됩니다. - (2) 상대적 좌표 이동
*
* 이동한 좌표를 90도 시계 방향으로 회전하면:
*
* (x1 - x, y1 - y)는 (1)
* X Y
* (y1 - y, - (x1 - x))로 변환됩니다.
* Y -X
*
* 원래의 피벗 위치로 다시 이동시키면 - (2)의 반대방향 이동
* 새로운 좌표는 (x + (y1 - y), y - (x1 - x))가 됩니다.
* X + x = x + (y1 - y)
* Y + y = y - ( x1-x )
*
*
*
*인덱스 x 0 1 2 3 0 1 2 3
* y
* 0 0 0 1 0 0 0 0 0 0
* 1 0 0 2 0 -> 1 4 3 2 1
* 2 0 0 3 0 2 0 0 0 0
* 3 0 0 4 0 3 0 0 0 0
*
* 90도 시계방향 회전으로 변환되었을때 인덱스가 변화되는 정도를 확인해보면
* pivot을 (2, 1)로 둠
* x, y
*
* x y x y
* 1 -> (2, 0) -> (3, 1)
* 2 -> (2, 1) -> (2, 1)
* 3 -> (2, 2) -> (1, 1)
* 4 -> (2, 3) -> (0, 1)
*
*/
void Player::rotate() const {
const int pivot_x = chunk.x;
const int pivot_y = chunk.y;
list<pair<int, int>> cells;
list<pair<int, int>> remove;
list<pair<int, int>> remove2;
int y1 = 0;
for (auto& y_cell : chunk.cell) {
int x1 = 0;
for (auto& x_cell: y_cell) {
if (x_cell == 1) {
// 원점을 기준으로 피벗 이동
int temp_x = x1 - pivot_x;
int temp_y = y1 - pivot_y;
// 90도 회전
int rotated_x = temp_y;
int rotated_y = -temp_x;
// 다시 피벗 위치로 이동
int new_x = rotated_x + pivot_x;
int new_y = rotated_y + pivot_y;
if (render.map[new_y + y][new_x + x] == 1) return;
remove2.emplace_front(x1, y1);
remove.emplace_front(x1 + x, y1 + y);
cells.emplace_front(new_x, new_y);
}
++x1;
}
++y1;
}
for (const auto& [x, y] : remove2) {
chunk.cell[y][x] = 0;
}
for (const auto& [x, y] : remove) {
render.map[y][x] = 0;
}
for (const auto& [x, y] : cells) {
chunk.cell[y][x] = 1;
}
// wcout << endl;
// for (auto& y_cell : chunk.cell) {
// wcout << endl;
// for (auto& x_cell: y_cell) {
// wcout << x_cell;
// }
// wcout << endl;
// }
}
void Player::update() {
list<pair<int, int>> temp;
const int deltaX = x - previousX, deltaY = y - previousY;
int y = this->y;
for (const auto& y_cell : chunk.cell) {
int x = this->x;
for (const auto& x_cell: y_cell) {
if (x_cell == 1) {
render.map[y - deltaY][x - deltaX] = 0;
temp.emplace_front(x, y);
}
++x;
}
++y;
}
previousX = this->x;
previousY = this->y;
for (const auto [x, y]: temp) {
render.map[y][x] = 2;
}
}
bool Player::collide(const int exceptX, const int exceptY) {
int y = exceptY;
for (auto& y_cell : chunk.cell) {
int x = exceptX;
for (auto& x_cell: y_cell) {
if (x_cell == 1 && render.map[y][x] == 1)
return true;
++x;
}
++y;
}
return false;
}