From 0c8636d6a54f6d12a8ba1afa6dc0712b32edace3 Mon Sep 17 00:00:00 2001 From: jakeroggenbuck Date: Thu, 5 Jan 2023 19:55:47 -0800 Subject: [PATCH 1/2] Add bullet start --- src/game/bullet.cpp | 30 +++++++++++++++++++++++++ src/game/bullet.hpp | 26 ++++++++++++++++++++++ src/game/gameobject.hpp | 2 ++ src/game/movement.cpp | 29 +++++++++++++++++++----- src/game/movement.hpp | 1 + src/main.cpp | 49 +++++++++++++++++++++-------------------- 6 files changed, 108 insertions(+), 29 deletions(-) create mode 100644 src/game/bullet.cpp create mode 100644 src/game/bullet.hpp diff --git a/src/game/bullet.cpp b/src/game/bullet.cpp new file mode 100644 index 0000000..0c61bbd --- /dev/null +++ b/src/game/bullet.cpp @@ -0,0 +1,30 @@ +#include "bullet.hpp" + +#include + +namespace game { + +Bullet::Bullet(float xloc, float yloc, float angle) { + this->x = xloc; + this->y = yloc; + this->angle = angle; + this->vx = 0; + this->vy = 0; +} + +void Bullet::render() { + // TODO: Change to actual bullet texture + tex::PLAYER_TEX->render(this->x, this->y, 0.01f, 0.04f, + tex::RenderBasis::MID, tex::RenderBasis::MID); +} + +std::pair Bullet::getvel() { + return std::pair(this->vx, this->vy); +} + +void Bullet::setvel(std::pair newvel) { + this->vx = newvel.first; + this->vy = newvel.second; +} + +} // namespace game diff --git a/src/game/bullet.hpp b/src/game/bullet.hpp new file mode 100644 index 0000000..67e7a17 --- /dev/null +++ b/src/game/bullet.hpp @@ -0,0 +1,26 @@ +#pragma once + +#include + +#include +#include + +namespace game { + +class Bullet : public GameObject, Renderable { + + public: + void render() override; + Bullet(float xloc, float yloc, float angle); + + float angle; + + /* Bounce off the edges of the screen. */ + void do_bounce(float screen_edge); + + // Read and update velocities. + std::pair getvel(); + void setvel(std::pair newvel); +}; + +} // namespace game diff --git a/src/game/gameobject.hpp b/src/game/gameobject.hpp index ea2607b..32f577a 100644 --- a/src/game/gameobject.hpp +++ b/src/game/gameobject.hpp @@ -1,3 +1,5 @@ +#pragma once + namespace game { class GameObject { diff --git a/src/game/movement.cpp b/src/game/movement.cpp index cd5c550..9318501 100644 --- a/src/game/movement.cpp +++ b/src/game/movement.cpp @@ -16,7 +16,8 @@ float FRICTION = 0.01f; /* Get the new X and Y speed of the player, given the current X and Y speed and readings from GLFW of the currently pressed keys. */ -std::pair new_speed(GLFWwindow * wn, std::pair old_speed) { +std::pair new_speed(GLFWwindow *wn, + std::pair old_speed) { std::pair ret = old_speed; @@ -39,7 +40,7 @@ std::pair new_speed(GLFWwindow * wn, std::pair old_s /* We slow down a tiny bit if nothing is pressed. */ if (hinc == 0 && vinc == 0) { - ret.first *= (1.0f - FRICTION); + ret.first *= (1.0f - FRICTION); ret.second *= (1.0f - FRICTION); return ret; } @@ -52,11 +53,10 @@ std::pair new_speed(GLFWwindow * wn, std::pair old_s float update_frac = 1 / SLIPPERINESS; - ret.first = ret.first * (1 - update_frac) + ideal_x * update_frac; + ret.first = ret.first * (1 - update_frac) + ideal_x * update_frac; ret.second = ret.second * (1 - update_frac) + ideal_y * update_frac; return ret; - } int FPS = 50; @@ -83,7 +83,26 @@ void Player::do_bounce(float screen_edge) { vy *= -1; y = -screen_edge; } - } +void Bullet::do_bounce(float screen_edge) { + + if (x > screen_edge) { + vx *= -1; + x = screen_edge; + } + if (x < -screen_edge) { + vx *= -1; + x = -screen_edge; + } + if (y > screen_edge) { + vy *= -1; + y = screen_edge; + } + if (y < -screen_edge) { + vy *= -1; + y = -screen_edge; + } } + +} // namespace game diff --git a/src/game/movement.hpp b/src/game/movement.hpp index bd29fbb..8442410 100644 --- a/src/game/movement.hpp +++ b/src/game/movement.hpp @@ -7,6 +7,7 @@ #include #include +#include namespace mvmt { diff --git a/src/main.cpp b/src/main.cpp index 95cabc3..974477b 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -6,8 +6,8 @@ #include #include -#include +#include #include #include #include @@ -15,7 +15,7 @@ namespace sniper { -GLFWwindow * wn; +GLFWwindow *wn; void init() { @@ -27,47 +27,50 @@ void init() { glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE); wn = glfwCreateWindow(500, 500, "Sniper", nullptr, nullptr); - glfwMakeContextCurrent(wn); - gladLoadGL(); - glfwShowWindow(wn); + glfwMakeContextCurrent(wn); + gladLoadGL(); + glfwShowWindow(wn); gl::load_all_shaders(); tex::load_all_textures(); - } void mainloop() { + static float GAME_EDGE = 0.9f; + game::Player player(0, 0); - + game::Bullet bullet(0.2, 0.2, 1); + gl::GAME_SHADER->bind(); while (!glfwWindowShouldClose(wn)) { - // We want each frame to last for exactly 1/50th second, - // so capture the starting time so we can sleep for the - // needed amount of time at the end of the frame. - auto start_of_frame = std::chrono::steady_clock::now(); + // We want each frame to last for exactly 1/50th second, + // so capture the starting time so we can sleep for the + // needed amount of time at the end of the frame. + auto start_of_frame = std::chrono::steady_clock::now(); - glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); + glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // Update the player speed and position. player.setvel(mvmt::new_speed(wn, player.getvel())); player.do_pos_updates(); - player.do_bounce(0.9f); + player.do_bounce(GAME_EDGE); + bullet.do_bounce(GAME_EDGE); player.render(); + bullet.render(); - glfwSwapBuffers(wn); - glfwPollEvents(); + glfwSwapBuffers(wn); + glfwPollEvents(); - std::this_thread::sleep_until(start_of_frame + std::chrono::milliseconds(1000 / mvmt::FPS)); - - } + std::this_thread::sleep_until( + start_of_frame + std::chrono::milliseconds(1000 / mvmt::FPS)); + } gl::GAME_SHADER->unbind(); - } void cleanup() { @@ -76,18 +79,16 @@ void cleanup() { gl::unload_all_shaders(); glfwDestroyWindow(wn); - glfwTerminate(); - + glfwTerminate(); } -} +} // namespace sniper int main() { - + sniper::init(); sniper::mainloop(); sniper::cleanup(); return 0; - } From 944bc04be4d79fe333c9114d61164c40a16dfc0f Mon Sep 17 00:00:00 2001 From: jakeroggenbuck Date: Thu, 5 Jan 2023 19:58:34 -0800 Subject: [PATCH 2/2] Add todo --- src/game/movement.cpp | 1 + 1 file changed, 1 insertion(+) diff --git a/src/game/movement.cpp b/src/game/movement.cpp index 9318501..5a299eb 100644 --- a/src/game/movement.cpp +++ b/src/game/movement.cpp @@ -85,6 +85,7 @@ void Player::do_bounce(float screen_edge) { } } +// TODO: abstract this out - issue #44 void Bullet::do_bounce(float screen_edge) { if (x > screen_edge) {