-
Notifications
You must be signed in to change notification settings - Fork 0
/
LocalPlayer.d
108 lines (86 loc) · 2.36 KB
/
LocalPlayer.d
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
module LocalPlayer;
import std.math;
import derelict.sdl.sdl;
import IPlayer;
import GlobalVariables;
class LocalPlayer : IPlayer {
float[3] pos;
float[2] angle;
bool moveUp;
bool moveDown;
bool moveForward;
bool moveBackward;
bool moveLeft;
bool moveRight;
this() {
pos = [-20.0f, blockHeight * (groundLevel + 3), 10.0f];
angle = [265.0f, 180.0f];
render.setCameraPosition(pos);
render.setCameraAngle(angle);
input.bindKey(SDLK_w, &onMoveForward);
input.bindKey(SDLK_s, &onMoveBackward);
input.bindKey(SDLK_a, &onMoveLeft);
input.bindKey(SDLK_d, &onMoveRight);
input.bindKey(SDLK_UP, &onMoveUp);
input.bindKey(SDLK_DOWN, &onMoveDown);
input.bindMouseMotion(&onMouseMotion);
debug input.bindKey(SDLK_q, &loadMore);
}
debug {
int radius = 1;
void loadMore(SDL_Event event) {
if(event.type == SDL_KEYDOWN) {
++radius;
foreach(i; -radius..radius+1)
foreach(j; -radius..radius+1)
thread.addPosToBlockLoader([i, 0, j]);
}
}
}
void onMoveUp(SDL_Event event) {
moveUp = event.type == SDL_KEYDOWN;
}
void onMoveDown(SDL_Event event) {
moveDown = event.type == SDL_KEYDOWN;
}
void onMoveForward(SDL_Event event) {
moveForward = event.type == SDL_KEYDOWN;
}
void onMoveBackward(SDL_Event event) {
moveBackward = event.type == SDL_KEYDOWN;
}
void onMoveLeft(SDL_Event event) {
moveLeft = event.type == SDL_KEYDOWN;
}
void onMoveRight(SDL_Event event) {
moveRight = event.type == SDL_KEYDOWN;
}
void onMouseMotion(SDL_Event event) {
angle[0] += event.motion.xrel;
angle[1] += event.motion.yrel;
render.setCameraAngle(angle);
}
void setPosition(float[3] pos) {
this.pos = pos;
}
bool update(float delta) {
if(moveUp ^ moveDown) {
pos[1] += (moveUp ? delta : -delta) * moveSpeed;
}
if(moveLeft ^ moveRight) {
pos[0] -= (moveLeft ? delta : -delta) * moveSpeed * cos(angle[0] * PI / 180.0f);
pos[2] += (moveLeft ? delta : -delta) * moveSpeed * sin(angle[0] * PI / 180.0f);
}
if(moveForward ^ moveBackward) {
pos[0] += (moveForward ? delta : -delta) * moveSpeed * sin(angle[0] * PI / 180.0f);
pos[2] += (moveForward ? delta : -delta) * moveSpeed * cos(angle[0] * PI / 180.0f);
}
if((moveUp ^ moveDown) || (moveLeft ^ moveRight) || (moveForward ^ moveBackward)) {
render.setCameraPosition(pos);
}
return true;
}
float[3] getPosition() {
return pos;
}
}