-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmonster_maze.c
More file actions
204 lines (176 loc) · 5.96 KB
/
monster_maze.c
File metadata and controls
204 lines (176 loc) · 5.96 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
#include <hurricane/engine.h>
#include <hurricane/renderer/SDL.h>
#include <hurricane/shared.h>
#include <hurricane/util/list.h>
#include <hurricane/util/vec.h>
#include <hurricane/util/quat.h>
#include <hurricane/renderer/console.h>
#include <hurricane/renderer/video.h>
// #include <hurricane/renderer/xlib.h>
#include <hurricane/clock.h>
#include <hurricane/loader/obj.h>
#include <hurricane/input.h>
#include <hurricane/anim.h>
#include <hurricane/util/log.h>
#include <hurricane/clock.h>
#include "maze.c"
#include <stdbool.h>
#define M_PI 3.1415926535897535
#if 1
#define KEYW 119
#define KEYA 97
#define KEYS 115
#define KEYD 100
#define KEYSPC 32
#else
#define KEYW 25
#define KEYA 38
#define KEYS 39
#define KEYD 40
#endif
void noop(void) {}
hc_object camera;
hc_renderer renderer;
hc_clock gclock;
double tmpvec[3] = {0.02, 0.02, 0};
double tmpvecupdate[3];
char action = '\0';
hc_list blocks;
struct {
int angle; // 0..=3
int x;
int y;
struct {
int real_angle; // free
bool animating;
double progress;
hc_quaternion rotation_start;
hc_quaternion rotation_end;
double position_start[3];
double position_end[3];
} camera;
} player;
const double CAMERA_ANIM_DURATION = 0.3;
char format_tmp[100];
hc_quaternion tick;
bool *maze = 0;
int maze_w = 0;
int maze_h = 0;
void on_key_down(void *e) {
hc_input_key_event *evt = e;
if (evt->code == KEYSPC) {
hc_log("\nangle=%d x=%d y=%d\npx=%f py=%f pz=%f\nanim=%f action=%d animating=%d", player.angle, player.x, player.y, camera.position[0], camera.position[1], camera.position[2], player.camera.progress, action, player.camera.animating);
}
/*
if (action == ' ') {
dump_maze(maze, 19, 19, player.x, player.y);
}
*/
if (action == '\0') {
switch (evt->code) {
case KEYW:
action = 'w';
break;
case KEYA:
action = 'a';
break;
case KEYS:
action = 's';
break;
case KEYD:
action = 'd';
break;
default:
hc_warn("unknown key code %d", evt->code);
return;
}
}
}
void update(void) {
double delta = hc_clock_step(&gclock);
for (int i = 0; i < blocks.length; i++) {
hc_object *obj = hc_list_get(&blocks, i);
hc_render_object(&camera, obj);
}
if (!player.camera.animating) {
if (action == 'w' || action == 's') {
hc_vec3_set(player.x * 2, 0, player.y * 2, player.camera.position_start);
int old_x = player.x;
int old_y = player.y;
player.x += (action == 'w' ? 1 : -1) * ((player.angle == 1 ? 1 : 0) - (player.angle == 3 ? 1 : 0));
player.y += (action == 'w' ? 1 : -1) * ((player.angle == 0 ? 1 : 0) - (player.angle == 2 ? 1 : 0));
if (maze[player.y * maze_w + player.x]) {
player.x = old_x;
player.y = old_y;
action = '\0';
return;
}
hc_vec3_set(player.x * 2, 0, player.y * 2, player.camera.position_end);
player.camera.progress = 0;
player.camera.animating = true;
}
if (action == 'a' || action == 'd') {
hc_quaternion_from_y_rotation(player.camera.real_angle * M_PI/2, &player.camera.rotation_start);
player.camera.real_angle += (action == 'a' ? -1 : 1);
player.angle = (player.camera.real_angle % 4 + 4) % 4;
hc_quaternion_from_y_rotation(player.camera.real_angle * M_PI/2, &player.camera.rotation_end);
player.camera.progress = 0;
player.camera.animating = true;
}
} else {
player.camera.progress += delta;
if (player.camera.progress > CAMERA_ANIM_DURATION) {
player.camera.progress = CAMERA_ANIM_DURATION;
player.camera.animating = false;
}
if (action == 'a' || action == 'd') {
hc_quaternion_slerp(&player.camera.rotation_start, &player.camera.rotation_end, player.camera.progress / CAMERA_ANIM_DURATION, &camera.rotation);
}
if (action == 'w' || action == 's') {
hc_vec3_lerp(player.camera.position_start, player.camera.position_end, player.camera.progress / CAMERA_ANIM_DURATION, camera.position);
}
if (!player.camera.animating) {
action = '\0';
}
}
}
void create_maze(int width, int height) {
maze_w = width * 2 + 3;
maze_h = height * 2 + 3;
maze = malloc(maze_w * maze_h * sizeof(bool));
GenerateMaze(maze, maze_w, maze_h);
for (int y = 0; y < maze_h; y++) {
for (int x = 0; x < maze_w; x++) {
if (maze[y * maze_w + x]) { // intentional y*w instead of y*h
hc_object *cube = malloc(sizeof(hc_object));
hc_new_object(cube, &hc_geometry_cube, VEC3(x*2, 0, y*2), hc_quaternion_identity, hc_vec3_one);
hc_list_add(&blocks, cube);
}
}
}
}
int main(void) {
hc_input_subscribe(on_key_down, HC_INPUT_KEYDOWN);
hc_clock_new(&gclock);
hc_list_new(&blocks);
create_maze(8, 8);
renderer = hc_renderer_sdl;
renderer.init();
hc_set_fov(90, false);
hc_new_object(&camera, &hc_geometry_none, VEC3(2, 0, 2), hc_quaternion_identity, hc_vec3_one);
hc_quaternion_copy(&camera.rotation, &player.camera.rotation_start);
// hc_quaternion_from_euler_zyx(VEC3(M_PI/2, 0, 0), &camera.rotation);
hc_vec3_copy(camera.position, player.camera.position_start);
// hc_vec3_copy(VEC3(0, 100, 0), camera.position);
hc_quaternion_from_y_rotation(M_PI / 2, &camera.rotation);
player.angle = 1;
player.x = 1;
player.y = 1;
player.camera.animating = false;
player.camera.real_angle = 1;
hc_init(false, -1, renderer, update);
renderer.finish();
// hc_sdl_finish();
// hc_video_finish();
return 0;
}