From: Seth Long Date: Tue, 3 Nov 2026 18:19:52 +0000 (-0800) Subject: Added broken cat/mouse system X-Git-Url: https://isoptera.lcsc.edu/gitweb/?a=commitdiff_plain;h=b0f165637896e244763ab24b8b70bd3f4768d335;p=engine26%2F.git Added broken cat/mouse system --- diff --git a/Makefile b/Makefile index b9a6140..bafbdf9 100644 --- a/Makefile +++ b/Makefile @@ -15,4 +15,4 @@ clean: test: all ./game -${OBJS} : game.h misc_objects.h water_tank.h projectile.h explodingtarget.h +${OBJS} : game.h misc_objects.h water_tank.h projectile.h explodingtarget.h cat.h diff --git a/cat.h b/cat.h new file mode 100644 index 0000000..f7813a5 --- /dev/null +++ b/cat.h @@ -0,0 +1,43 @@ +#pragma once + + +class Cat : public FloorPanel { +public: + bool direction = true; + float switchtime; + Cat() { + switchtime = 1000; + }; + void add_location(vec3 loc){ + models.push_back(translate(mat4(1.0), loc)); + } + void movement(float time) { + for(size_t i = 0; i < models.size(); i++) { + vec3 mov = vec3(0, 0, -.01); + if(!direction) + mov = vec3(0, 0, .01); + if(switchtime < 0) { + direction = !direction; + switchtime = 1000; + } + switchtime -= time; + models[i] = translate(models[i], mov); + vec3 our_center = get_center(i); + for(auto &mouse : game_objects){ + if(mouse->name == "Mouse"){ + for(size_t mouse_inst = 0; mouse_inst < mouse->models.size(); mouse_inst++){ + vec3 mcp = mouse->closest_point(our_center, mouse_inst); + vec3 ocp = closest_point(mcp, i); + if(can_see(mcp, ocp)){ + printf("We can see a mouse!\n"); + } else { + if(switchtime > 900){ + cout << "Can't see mouse, our_center = " << our_center << " mcp = " << mcp << endl; + } + } + } + } + } + } + } +}; diff --git a/game.h b/game.h index 92ec7f4..00c61ec 100644 --- a/game.h +++ b/game.h @@ -157,10 +157,12 @@ struct vertex { }; bool operator==(const vertex& a, const vertex& o); +/* Helper functions */ ostream& operator<<(ostream& out, vec3 v); GameObject* add_object(GameObject *new_object, string name, bool solid = false); void load_level(); GameObject* find_object(string name); +bool can_see(vec3 A, vec3 B); #define PLAYER_ON_OBJECT 1 #define ACTIVATE 2 @@ -170,3 +172,4 @@ GameObject* find_object(string name); #include "water_tank.h" #include "projectile.h" #include "explodingtarget.h" +#include "cat.h" diff --git a/load_level.cpp b/load_level.cpp index 1f96701..c26283f 100644 --- a/load_level.cpp +++ b/load_level.cpp @@ -6,9 +6,9 @@ void load_level(){ FloorPanel *floor = (FloorPanel*)add_object(new FloorPanel, "The Floor", true); floor->texture_file = "textures/stones.png"; - for(float i = -90; i < 90; i+= 5) - for(float j = -90; j < 90; j+= 5) - for(float k = -10; k <= -2; k += 1) + for(float i = -45; i < 45; i+= 5) + for(float j = -45; j < 45; j+= 5) + for(float k = -2; k <= -2; k += 1) floor->add_location_size(vec3(i, k, j), vec3(5, 1, 5)); /* New Way */ @@ -50,8 +50,16 @@ void load_level(){ ExplodingTarget *targ = (ExplodingTarget*)add_object(new ExplodingTarget, "Target", true); targ->texture_file = "textures/chalet.jpg"; - targ->add_location_size(vec3(20, 4, 40), vec3(6, 6, 6)); + targ->add_location_size(vec3(20, 20, 40), vec3(6, 6, 6)); targ->add_location_size(vec3(20, 40, 40), vec3(6, 6, 6)); + + Cat *cat = (Cat*)add_object(new Cat, "Cat", true); + cat->add_location(vec3(5, 2, 40)); + cat->texture_file = "textures/cat.jpg"; + + FloorPanel *mouse = (FloorPanel*)add_object(new FloorPanel, "Mouse", true); + mouse->add_location(vec3(-5, 0, 40)); + mouse->texture_file = "textures/mouse.jpg"; } GameObject* add_object(GameObject *new_object, string name, bool solid){ diff --git a/main.cpp b/main.cpp index 3bcc6f6..7a49e94 100644 --- a/main.cpp +++ b/main.cpp @@ -50,13 +50,13 @@ void collision_check(){ vec3 moinst_center = mo->get_center(mo_inst); vec3 soinst_center = so->get_center(so_inst); if(mo->fast_check_distance(mo_inst) + so->fast_check_distance(so_inst) - < distance(moinst_center, soinst_center)) + < distance(moinst_center, soinst_center)) continue; vec3 mo_cp = mo->closest_point(soinst_center, mo_inst); vec3 so_cp = so->closest_point(moinst_center, so_inst); if(mo->inside_instance(so_cp, mo_inst) || so->inside_instance(mo_cp, so_inst)){ // We have a collison! -// printf("Collision Found: (%f %f %f), (%f %f %f)\n", moinst_center.x, moinst_center.y, moinst_center.z, soinst_center.x, soinst_center.y, soinst_center.z); + // printf("Collision Found: (%f %f %f), (%f %f %f)\n", moinst_center.x, moinst_center.y, moinst_center.z, soinst_center.x, soinst_center.y, soinst_center.z); mo->report_collision(so, so_inst, mo_inst); so->report_collision(mo, mo_inst, so_inst); } @@ -66,13 +66,51 @@ void collision_check(){ } } +float closest_check(vec3 pointcheck){ + float min_distance = 1000.0; + for(auto so : solid_objects){ + for(size_t so_inst = 0; so_inst < so->models.size(); so_inst++){ + vec3 soinst_center = so->get_center(so_inst); + float fcd = distance(pointcheck, soinst_center); + if(so->fast_check_distance(so_inst) < fcd) { + if(fcd < min_distance) + min_distance = fcd; + continue; + } + if(so->inside_instance(pointcheck, so_inst)){ + // There is something in the way! + return 0.0f; + } + vec3 so_cp = so->closest_point(pointcheck, so_inst); + float compare_dist = distance(so_cp, pointcheck); + if(compare_dist < min_distance) + min_distance = compare_dist; + } + } + return min_distance; +} + +bool can_see(vec3 A, vec3 B){ + vec3 D = normalize(B - A); + while(true){ + float move_distance = closest_check(A); + /* Should the 0.01 be a parameter like "Open space width"? */ + if(move_distance < 0.01) + return false; + float remaining_distance = distance(A, B); + if(remaining_distance <= (move_distance + 0.5)) + return true; + A = A + (D * move_distance); + } +} + void collision_check_thread_function(){ for(;;){ double startime = glfwGetTime(); collision_check(); double endtime = glfwGetTime(); - + g.cctime = endtime - startime; } } @@ -180,7 +218,7 @@ void movement_function() { for(auto o : game_objects) o->movement(1000 * (start_time - last_start)); last_start = start_time; - + double end_time = glfwGetTime(); movement_count++; @@ -199,14 +237,14 @@ void stats_function() { movement_count = 0; printf("FPS: %u cctime = %lf movement_time = %lf gmtime = %lf\n", g.last_fps, g.cctime, g.movement_time, g.gmtime); /* - for(auto proj : game_objects) { + for(auto proj : game_objects) { if(proj->name == "Projectiles") { - printf("Projectile count: %lu\n", ((Projectile*)proj)->models.size()); - break; + printf("Projectile count: %lu\n", ((Projectile*)proj)->models.size()); + break; } - } - */ - + } + */ + this_thread::sleep_for(chrono::milliseconds(1000)); } } @@ -301,6 +339,13 @@ void mouse_button_callback(GLFWwindow* window, int button, int action, int mods) g.release_cursor = false; else if (button == GLFW_MOUSE_BUTTON_LEFT && action == GLFW_PRESS) { fire(); + } else if(button == GLFW_MOUSE_BUTTON_RIGHT && action == GLFW_PRESS) { + vec3 B; + B.x = g.player_position.x + cos(g.heading) * 10; + B.y = g.player_position.y; + B.z = g.player_position.z + sin(g.heading) * 10; + bool is_clear = can_see(g.player_position, B); + cout << "is_clear: " << is_clear << endl; } } diff --git a/textures/cat.jpg b/textures/cat.jpg new file mode 100644 index 0000000..ac2df8f Binary files /dev/null and b/textures/cat.jpg differ diff --git a/textures/mouse.jpg b/textures/mouse.jpg new file mode 100644 index 0000000..d10d164 Binary files /dev/null and b/textures/mouse.jpg differ