#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<void (*)()> callbacks;
+ std::vector<char> 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;
+ }
+ }
+ }
+ }
};
* Fall 2024
* For CS392: Game Engine Design
*/
-
+#define MAIN
#include "game.h"
#include "activation_area.h"
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<gameobject*> objects;
projectile ice_balls;
}
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<std::chrono::microseconds>(start_time - last_call).count();
+
// grand_mutex.lock();
if(player_platform){
glm::vec3 pltloc = player_platform->locations[player_platform_index];
}
// grand_mutex.unlock();
for(gameobject* o : objects)
- o->move();
- auto end = std::chrono::system_clock::now();
- // double difference = std::chrono::duration_cast<std::chrono::milliseconds>(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<std::chrono::microseconds>(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));
}
}
}
};
+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());
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?
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;
#pragma once
-#include "game.h"
#include "projectiles.h"
class turret : public loaded_object {
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)) {}
* 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;