From: Seth Long Date: Thu, 17 Oct 2024 18:02:54 +0000 (-0700) Subject: Taught class and updated a bunch of stuff X-Git-Url: https://isoptera.lcsc.edu/gitweb/?a=commitdiff_plain;h=82b5bed763ca5c0629f37eb751a6fc6ab2ceeb30;p=example_engine%2F.git Taught class and updated a bunch of stuff --- diff --git a/activation_area.h b/activation_area.h index d059527..1ba4414 100644 --- a/activation_area.h +++ b/activation_area.h @@ -1,26 +1,60 @@ #pragma once +/* + * Type 0: Call whenever the player is in the area + * Type 1: Call once on entry + * Type 2: Call on entry and exit + * + * Internal types: + * Type 100: Inactive + * Type 101: Call on exit + */ + class activation_area : public gameobject { public: std::vector callbacks; + std::vector types; activation_area() { collision_check = false; } - void add_area(glm::vec3 location, void (*callback_function)()){ + void add_area(glm::vec3 location, void (*callback_function)(), char type = 1){ locations.push_back(location); callbacks.push_back(callback_function); + types.push_back(type); + } + bool collision_with_index(glm::vec3 position, size_t index, float distance){ + glm::vec3 l = locations[index]; // This'll get optimized out + return (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)); + } - ssize_t collision_index(glm::vec3 position, float distance = 0){ + ssize_t 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( 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)){ - callbacks[i](); + if(collision_with_index(position, i, distance)){ + if(types[i] == 0) + callbacks[i](); + if(types[i] == 1){ + callbacks[i](); + types[i] = 100; + } if(types[i] == 2){ + callbacks[i](); + types[i] = 101; + } return i; } } return -1; } + void move(){ + for(size_t i = 0; i < types.size(); i++){ + if(types[i] == 101){ + if(!collision_with_index(player_position, i, 0.2f)){ // Note: Included collision check as a magic number + callbacks[i](); + types[i] = 2; + } + } + } + } }; diff --git a/game.h b/game.h index 4d2d66c..1deac74 100644 --- a/game.h +++ b/game.h @@ -1,6 +1,4 @@ - -#ifndef GAME_H -#define GAME_H +#pragma once #include #include @@ -91,7 +89,6 @@ public: }; /* Global variables that are really all the way global */ -/* #ifdef MAIN #define EXTERN #define INIT(X) = X @@ -100,7 +97,12 @@ public: #define INIT(x) #endif -EXTERN char* general_buffer; -EXTERN int framecount = INIT(0); -*/ -#endif +/* Player globals */ +EXTERN glm::vec3 player_position; +EXTERN float player_heading; +EXTERN float player_height INIT(2); +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); + diff --git a/main.cpp b/main.cpp index 8e73937..3a37ea0 100644 --- a/main.cpp +++ b/main.cpp @@ -3,7 +3,7 @@ * Fall 2024 * For CS392: Game Engine Design */ - +#define MAIN #include "game.h" #include "activation_area.h" @@ -21,15 +21,6 @@ float width = 2600; int time_resolution = 10; int framecount = 0; -/* Player globals */ -glm::vec3 player_position; -float player_heading; -float player_height = 2; -float player_elevation; -float player_fall_speed = 0; -gameobject *player_platform = 0; -size_t player_platform_index = 0; - std::vector objects; projectile ice_balls; @@ -175,8 +166,12 @@ void player_movement(){ } void object_movement(){ + int loop_time; + auto last_call = std::chrono::system_clock::now(); while(!shutdown_engine){ - auto start = std::chrono::system_clock::now(); + auto start_time = std::chrono::system_clock::now(); + loop_time = std::chrono::duration_cast(start_time - last_call).count(); + // grand_mutex.lock(); if(player_platform){ glm::vec3 pltloc = player_platform->locations[player_platform_index]; @@ -185,11 +180,14 @@ void object_movement(){ } // grand_mutex.unlock(); for(gameobject* o : objects) - o->move(); - auto end = std::chrono::system_clock::now(); - // double difference = std::chrono::duration_cast(start - end).count(); - // printf("Time difference: %lf\n", difference); - std::this_thread::sleep_for(std::chrono::microseconds(1000) - (start - end)); + o->move(); // move needs a paramter which indicates how long since we last called move + auto end_time = std::chrono::system_clock::now(); + int cpu_time = std::chrono::duration_cast(end_time - start_time).count(); + last_call = start_time; + int sleep_time = 1000 - cpu_time; + printf("Loop time: %d Sleep time: %d CPU time: %d\n", loop_time, sleep_time, cpu_time); + if(sleep_time > 100 ) + std::this_thread::sleep_for(std::chrono::microseconds(sleep_time)); } } @@ -257,6 +255,11 @@ void bob(){ } }; +turret t; +void activate_turret(){ + t.active = !t.active; +} + void debug_callback(GLenum, GLenum, GLuint, GLenum severity, GLsizei, const GLchar* message, const void*) { if(severity == GL_DEBUG_SEVERITY_HIGH) puts(RED(message).c_str()); @@ -327,12 +330,13 @@ 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); + target_spawning.add_area(glm::vec3(10, 0, 10), bob, 1); + target_spawning.add_area(glm::vec3(90, 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(-40, 0, 20)); + monster_box.locations.push_back(glm::vec3(90, -5, 300 )); objects.push_back(&monster_box); // What if it wasn't a cube? @@ -341,8 +345,7 @@ int main(int argc, char** argv) { bwb.texture_scale = glm::vec2(10, 10); bwb.locations.push_back(glm::vec3(-30, 0, 0)); objects.push_back(&bwb); - - turret t; + t.locations.push_back(glm::vec3(100, 0, 300)); t.current_target = &targets; t.current_projectile = &ice_balls; diff --git a/turret.h b/turret.h index fe22eec..772d45b 100644 --- a/turret.h +++ b/turret.h @@ -1,5 +1,4 @@ #pragma once -#include "game.h" #include "projectiles.h" class turret : public loaded_object { @@ -7,7 +6,8 @@ public: size_t target_idx = 0; gameobject *current_target; // Might be nice if this was a list projectile *current_projectile; - int countdown = 10000; + int countdown = 0; + bool active = false; turret() : loaded_object("turret.obj", "turret.png", glm::vec3(1, 1, 1)) {} @@ -16,14 +16,17 @@ public: * 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; if(!current_target->locations.size()) return; glm::vec3 target_location = current_target->locations[target_idx]; - current_projectile->add_projectile(locations[0], 0.001f * (target_location - locations[0]), 10000); + 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;