From: Brendan Hansen Date: Sun, 30 Jan 2022 00:40:03 +0000 (-0600) Subject: code cleanup; multiple players X-Git-Url: https://git.brendanfh.com/?a=commitdiff_plain;h=4720f9c4b9be4bbc2595d9f9535978883fe1b0a1;p=bar-game.git code cleanup; multiple players --- diff --git a/docs/todo.txt b/docs/todo.txt new file mode 100644 index 0000000..4c7bb0e --- /dev/null +++ b/docs/todo.txt @@ -0,0 +1,20 @@ +Things that look like comments in this file are likely comments +in the actual codebase, at or near where the feature will be +implemented. + + +// Highlight the object that you are going to interact with + +Start the actual gameplay system: + - "Orders" arrive with requirements + - You visit stations to get the ingredients + - You build the order + - You give it to the customer + +Automated asset loader for: + - Textures + - Fonts + - Sounds + - Shaders? + + diff --git a/src/build.onyx b/src/build.onyx index 9ef2e81..990b1f5 100644 --- a/src/build.onyx +++ b/src/build.onyx @@ -4,7 +4,7 @@ package runtime.vars MAJOR_VERSION :: 0 MINOR_VERSION :: 1 -DEBUG :: true +DEBUG :: false #load_path "/home/brendan/dev/onyx" diff --git a/src/entity/player.onyx b/src/entity/player.onyx index b42f0c5..0ad5280 100644 --- a/src/entity/player.onyx +++ b/src/entity/player.onyx @@ -3,26 +3,45 @@ use package core use package glfw3 Player_Controls :: struct { - up := GLFW_KEY_W; - down := GLFW_KEY_S; - left := GLFW_KEY_A; - right := GLFW_KEY_D; - interact := GLFW_KEY_F; - pick_up := GLFW_KEY_G; + up : i32; + down : i32; + left : i32; + right : i32; + interact : i32; + pick_up : i32; +} + +player_1_controls :: Player_Controls.{ + up = GLFW_KEY_W, + down = GLFW_KEY_S, + left = GLFW_KEY_A, + right = GLFW_KEY_D, + interact = GLFW_KEY_F, + pick_up = GLFW_KEY_G, +} + +player_2_controls :: Player_Controls.{ + up = GLFW_KEY_UP, + down = GLFW_KEY_DOWN, + left = GLFW_KEY_LEFT, + right = GLFW_KEY_RIGHT, + interact = GLFW_KEY_COMMA, + pick_up = GLFW_KEY_PERIOD, } Player :: struct { use entity: Entity; holding: Entity_ID; - controls: Player_Controls; + color := Color.{1,1,1}; Size :: 16.0f init_data :: struct { pos := Vector2.{400, 200}; - controls := Player_Controls.{}; + controls: Player_Controls = player_1_controls; + color := Color.{1,1,1}; } init :: (use this: ^Player, data: init_data) { @@ -30,6 +49,9 @@ Player :: struct { this.holding = Entity_Nothing; this.controls = data.controls; + this.color = data.color; + + this.flags |= .Solid; } get_rect :: (use this: ^Player) => Rect.{ pos.x - Size, pos.y - Size, Size * 2, Size * 2 }; @@ -49,13 +71,19 @@ Player :: struct { try_move(this, .{delta.x, 0}, walls); try_move(this, .{0, delta.y}, walls); + // Highlight the object that you are going to interact with + // // Try to pick up nearby objects // if is_key_just_up(controls.pick_up) { if holding != Entity_Nothing { holding_object := entity_manager->get(holding); - holding_object.pos = pos + .{20, 0}; + vtable := ^entity_manager.entity_types[holding_object.type]; + if vtable.get_rect != null_proc { + r := vtable.get_rect(holding_object); + holding_object.pos = pos + .{Size + r.w / 2 + 4, 0}; + } holding = Entity_Nothing; } else { @@ -64,8 +92,14 @@ Player :: struct { defer memory.free_slice(^objects); // This should first sort by the distance to the object - target_object := objects[0]; - this.holding = target_object.id; + while i := 0; i < objects.count { + defer i += 1; + if objects[i] == this do continue; + + target_object := objects[i]; + this.holding = target_object.id; + break; + } // Should this fire an event on the target object? } @@ -105,7 +139,8 @@ Player :: struct { ent_rect := get_rect(this); for obsticles { - // Don't check collision on the object that you are carrying because it probably won't make sense. + // Don't check collision on the object that you are carrying or yourself because it probably won't make sense. + if it == this do continue; if it.id == this.holding do continue; vtable := ^entity_manager.entity_types[it.type]; @@ -117,7 +152,7 @@ Player :: struct { } draw :: (use this: ^Player) { - immediate_set_color(.{1,1,1}); + immediate_set_color(color); rect := this->get_rect(); immediate_image(^player_texture, rect.x, rect.y, rect.w, rect.h); diff --git a/src/gfx/immediate.onyx b/src/gfx/immediate.onyx index 9221bf4..65f6ce1 100644 --- a/src/gfx/immediate.onyx +++ b/src/gfx/immediate.onyx @@ -21,13 +21,15 @@ immediate_flush :: () { if vertex_count == 0 do return; shader_use(imgui_shader); + shader_set_uniform(imgui_shader, #cstr "u_texture_enabled", 1.0f if rendering_type == .Image else 0.0f); + immediate_mesh.vertex_count = vertex_count; mesh_update_verticies(immediate_mesh, vertex_data); mesh_draw(immediate_mesh); - shader_set_uniform(imgui_shader, #cstr "u_texture_enabled", 0.0f); vertex_count = 0; + rendering_type = .Plain; } immediate_clear :: (color: Color) { @@ -41,12 +43,14 @@ immediate_set_color :: (color: Color) { immediate_vertex :: (x, y: f32, t_x := 0.0f, t_y := 0.0f) { if vertex_count >= Maximum_Vertex_Count do immediate_flush(); + set_rendering_type(.Plain); vertex_data[vertex_count] = .{ .{x, y}, .{t_x, t_y}, immediate_color }; } immediate_triangle :: (x1, x2, x3: Vector2) { if vertex_count + 3 > Maximum_Vertex_Count do immediate_flush(); + set_rendering_type(.Plain); vertex_data[vertex_count + 0] = .{ x1, .{0,0}, immediate_color }; vertex_data[vertex_count + 1] = .{ x2, .{0,0}, immediate_color }; @@ -56,6 +60,7 @@ immediate_triangle :: (x1, x2, x3: Vector2) { immediate_rectangle :: (x, y, w, h: f32) { if vertex_count + 6 > Maximum_Vertex_Count do immediate_flush(); + set_rendering_type(.Plain); vertex_data[vertex_count + 0] = .{ .{x, y}, .{0,0}, immediate_color }; vertex_data[vertex_count + 1] = .{ .{x+w, y}, .{0,0}, immediate_color }; @@ -69,9 +74,9 @@ immediate_rectangle :: (x, y, w, h: f32) { immediate_image :: (image: ^Texture, x, y, w, h: f32) { if vertex_count > 0 do immediate_flush(); + set_rendering_type(.Image); texture_use(image); shader_use(imgui_shader); - shader_set_uniform(imgui_shader, #cstr "u_texture_enabled", 1.0f); shader_set_uniform(imgui_shader, #cstr "u_texture", 0); vertex_data[vertex_count + 0] = .{ .{x, y}, .{0,0}, immediate_color }; @@ -81,8 +86,6 @@ immediate_image :: (image: ^Texture, x, y, w, h: f32) { vertex_data[vertex_count + 4] = .{ .{x+w, y+h}, .{1,1}, immediate_color }; vertex_data[vertex_count + 5] = .{ .{x, y+h}, .{0,1}, immediate_color }; vertex_count += 6; - - immediate_flush(); } immediate_ellipse :: () {} @@ -109,4 +112,17 @@ Immediate_Vertex :: struct { immediate_color: Color; immediate_mesh: ^Mesh(Immediate_Vertex); + + Rendering_Type :: enum { + Plain; + Image; + } + rendering_type := Rendering_Type.Plain; + + set_rendering_type :: (new_type: typeof rendering_type) { + if rendering_type != new_type { + immediate_flush(); + rendering_type = new_type; + } + } } diff --git a/src/main.onyx b/src/main.onyx index 37efd8e..b91e144 100644 --- a/src/main.onyx +++ b/src/main.onyx @@ -41,14 +41,13 @@ init :: () { #if DEBUG { debug_font = font_lookup(.{"./assets/fonts/calibri.ttf", 16}); } entity_manager = entity_manager_create(); - player := entity_manager->make(Player, .{ - pos = .{300, 300} - }); + entity_manager->make(Player, .{ pos = .{300, 300}, controls=player_1_controls }); + entity_manager->make(Player, .{ pos = .{400, 300}, controls=player_2_controls, color=.{1,0,0} }); entity_manager->make(Wall, .{ .{100, 100}, .{400, 50} }); - entity_manager->make(Wall, .{ .{100, 100}, .{50, 400} }); - entity_manager->make(Wall, .{ .{450, 100}, .{50, 400} }); - entity_manager->make(Wall, .{ .{100, 450}, .{400, 50} }); + // entity_manager->make(Wall, .{ .{100, 100}, .{50, 400} }); + // entity_manager->make(Wall, .{ .{450, 100}, .{50, 400} }); + // entity_manager->make(Wall, .{ .{100, 450}, .{400, 50} }); entity_manager->make(Door, .{ .{150, 150}, .{50, 50} }); entity_manager->make(Door, .{ .{400, 150}, .{50, 50} });