]> isoptera.lcsc.edu Git - example_engine/.git/commitdiff
Added vehicle that doesn't work
authorSeth Long <sslong@lcsc.edu>
Wed, 20 Nov 2024 21:14:50 +0000 (13:14 -0800)
committerSeth Long <sslong@lcsc.edu>
Wed, 20 Nov 2024 21:14:50 +0000 (13:14 -0800)
Makefile
game.h
geometric_objects.h
main.cpp
turret.h
vehicle.cpp [new file with mode: 0644]
vehicle.h [new file with mode: 0644]

index 524641bdd6c495c30581ab8255f2a32d6c733126..66a3e1de69fbb80d9e88f13df00128d116952430 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 door.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 vehicle.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 door.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 vehicle.h
diff --git a/game.h b/game.h
index 597defc331e98c5af4cad13433eb70a9dedf16f3..f819ebdcc38713b1725df9c83e1342d3991cb76a 100644 (file)
--- a/game.h
+++ b/game.h
@@ -61,6 +61,13 @@ class gameobject {
                virtual void activate(size_t index) {}
 };
 
+/*
+class vehicle : virtual public gameobject {
+public:
+       virtual void vehicle_move() {};
+};
+*/
+
 class loaded_object : virtual  public gameobject {
 public:
                const char *objectfile, *texturefile;
@@ -91,6 +98,11 @@ public:
                }
 };
 
+struct key_status {
+       int forward, backward, left, right;
+};
+
+
 /* Global variables that are really all the way global */
 #ifdef MAIN
 #define EXTERN
@@ -100,6 +112,7 @@ public:
 #define INIT(x)
 #endif
 
+
 /* Player globals */
 EXTERN glm::vec3 player_position;
 EXTERN float player_heading;
@@ -108,4 +121,11 @@ EXTERN float player_elevation;
 EXTERN float player_fall_speed INIT(0);
 EXTERN gameobject *player_platform INIT(0);
 EXTERN size_t player_platform_index INIT(0);
+EXTERN struct key_status player_key_status;
+
+
+#include "vehicle.h"
+EXTERN flying_carpet *current_vehicle INIT(0);
+EXTERN size_t vehicle_index INIT(0);
+
 
index 6f729cc405884f0139834642fdd2f117e39b9392..107ec5d5a6378d6c201098f7eb5aca309c93337b 100644 (file)
@@ -3,7 +3,7 @@
 /* TODO:
  *     Collision detection (can't move through panel)
  */
-class flat_panel : public gameobject {
+class flat_panel : virtual public gameobject {
 public:
        std::vector<glm::vec2> object_scales, texture_scales;
        std::vector<glm::vec4> normals, ups;
index 51e0a525494d2e011dc74e1bed69d435bf2ac68d..c380e0adc5e619cce619cb1797497a20b125d254 100644 (file)
--- a/main.cpp
+++ b/main.cpp
@@ -53,11 +53,6 @@ ssize_t is_empty(glm::vec3 position, float distance = 0.2f){
 
 }
 
-struct key_status {
-       int forward, backward, left, right;
-};
-struct key_status player_key_status;
-
 void fire(bool burst = false){
        if(burst)
                ice_balls.add_projectile(player_position, player_heading, player_elevation, 0.4f, 10000.0f, 1.0f, true);
@@ -110,7 +105,7 @@ void key_callback(GLFWwindow* window, int key, int scancode, int action, int mod
                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);
+                       ssize_t collide_index = o->collision_index(look_at_point, 20.0f);
                        if(collide_index != -1)
                                o->activate(collide_index);
                }
@@ -134,62 +129,67 @@ void player_movement(){
        while(!shutdown_engine){
                //              grand_mutex.lock();
                auto start = std::chrono::system_clock::now();
-               glm::vec3 step_to_point = player_position;
-               if(player_key_status.forward){
-                       step_to_point += 0.1f * glm::vec3(sinf(player_heading), 0, cosf(player_heading));
-               }
-               if(player_key_status.backward){
-                       step_to_point += 0.1f * glm::vec3(-sinf(player_heading), 0, -cosf(player_heading));
-               }
-               if(player_key_status.left){
-                       step_to_point += 0.05f * glm::vec3(sinf(player_heading + M_PI/2), 0, cosf(player_heading + M_PI/2));
-               }
-               if(player_key_status.right){
-                       step_to_point += 0.05f * glm::vec3(-sinf(player_heading + M_PI/2), 0, -cosf(player_heading + M_PI/2));
-               }
-               for(gameobject* o : objects) {
-                       ssize_t collide_index = o->collision_index(step_to_point, 0.2f);
-                       if(collide_index != -1) {
-                               if(is_empty(glm::vec3(player_position.x, step_to_point.y, step_to_point.z), 0.2f)) {
-                                       step_to_point.x = player_position.x;
-                                       break;
-                               }
-                               else if(is_empty(glm::vec3(step_to_point.x, step_to_point.y, player_position.z), 0.2f)) {
-                                       step_to_point.z = player_position.z;
-                                       break;
-                               }
-                               else {
-                                       step_to_point = player_position;
-                                       break;
-                               }
+               if(current_vehicle){
+                       current_vehicle->vehicle_move();
 
+               } else {
+                       glm::vec3 step_to_point = player_position;
+                       if(player_key_status.forward){
+                               step_to_point += 0.1f * glm::vec3(sinf(player_heading), 0, cosf(player_heading));
+                       }
+                       if(player_key_status.backward){
+                               step_to_point += 0.1f * glm::vec3(-sinf(player_heading), 0, -cosf(player_heading));
+                       }
+                       if(player_key_status.left){
+                               step_to_point += 0.05f * glm::vec3(sinf(player_heading + M_PI/2), 0, cosf(player_heading + M_PI/2));
+                       }
+                       if(player_key_status.right){
+                               step_to_point += 0.05f * glm::vec3(-sinf(player_heading + M_PI/2), 0, -cosf(player_heading + M_PI/2));
+                       }
+                       for(gameobject* o : objects) {
+                               ssize_t collide_index = o->collision_index(step_to_point, 0.2f);
+                               if(collide_index != -1) {
+                                       if(is_empty(glm::vec3(player_position.x, step_to_point.y, step_to_point.z), 0.2f)) {
+                                               step_to_point.x = player_position.x;
+                                               break;
+                                       }
+                                       else if(is_empty(glm::vec3(step_to_point.x, step_to_point.y, player_position.z), 0.2f)) {
+                                               step_to_point.z = player_position.z;
+                                               break;
+                                       }
+                                       else {
+                                               step_to_point = player_position;
+                                               break;
+                                       }
 
+
+                               }
                        }
-               }
-               player_position = step_to_point;
+                       player_position = step_to_point;
 
-               if(player_platform){
-                       if(!player_platform->is_on_idx(player_position, player_platform_index, player_height))
-                               player_platform = 0;
-               } else {
-                       float floor_height = 0;
-                       for(gameobject* o : objects) {
-                               ssize_t ppi = o->is_on(player_position, player_height);
-                               if(ppi != -1) {
-                                       player_platform_index = ppi;
-                                       player_platform = o;    
-                                       floor_height = player_platform->locations[player_platform_index].y + (player_platform->size.y / 2);
+                       if(player_platform){
+                               if(!player_platform->is_on_idx(player_position, player_platform_index, player_height))
+                                       player_platform = 0;
+                       } else {
+                               float floor_height = 0;
+                               for(gameobject* o : objects) {
+                                       ssize_t ppi = o->is_on(player_position, player_height);
+                                       if(ppi != -1) {
+                                               player_platform_index = ppi;
+                                               player_platform = o;    
+                                               floor_height = player_platform->locations[player_platform_index].y + (player_platform->size.y / 2);
+                                               player_fall_speed = 0;
+                                               player_position.y = floor_height + player_height; 
+                                       }
+                               }
+                               if(player_position.y - player_height > floor_height) {
+                                       player_position.y += player_fall_speed;
+                                       player_fall_speed -= GRAVITY;
+                               } else {
                                        player_fall_speed = 0;
                                        player_position.y = floor_height + player_height; 
                                }
                        }
-                       if(player_position.y - player_height > floor_height) {
-                               player_position.y += player_fall_speed;
-                               player_fall_speed -= GRAVITY;
-                       } else {
-                               player_fall_speed = 0;
-                               player_position.y = floor_height + player_height; 
-                       }
                }
                main_hud.lprintf(2, "Player Position:  (%f, %f, %f)", player_position.x, player_position.y, player_position.z);
                //              grand_mutex.unlock();
@@ -381,10 +381,10 @@ int main(int argc, char** argv) {
        bwb.locations.push_back(glm::vec3(-30, 0, 0));
        objects.push_back(&bwb);
 
-       t.locations.push_back(glm::vec3(100, -5, 200));
-       t.locations.push_back(glm::vec3(-100, -5, 200));
-       t.locations.push_back(glm::vec3(100, -5, 100));
-       t.locations.push_back(glm::vec3(50, -5, 100));
+       t.add(glm::vec3(100, -5, 200));
+       t.add(glm::vec3(-100, -5, 200));
+       t.add(glm::vec3(100, -5, 100));
+       t.add(glm::vec3(50, -5, 100));
        t.current_target = &targets;
        t.current_projectile = &ice_balls;
        objects.push_back(&t);
@@ -427,6 +427,10 @@ int main(int argc, char** argv) {
 
        objects.push_back(&spawned_blocks);
 
+       flying_carpet fc;
+       fc.locations.push_back(glm::vec3(30, -5, 450));
+       objects.push_back(&fc);
+
        /* Initialize game objects */
        for(gameobject* o : objects){
                if(o->init()){
@@ -434,7 +438,7 @@ int main(int argc, char** argv) {
                        return 1;
                }
        }
-       
+
        main_hud.set_text("Hello World\nThis is line two\nAnd line three!");
 
        /* Start Other Threads */
@@ -443,14 +447,14 @@ int main(int argc, char** argv) {
        std::thread animation_thread(animation);
        std::thread collision_detection_thread(collision_detection);
        std::thread display_fps([](){
-               while(!shutdown_engine){
+                       while(!shutdown_engine){
                        double loop_time = 1000.0 / loopcount;
                        main_hud.lprintf(1, "FPS: %4d  Move: %lf", framecount, loop_time);
                        framecount = 0;
                        loopcount = 0;
                        std::this_thread::sleep_for(std::chrono::seconds(1));
-               }
-       });
+                       }
+                       });
 
 
        glEnable(GL_DEPTH_TEST);
index fd2b83142db4038237e2c7ad5e3bf1d09a6c19bb..13eb74120a1f377053e33e9b784e2ab7912073b0 100644 (file)
--- a/turret.h
+++ b/turret.h
@@ -1,7 +1,7 @@
 #pragma once
 #include "projectiles.h"
 
-class turret : public loaded_object {
+class turret : public loaded_object, public block_object {
 public:
        size_t target_idx = 0;
        gameobject *current_target; // Might be nice if this was a list
@@ -9,16 +9,26 @@ public:
        int countdown = 0;
        float rotation = 0;
        bool active = false;
+       std::vector<float> raise_to_points;
 
        turret() : loaded_object("cube.obj", "monster1.png", glm::vec3(5, 5, 5)) {
                scale = 5;
        }
+       
+       void add(glm::vec3 location){
+               locations.push_back(location);
+               raise_to_points.push_back(location.y);
+       }
 
        void move(int elapsed_time) { // TODO:  Incorporate elapsed_time
                /* At some rate (every so many calls to this function)
                 * Pick a target  (How do we know what targets we have?)
                 * Launch a projectile at it (How do we launch a projectile from here?)
                 */
+               for(size_t i = 0; i < locations.size(); i++){
+                       if(locations[i].y < raise_to_points[i])
+                               locations[i].y += 0.01; // Would be a good idea to use elapsed_time
+               }
                if(!active)
                        return;
                if(countdown > 0){
@@ -55,6 +65,11 @@ public:
                data_mutex.unlock();
                return models;
        }
+       
+       void activate(size_t index) override {
+               raise_to_points[index] += 30.0;
+       }       
+
 };
 
 
diff --git a/vehicle.cpp b/vehicle.cpp
new file mode 100644 (file)
index 0000000..bdf19ce
--- /dev/null
@@ -0,0 +1,24 @@
+#include "game.h"
+
+void flying_carpet::vehicle_move() {
+       glm::vec3 step_to_point = player_position;
+       if(player_key_status.forward){
+               step_to_point += 0.5f * glm::vec3(sinf(player_heading), 0, cosf(player_heading));
+       }
+       if(player_key_status.backward){
+               step_to_point += 0.5f * glm::vec3(-sinf(player_heading), 0, -cosf(player_heading));
+       }
+       if(player_key_status.left){
+               step_to_point += 0.5f * glm::vec3(sinf(player_heading + M_PI/2), 0, cosf(player_heading + M_PI/2));
+       }
+       if(player_key_status.right){
+               step_to_point += 0.5f * glm::vec3(-sinf(player_heading + M_PI/2), 0, -cosf(player_heading + M_PI/2));
+       }
+       locations[vehicle_index] = step_to_point;
+       player_position = step_to_point;
+}
+
+void flying_carpet::activate(size_t index) {
+       current_vehicle = this;
+       vehicle_index = index;
+}      
diff --git a/vehicle.h b/vehicle.h
new file mode 100644 (file)
index 0000000..185b53f
--- /dev/null
+++ b/vehicle.h
@@ -0,0 +1,18 @@
+#pragma once
+
+#include "geometric_objects.h"
+
+class flying_carpet : virtual public flat_panel {
+public:
+       flying_carpet() : flat_panel("brick.jpg") {
+               size = glm::vec3(10, 1, 10);
+       }
+
+       void draw(glm::mat4 vp) override {
+               flat_panel::draw(vp);
+       }
+
+       void vehicle_move() ;
+
+       void activate(size_t index) override ;
+};