@@ -10,10 +10,17 @@ const math = delve.math;
10
10
var camera : delve.graphics.camera.Camera = undefined ;
11
11
var fallback_material : graphics.Material = undefined ;
12
12
13
+ var quake_map : delve.utils.quakemap.QuakeMap = undefined ;
13
14
var map_meshes : std .ArrayList (delve .graphics .mesh .Mesh ) = undefined ;
14
15
var entity_meshes : std .ArrayList (delve .graphics .mesh .Mesh ) = undefined ;
16
+ var cube_mesh : delve.graphics.mesh.Mesh = undefined ;
15
17
16
- var time : f64 = 0.0 ;
18
+ var map_transform : math.Mat4 = undefined ;
19
+
20
+ var bounding_box_size : math.Vec3 = math .Vec3 .new (2 , 3 , 2 );
21
+ var player_pos : math.Vec3 = math .Vec3 .zero ;
22
+ var player_vel : math.Vec3 = math .Vec3 .zero ;
23
+ var on_ground = true ;
17
24
18
25
pub fn main () ! void {
19
26
const example = delve.modules.Module {
@@ -113,9 +120,11 @@ pub fn on_init() !void {
113
120
\\}
114
121
;
115
122
123
+ map_transform = delve .math .Mat4 .scale (delve .math .Vec3 .new (0.1 , 0.1 , 0.1 )).mul (delve .math .Mat4 .rotate (-90 , delve .math .Vec3 .x_axis ));
124
+
116
125
var allocator = gpa .allocator ();
117
126
var err : delve.utils.quakemap.ErrorInfo = undefined ;
118
- const quake_map = try delve .utils .quakemap .QuakeMap .read (allocator , test_map_file , & err );
127
+ quake_map = try delve .utils .quakemap .QuakeMap .read (allocator , test_map_file , map_transform , & err );
119
128
120
129
// Create a material out of the texture
121
130
fallback_material = graphics .Material .init (.{
@@ -125,10 +134,11 @@ pub fn on_init() !void {
125
134
});
126
135
127
136
// create our camera
128
- camera = delve .graphics .camera .Camera .initThirdPerson (90.0 , 0.01 , 512 , 25 .0 , math .Vec3 .up );
129
- camera .position .y = 7 .0 ;
137
+ camera = delve .graphics .camera .Camera .initThirdPerson (90.0 , 0.01 , 512 , 16 .0 , math .Vec3 .up );
138
+ camera .position .y = 10 .0 ;
130
139
131
- const map_transform = delve .math .Mat4 .scale (delve .math .Vec3 .new (0.1 , 0.1 , 0.1 )).mul (delve .math .Mat4 .rotate (-90 , delve .math .Vec3 .x_axis ));
140
+ // set our player position too
141
+ player_pos = camera .position ;
132
142
133
143
var materials = std .StringHashMap (delve .utils .quakemap .QuakeMaterial ).init (allocator );
134
144
const shader = graphics .Shader .initDefault (.{});
@@ -162,16 +172,19 @@ pub fn on_init() !void {
162
172
.samplers = &[_ ]graphics.FilterMode {.NEAREST },
163
173
.texture_0 = tex ,
164
174
});
165
- try materials .put (mat_name_null , .{ .material = mat , .tex_size_x = @intCast (tex .width ), .tex_size_y = @intCast (tex .height )});
175
+ try materials .put (mat_name_null , .{ .material = mat , .tex_size_x = @intCast (tex .width ), .tex_size_y = @intCast (tex .height ) });
166
176
167
177
// delve.debug.log("Loaded image: {s}", .{tex_path_null});
168
178
}
169
179
}
170
180
}
171
181
172
182
// make meshes out of the quake map, one per material
173
- map_meshes = try quake_map .buildWorldMeshes (allocator , map_transform , materials , .{ .material = fallback_material });
174
- entity_meshes = try quake_map .buildEntityMeshes (allocator , map_transform , materials , .{ . material = fallback_material });
183
+ map_meshes = try quake_map .buildWorldMeshes (allocator , math .Mat4 .identity , materials , .{ .material = fallback_material });
184
+ entity_meshes = try quake_map .buildEntityMeshes (allocator , math .Mat4 .identity , materials , .{ .material = fallback_material });
185
+
186
+ // make a bounding box cube
187
+ cube_mesh = try delve .graphics .mesh .createCube (math .Vec3 .new (0 , 0 , 0 ), bounding_box_size , delve .colors .red , fallback_material );
175
188
176
189
// set a bg color
177
190
delve .platform .graphics .setClearColor (delve .colors .examples_bg_light );
@@ -183,9 +196,11 @@ pub fn on_tick(delta: f32) void {
183
196
if (delve .platform .input .isKeyJustPressed (.ESCAPE ))
184
197
std .os .exit (0 );
185
198
186
- time += delta ;
199
+ do_player_move ( delta ) ;
187
200
188
- camera .runSimpleCamera (30 * delta , 60 * delta , true );
201
+ // update camera position to new player pos
202
+ camera .position = player_pos ;
203
+ camera .runSimpleCamera (0 , 60 * delta , true );
189
204
}
190
205
191
206
pub fn on_draw () void {
@@ -198,4 +213,86 @@ pub fn on_draw() void {
198
213
for (0.. entity_meshes .items .len ) | idx | {
199
214
entity_meshes .items [idx ].draw (proj_view_matrix , model );
200
215
}
216
+
217
+ cube_mesh .draw (proj_view_matrix , math .Mat4 .translate (camera .position ));
218
+ }
219
+
220
+ pub fn do_player_move (delta : f32 ) void {
221
+ // gravity!
222
+ player_vel .y -= 0.5 * delta ;
223
+
224
+ // get our forward input direction
225
+ var move_dir : math.Vec3 = math .Vec3 .zero ;
226
+ if (delve .platform .input .isKeyPressed (.W )) {
227
+ var dir = camera .direction ;
228
+ dir .y = 0.0 ;
229
+ dir = dir .norm ();
230
+ move_dir = move_dir .sub (dir );
231
+ }
232
+ if (delve .platform .input .isKeyPressed (.S )) {
233
+ var dir = camera .direction ;
234
+ dir .y = 0.0 ;
235
+ dir = dir .norm ();
236
+ move_dir = move_dir .add (dir );
237
+ }
238
+
239
+ // get our sideways input direction
240
+ if (delve .platform .input .isKeyPressed (.D )) {
241
+ const right_dir = camera .getRightDirection ();
242
+ move_dir = move_dir .add (right_dir );
243
+ }
244
+ if (delve .platform .input .isKeyPressed (.A )) {
245
+ const right_dir = camera .getRightDirection ();
246
+ move_dir = move_dir .sub (right_dir );
247
+ }
248
+
249
+ // jumnp and fly
250
+ if (delve .platform .input .isKeyPressed (.SPACE ) and on_ground ) player_vel .y = 0.3 ;
251
+ if (delve .platform .input .isKeyPressed (.F )) player_vel .y = 0.1 ;
252
+
253
+ move_dir = move_dir .norm ();
254
+ player_vel = player_vel .add (move_dir .scale (10.0 ).scale (delta ));
255
+
256
+ // horizontal collisions
257
+ var check_bounds_x = delve .spatial .BoundingBox .init (player_pos .add (math .Vec3 .new (player_vel .x , 0 , 0 )), bounding_box_size );
258
+ var did_collide_x = false ;
259
+ for (quake_map .worldspawn .solids .items ) | solid | {
260
+ did_collide_x = solid .checkBoundingBoxCollision (check_bounds_x );
261
+ if (did_collide_x )
262
+ break ;
263
+ }
264
+ if (did_collide_x )
265
+ player_vel .x = 0.0 ;
266
+
267
+ var check_bounds_z = delve .spatial .BoundingBox .init (player_pos .add (math .Vec3 .new (player_vel .x , 0 , player_vel .z )), bounding_box_size );
268
+ var did_collide_z = false ;
269
+ for (quake_map .worldspawn .solids .items ) | solid | {
270
+ did_collide_z = solid .checkBoundingBoxCollision (check_bounds_z );
271
+ if (did_collide_z )
272
+ break ;
273
+ }
274
+ if (did_collide_z )
275
+ player_vel .z = 0.0 ;
276
+
277
+ // vertical collision
278
+ var check_bounds_y = delve .spatial .BoundingBox .init (player_pos .add (math .Vec3 .new (player_vel .x , player_vel .y , player_vel .z )), bounding_box_size );
279
+ var did_collide_y = false ;
280
+ for (quake_map .worldspawn .solids .items ) | solid | {
281
+ did_collide_y = solid .checkBoundingBoxCollision (check_bounds_y );
282
+ if (did_collide_y )
283
+ break ;
284
+ }
285
+ if (did_collide_y ) {
286
+ on_ground = player_vel .y < 0.0 ;
287
+ player_vel .y = 0.0 ;
288
+ } else {
289
+ on_ground = false ;
290
+ }
291
+
292
+ // velocity has been clipped to collisions, can move now
293
+ player_pos = player_pos .add (player_vel );
294
+
295
+ // dumb friction!
296
+ player_vel .x = 0.0 ;
297
+ player_vel .z = 0.0 ;
201
298
}
0 commit comments