code cleanup; multiple players
authorBrendan Hansen <brendan.f.hansen@gmail.com>
Sun, 30 Jan 2022 00:40:03 +0000 (18:40 -0600)
committerBrendan Hansen <brendan.f.hansen@gmail.com>
Sun, 30 Jan 2022 00:40:03 +0000 (18:40 -0600)
docs/todo.txt [new file with mode: 0644]
src/build.onyx
src/entity/player.onyx
src/gfx/immediate.onyx
src/main.onyx

diff --git a/docs/todo.txt b/docs/todo.txt
new file mode 100644 (file)
index 0000000..4c7bb0e
--- /dev/null
@@ -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?
+
+
index 9ef2e81f62e60824dac155eba158a0c74e533401..990b1f5387006f24c2276e105ca61f2520c5df3a 100644 (file)
@@ -4,7 +4,7 @@ package runtime.vars
 MAJOR_VERSION :: 0
 MINOR_VERSION :: 1
 
-DEBUG :: true
+DEBUG :: false
 
 
 #load_path "/home/brendan/dev/onyx"
index b42f0c59845d07a34819d35827325534560d986f..0ad52809583417745bc39c6cbd9b7f033760429b 100644 (file)
@@ -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);
index 9221bf473c126781d1de69c85605cb175c38671a..65f6ce157b9f790fc3aa2903abea46712439ff74 100644 (file)
@@ -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;
+        }
+    }
 }
index 37efd8ed8c5f5a557fec2172e47317d39ed958cb..b91e144882d8c36036432aa0dca4f9bf9baf174b 100644 (file)
@@ -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} });