From: = <=> Date: Mon, 21 Oct 2024 23:29:27 +0000 (-0700) Subject: Updates to make the turrets work right and rotate X-Git-Url: https://isoptera.lcsc.edu/gitweb/?a=commitdiff_plain;h=b6c8a189831cd0618d21b9a32cf91509d74cccde;p=example_engine%2F.git Updates to make the turrets work right and rotate --- diff --git a/Makefile b/Makefile index 8b95cc6..a962bbf 100644 --- a/Makefile +++ b/Makefile @@ -10,7 +10,7 @@ all: ${OBJS} ${CXX} ${OBJS} ${LDFLAGS} clean: - rm a.out ${OBJS} + rm -f a.out ${OBJS} test: all ./a.out diff --git a/activation_area.h b/activation_area.h index 1ba4414..d969abd 100644 --- a/activation_area.h +++ b/activation_area.h @@ -41,10 +41,9 @@ class activation_area : public gameobject { callbacks[i](); types[i] = 101; } - return i; } } - return -1; + return -1; // Don't block movement } void move(){ for(size_t i = 0; i < types.size(); i++){ diff --git a/baseclass.cpp b/baseclass.cpp index dd43f6d..67a3973 100644 --- a/baseclass.cpp +++ b/baseclass.cpp @@ -30,8 +30,7 @@ int loaded_object::init() { return 0; } -void loaded_object::draw(glm::mat4 vp) { - glUseProgram(program); +std::vector loaded_object::create_models(glm::mat4 vp){ std::vector models; data_mutex.lock(); models.reserve(locations.size()); @@ -41,6 +40,12 @@ void loaded_object::draw(glm::mat4 vp) { models.push_back(new_model); } data_mutex.unlock(); + return models; +} + +void loaded_object::draw(glm::mat4 vp) { + glUseProgram(program); + std::vector models = create_models(vp); glBindBuffer(GL_SHADER_STORAGE_BUFFER, models_buffer); glBufferData(GL_SHADER_STORAGE_BUFFER, models.size() * sizeof(glm::mat4), models.data(), GL_STATIC_DRAW); glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 0, models_buffer); diff --git a/game.h b/game.h index 1deac74..05c3e36 100644 --- a/game.h +++ b/game.h @@ -67,6 +67,7 @@ public: loaded_object(const char* of, const char* tf, glm::vec3 s) : objectfile(of), texturefile(tf) { size = s; } int init() override ; + virtual std::vector create_models(glm::mat4 vp); void draw(glm::mat4 vp) override; }; diff --git a/helpers.cpp b/helpers.cpp index 3a0595c..36c5882 100644 --- a/helpers.cpp +++ b/helpers.cpp @@ -27,7 +27,7 @@ void free_helpers() { float randvel(float speed){ long min = -100; long max = 100; - return speed * (min + random() % (max + 1 - min)); + return speed * (min + rand() % (max + 1 - min)); } GLuint make_shader(const char* filename, GLenum shaderType) { diff --git a/main.cpp b/main.cpp index 3a37ea0..e95fa3d 100644 --- a/main.cpp +++ b/main.cpp @@ -76,7 +76,7 @@ void key_callback(GLFWwindow* window, int key, int scancode, int action, int mod if(GLFW_KEY_D == key) player_key_status.right = action; if(GLFW_KEY_SPACE == key && 1 == action){ - if(player_platform){ + if(player_platform || player_position.y < 0.1f + player_height){ player_fall_speed = 0.04f; player_position.y += 1.0f; player_platform = 0; @@ -331,13 +331,15 @@ int main(int argc, char** argv) { activation_area target_spawning; target_spawning.size = glm::vec3(10, 10, 10); target_spawning.add_area(glm::vec3(10, 0, 10), bob, 1); - target_spawning.add_area(glm::vec3(90, 5, 300), activate_turret, 2); + target_spawning.add_area(glm::vec3(100, 5, 300), activate_turret, 2); objects.push_back(&target_spawning); + /* wall_block monster_box("cube.obj", "monster1.png", glm::vec3(10, 10, 10)); monster_box.scale = 5.0; monster_box.locations.push_back(glm::vec3(90, -5, 300 )); objects.push_back(&monster_box); + */ // What if it wasn't a cube? wall_block bwb("cube.obj", "brick.jpg", glm::vec3(20, 20, 20)); @@ -346,7 +348,8 @@ 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, 0, 300)); + t.locations.push_back(glm::vec3(100, -5, 300)); + t.locations.push_back(glm::vec3(-100, -5, 300)); t.current_target = &targets; t.current_projectile = &ice_balls; objects.push_back(&t); diff --git a/projectiles.h b/projectiles.h index 045c51c..f838cb6 100644 --- a/projectiles.h +++ b/projectiles.h @@ -60,6 +60,7 @@ public: } void hit_index(size_t index) override { directions[index] = glm::vec3(0, 0, 0); + lifetimes[index] = 0.0f; // Delegate removal to move() } }; diff --git a/turret.h b/turret.h index 772d45b..53e7c42 100644 --- a/turret.h +++ b/turret.h @@ -7,28 +7,55 @@ public: gameobject *current_target; // Might be nice if this was a list projectile *current_projectile; int countdown = 0; + float rotation = 0; bool active = false; - - turret() : loaded_object("turret.obj", "turret.png", glm::vec3(1, 1, 1)) {} + + turret() : loaded_object("cube.obj", "monster1.png", glm::vec3(5, 5, 5)) { + scale = 5; + } void move() { -/* 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?) - */ + /* 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?) + */ if(!active) return; if(countdown > 0){ countdown--; return; } - countdown = 300; + countdown = 314; if(!current_target->locations.size()) return; glm::vec3 target_location = current_target->locations[target_idx]; - current_projectile->add_projectile(locations[0], glm::normalize(target_location - locations[0]), 10000); - target_idx++; - if(target_idx >= current_target->locations.size()) - target_idx = 0; + for(auto l : locations){ + current_projectile->add_projectile(l, 0.2f * glm::normalize(target_location - l), 100000); + target_idx++; + if(target_idx >= current_target->locations.size()) + target_idx = 0; + } + } + + void animate(){ + if(active) + rotation += 0.01; + } + + std::vector create_models(glm::mat4 vp) override { + std::vector models; + data_mutex.lock(); + models.reserve(locations.size()); + for(glm::vec3 l : locations){ + printf("create_models loop in turret\n"); + glm::mat4 new_model = glm::mat4(1.0f); + new_model = translate(new_model, l); + new_model = rotate(new_model, rotation, glm::vec3(0, 1, 0)); + models.push_back(new_model); + } + data_mutex.unlock(); + return models; } }; + +