-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinput.cpp
More file actions
75 lines (70 loc) · 2.86 KB
/
input.cpp
File metadata and controls
75 lines (70 loc) · 2.86 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
#include "input.h"
#include <cmath>
#include "doors.h"
namespace {
bool isWalkable(double x, double y, const Map& map, const std::vector<Door>& doors) {
int cellX = static_cast<int>(x);
int cellY = static_cast<int>(y);
int tile = map.at(cellX, cellY);
if (tile == 0) {
return true;
}
if (tile == DOOR_TILE) {
const Door* door = findDoor(doors, cellX, cellY);
return door && door->openAmount > 0.8;
}
return false;
}
} // namespace
void handleInput(const Uint8* keystate, const Map& map, std::vector<Door>& doors, Player& player, const Config& cfg, double dt) {
double moveStep = cfg.moveSpeed * dt;
double rotStep = cfg.rotSpeed * dt;
if (keystate[SDL_SCANCODE_LSHIFT] || keystate[SDL_SCANCODE_RSHIFT]) {
moveStep = cfg.moveSpeedSprint * dt;
}
if (keystate[SDL_SCANCODE_W] || keystate[SDL_SCANCODE_UP]) {
double nextX = player.x + player.dirX * moveStep;
double nextY = player.y + player.dirY * moveStep;
if (isWalkable(nextX, player.y, map, doors)) {
player.x = nextX;
}
if (isWalkable(player.x, nextY, map, doors)) {
player.y = nextY;
}
}
if (keystate[SDL_SCANCODE_S] || keystate[SDL_SCANCODE_DOWN]) {
double nextX = player.x - player.dirX * moveStep;
double nextY = player.y - player.dirY * moveStep;
if (isWalkable(nextX, player.y, map, doors)) {
player.x = nextX;
}
if (isWalkable(player.x, nextY, map, doors)) {
player.y = nextY;
}
}
if (keystate[SDL_SCANCODE_A] || keystate[SDL_SCANCODE_LEFT]) {
double oldDirX = player.dirX;
player.dirX = player.dirX * std::cos(rotStep) - player.dirY * std::sin(rotStep);
player.dirY = oldDirX * std::sin(rotStep) + player.dirY * std::cos(rotStep);
double oldPlaneX = player.planeX;
player.planeX = player.planeX * std::cos(rotStep) - player.planeY * std::sin(rotStep);
player.planeY = oldPlaneX * std::sin(rotStep) + player.planeY * std::cos(rotStep);
}
if (keystate[SDL_SCANCODE_D] || keystate[SDL_SCANCODE_RIGHT]) {
double oldDirX = player.dirX;
player.dirX = player.dirX * std::cos(-rotStep) - player.dirY * std::sin(-rotStep);
player.dirY = oldDirX * std::sin(-rotStep) + player.dirY * std::cos(-rotStep);
double oldPlaneX = player.planeX;
player.planeX = player.planeX * std::cos(-rotStep) - player.planeY * std::sin(-rotStep);
player.planeY = oldPlaneX * std::sin(-rotStep) + player.planeY * std::cos(-rotStep);
}
static bool wasSpaceDown = false;
bool isSpaceDown = keystate[SDL_SCANCODE_SPACE];
if (isSpaceDown && !wasSpaceDown) {
Door* target = doorInFront(player, map, doors);
if (target) {
target->targetOpen = !target->targetOpen;
}
}
wasSpaceDown = isSpaceDown;
}