]> isoptera.lcsc.edu Git - example_engine/.git/commitdiff
Added door
authorSeth Long <sslong@lcsc.edu>
Thu, 7 Nov 2024 18:22:39 +0000 (10:22 -0800)
committerSeth Long <sslong@lcsc.edu>
Thu, 7 Nov 2024 18:22:39 +0000 (10:22 -0800)
Makefile
cone.png [new file with mode: 0644]
door.cpp [new file with mode: 0644]
door.h [new file with mode: 0644]
door.obj [new file with mode: 0644]
game.h
main.cpp

index 7e261002072d1c39eca596df9f3c396955c61fab..524641bdd6c495c30581ab8255f2a32d6c733126 100644 (file)
--- a/Makefile
+++ b/Makefile
@@ -1,4 +1,4 @@
-OBJS = main.o stb_image.o helpers.o tiny_obj_loader.o baseclass.o old_objects.o geometric_objects.o hud.o
+OBJS = main.o stb_image.o helpers.o tiny_obj_loader.o baseclass.o old_objects.o geometric_objects.o hud.o door.o
 CXXFLAGS = -g -Wall 
 LDFLAGS = -lGL -lglfw -lGLEW -pthread -g 
 
@@ -16,4 +16,4 @@ test: all
        ./a.out
 
 ${OBJS} : game.h
-main.o : projectiles.h activation_area.h turret.h tile_floor.h old_objects.h moving_platform.h geometric_objects.h hud.h
+main.o : projectiles.h activation_area.h turret.h tile_floor.h old_objects.h moving_platform.h geometric_objects.h hud.h door.h
diff --git a/cone.png b/cone.png
new file mode 100644 (file)
index 0000000..3174c68
Binary files /dev/null and b/cone.png differ
diff --git a/door.cpp b/door.cpp
new file mode 100644 (file)
index 0000000..171e7b5
--- /dev/null
+++ b/door.cpp
@@ -0,0 +1,94 @@
+#include "game.h"
+#include "door.h"
+
+void door::add_door(glm::vec3 location){
+       locations.push_back(location);
+       states.push_back(0);
+       open_angles.push_back(0.0f);
+}
+
+ssize_t door::collision_index(glm::vec3 position, float distance){
+       for(size_t i = 0; i < locations.size(); i++){
+               glm::vec3 l = locations[i]; // This'll get optimized out
+               if(states[i] == 0){
+                       if(     size.x/2.0f + distance > abs(l.x-position.x) && 
+                                       size.y/2.0f + distance > abs(l.y-position.y) && 
+                                       size.z/2.0f + distance > abs(l.z-position.z)){
+                               return i;       
+                       }
+               } else if(states[i] == 3){
+                       if(     size.z/2.0f + distance > abs(l.x-(position.x - (size.x / 2.0f))) && 
+                                       size.y/2.0f + distance > abs(l.y-position.y) && 
+                                       size.x/2.0f + distance > abs(l.z-position.z + (size.x / 2.0f))){
+                               return i;       
+                       }
+               }
+       }
+       return -1;
+}
+
+bool door::is_on_idx(glm::vec3 position, size_t index, float height){
+       if(states[index] != 0)
+               return false;
+       return (0.0f < (position.y - locations[index].y) && 
+                       1.0f > (position.y - height) - (locations[index].y + size.y/2) &&
+                       size.x/2 > fabs(position.x - locations[index].x) && 
+                       size.z/2 > fabs(position.z - locations[index].z));
+}
+
+void door::activate(size_t index){
+       if(states[index] == 3)
+               close(index);
+       if(states[index] == 0)
+               open(index);
+}
+
+void door::open(size_t index){
+       states[index] = 2;
+}
+
+void door::close(size_t index) {
+       states[index] = 1;
+}
+
+void door::move(int elapsed_time){
+       for(size_t i = 0; i < locations.size(); i++){
+               if(states[i] == 0 || states[i] == 3) // If all the way open or closed, do nothing
+                       return;
+               if(states[i] == 1) { // closing
+                       if(open_angles[i] < 0.01f){
+                               open_angles[i] = 0.0f;
+                               states[i] = 0; // closed
+                       } else 
+                               open_angles[i] -= elapsed_time * (M_PI/2) / 2000000;
+               }
+               if(states[i] == 2) { // opening
+                       if(open_angles[i] > M_PI/2){
+                               open_angles[i] = M_PI/2;
+                               states[i] = 3; // closed
+                       } else 
+                               open_angles[i] += elapsed_time * (M_PI/2) / 2000000;
+               }
+       }
+}
+
+std::vector<glm::mat4> door::create_models() {
+       std::vector<glm::mat4> models;
+       data_mutex.lock();
+       models.reserve(locations.size());
+       for(size_t i = 0; i < locations.size(); i++){
+               auto l = locations[i];
+               glm::mat4 new_model = glm::mat4(1.0f);
+               new_model = translate(new_model, l);
+               // Rotation:  Is it open or closed?
+               // A problem:  Hinges are usually on the edge of doors!
+               // Minor caveat:  doors can be hinged on either side, and only open one way
+               new_model = glm::translate(new_model, glm::vec3(5, 0, 1));
+               new_model = glm::rotate(new_model, open_angles[i], glm::vec3(0, 1, 0));
+               new_model = glm::translate(new_model, glm::vec3(-5, 0, -1));
+               models.push_back(new_model);
+       }
+       data_mutex.unlock();
+       return models;
+}
+
diff --git a/door.h b/door.h
new file mode 100644 (file)
index 0000000..668ec71
--- /dev/null
+++ b/door.h
@@ -0,0 +1,24 @@
+#pragma once
+class door : public loaded_object, public block_object {
+       public:
+               /* States for this door:
+                *              closed: 0 
+                *              closing:  1
+                *              opening:  2
+                *              open: 3
+                */
+               std::vector<int> states;
+               std::vector<float> open_angles;
+               door(const char* of, const char* tf, glm::vec3 s) : loaded_object(of, tf, s) {
+                       collision_check = true;
+               }
+               void open(size_t index);
+               void close(size_t index);
+               void activate(size_t index) override;
+               void move(int elapsed_time) override;
+               std::vector<glm::mat4> create_models() override;
+               ssize_t collision_index(glm::vec3 position, float distance);
+               void add_door(glm::vec3 location);
+               bool is_on_idx(glm::vec3 position, size_t index, float height);
+};
+
diff --git a/door.obj b/door.obj
new file mode 100644 (file)
index 0000000..47459bf
--- /dev/null
+++ b/door.obj
@@ -0,0 +1,30 @@
+# Blender v3.0.1 OBJ File: ''
+# www.blender.org
+mtllib cube.mtl
+o Cube
+v 5.000000 10.000000 -1.000000
+v 5.000000 -10.000000 -1.000000
+v 5.000000 10.000000 1.000000
+v 5.000000 -10.000000 1.000000
+v -5.000000 10.000000 -1.000000
+v -5.000000 -10.000000 -1.000000
+v -5.000000 10.000000 1.000000
+v -5.000000 -10.000000 1.000000
+vt 0.0 1.0 
+vt 0.0 0.0 
+vt 1.0 1.0 
+vt 1.0 0.0 
+vn 0.0000 1.0000 0.0000
+vn 0.0000 0.0000 1.0000
+vn -1.0000 0.0000 0.0000
+vn 0.0000 -1.0000 0.0000
+vn 1.0000 0.0000 0.0000
+vn 0.0000 0.0000 -1.0000
+usemtl Material
+s off
+f 1/2/1 5/1/1 7/3/1 3/4/1
+f 4/2/2 3/1/2 7/3/2 8/4/2
+f 8/2/3 7/1/3 5/3/3 6/4/3
+f 6/2/4 2/1/4 4/3/4 8/4/4
+f 2/2/5 1/1/5 3/3/5 4/4/5
+f 6/2/6 5/1/6 1/3/6 2/4/6
diff --git a/game.h b/game.h
index e8476362359afc40e43b7833960841aa9aab3fb4..597defc331e98c5af4cad13433eb70a9dedf16f3 100644 (file)
--- a/game.h
+++ b/game.h
@@ -58,6 +58,7 @@ class gameobject {
                }
                virtual bool collision_with_index(glm::vec3 position, size_t index, float distance = 0) { return false;}
                virtual void hit_index(size_t index) {}
+               virtual void activate(size_t index) {}
 };
 
 class loaded_object : virtual  public gameobject {
index 26b27f17de43dcc8822f16b17c07c2352b5fbf18..51e0a525494d2e011dc74e1bed69d435bf2ac68d 100644 (file)
--- a/main.cpp
+++ b/main.cpp
@@ -13,6 +13,7 @@
 #include "turret.h"
 #include "geometric_objects.h"
 #include "hud.h"
+#include "door.h"
 
 std::mutex grand_mutex;
 
@@ -102,6 +103,18 @@ void key_callback(GLFWwindow* window, int key, int scancode, int action, int mod
        else if(GLFW_KEY_S == key && 0 == action){
                player_key_status.backward = 0;
        }
+       if(GLFW_KEY_E == key && 1 == action){
+               // Spawn a block
+               glm::vec3 look_at_point = glm::vec3(player_position.x, player_position.y, player_position.z) ;
+               look_at_point.x += 6.0f * cosf(player_elevation) * sinf(player_heading);
+               look_at_point.y += 6.0f * sinf(player_elevation);
+               look_at_point.z += 6.0f * cosf(player_elevation) * cosf(player_heading);
+               for(gameobject* o : objects) {
+                       ssize_t collide_index = o->collision_index(look_at_point, 4.0f);
+                       if(collide_index != -1)
+                               o->activate(collide_index);
+               }
+       }
        if(GLFW_KEY_A == key)
                player_key_status.left = action;
        if(GLFW_KEY_D == key)
@@ -401,6 +414,12 @@ int main(int argc, char** argv) {
        fpp.addpanel(glm::vec3(-30, 0, 500), glm::vec2(10, 10), glm::vec2(2.0, 2.0), glm::vec3(0, 0, 1), glm::vec3(0, 1, 0));
        objects.push_back(&fpp);
 
+       door d("door.obj", "cone.png", glm::vec3(10, 20, 2));
+       d.add_door(glm::vec3(0, 0, 300));
+       objects.push_back(&d);
+       bwb.locations.push_back(glm::vec3(15, 0, 293));
+       bwb.locations.push_back(glm::vec3(-15, 0, 293));
+
        aimpoint main_aimpoint;
        objects.push_back(&main_aimpoint);