Skip to content
Snippets Groups Projects
Commit 5d6ca58f authored by Tomas Vybiral's avatar Tomas Vybiral
Browse files

added entities and fixes

parent 01e0bc85
No related branches found
No related tags found
No related merge requests found
......@@ -6,13 +6,54 @@ This project is using [cpp_starter_project](https://github.com/lefticus/cpp_star
 
Required:
 
* C++ compiler
* [SDL2](https://www.libsdl.org/download-2.0.php)
* [CMake](https://cmake.org/)
* [Conan](https://conan.io/)
* C++ compiler - GCC recomended
* [SDL2](https://www.libsdl.org/download-2.0.php) - rendering and input handeling
* [CMake](https://cmake.org/) - for project compilation
* [Conan](https://conan.io/) - for downloading extra libs
 
Extra:
 
* Doxygen (for documentation)
* libasan (for memory debugging)
* cppcheck
* Doxygen (for documentation) - not neededd for game compilation
* libasan (for memory debugging) - must be turned on in `cmake/Sanitizers.cmake`
* cppcheck - must be turned on in `cmake/StaticAnalyzers.cmake`
## Compilation
First you need to generate Makefile using cmake.
```
mkdir build
cd build
cmake ..
```
### Game
To compile:
```
make shooter
```
To run:
```
cd bin
./shooter
```
Please run the game from bin directory.
### Documentation
To compile:
```
make doxygen-docs
```
Documentation is then generated into `build/html` folder.
### Tests
TODO
\ No newline at end of file
option(ENABLE_CACHE "Enable cache if available" ON)
option(ENABLE_CACHE "Enable cache if available" OFF)
if(NOT ENABLE_CACHE)
return()
endif()
......
File moved
......@@ -48,7 +48,7 @@ void game::process_events() {
do_event<change_player_angle>(e, [&](const auto& cpa){player_angle_delta += cpa.delta;});
do_event<change_move_vector>(e, [&](const auto& cmv) {player_position_delta += cmv.delta;});
do_event<push_state>(e, [&](auto&) {current_state.push();});
do_event<pop_state>(e, [&](auto&) {spdlog::get("game")->warn("IMPLEMENT POP STATE OPERATOR.");});
do_event<pop_state>(e, [&](auto&) {current_state = std::move(state::pop());});
}
}
 
......
......@@ -9,7 +9,9 @@ sdl_resources::sdl_resources(SDL_Renderer* renderer): renderer(renderer) {
throw std::runtime_error(IMG_GetError());
}
shooter = load_texture("images/cannon.png");
enemy = load_texture("images/enemy1.png");
enemy1 = load_texture("images/enemy1.png");
enemy2 = load_texture("images/enemy2.png");
enemy3 = load_texture("images/enemy3.png");
projectile = load_texture("images/missile.png");
}
 
......
......@@ -17,8 +17,12 @@ public:
const SDL_Texture* shooter;
/// Projectile texture
const SDL_Texture* projectile;
/// Enemy texture
const SDL_Texture* enemy;
/// Enemy 1 texture
const SDL_Texture* enemy1;
/// Enemy 2 texture
const SDL_Texture* enemy2;
/// Enemy 3 texture
const SDL_Texture* enemy3;
protected:
SDL_Renderer* renderer;
/// Loads texture by path defined by path
......
add_library(state
state.h state.cpp
shooter.h shooter.cpp)
shooter.h shooter.cpp
entities.h entities.cpp)
 
target_include_directories(state PUBLIC ${CMAKE_CURRENT_SOURCE_DIR})
 
......
#include "entities.h"
#include <random>
#include <chrono>
#include <cmath>
entity::entity(const vector2& initial_position, uint32_t rad):position(initial_position), radius(rad) { }
bool entity::did_collide(const entity& other) const {
uint32_t min_distance = radius + other.radius;
double actual_distance = sqrt(pow(position.x - other.position.x, 2) +
pow(position.y - other.position.y, 2));
return actual_distance <= min_distance;
}
enemy::enemy(const vector2& initial_position, uint32_t rad): entity(initial_position, rad), variant(0) {
std::mt19937 rng(
static_cast<uint32_t>(
std::chrono::high_resolution_clock::now().time_since_epoch().count()));
variant = static_cast<uint32_t>(rng()) % MAX_VARIATIONS;
}
projectile::projectile(const vector2& initial_position, uint32_t rad, const vector2& initial_speed):
entity(initial_position, rad), speed(initial_speed) { }
#ifndef ENTITIES_H
#define ENTITIES_H
#include <cstdint>
const uint32_t MAX_VARIATIONS = 3;
struct vector2 {
uint32_t x, y;
};
class entity {
public:
entity(const vector2& initial_position, uint32_t rad);
bool did_collide(const entity& other) const;
vector2 position;
uint32_t radius;
};
class enemy : public entity {
public:
enemy(const vector2& initial_position, uint32_t rad);
uint32_t variant;
};
class projectile: public entity {
public:
projectile(const vector2& initial_position, uint32_t rad, const vector2& initial_speed);
vector2 speed;
};
#endif//ENTITIES_H
\ No newline at end of file
#include "shooter.h"
 
shooter::shooter():position{0, 0, shooter::MAX_POSITION},
angle{0.0, 0.0, shooter::MAX_ANGLE} { }
\ No newline at end of file
angle{0.0, 0.0, shooter::MAX_ANGLE} { }
shooter& shooter::operator=(const shooter& other) {
position.set(other.position.get());
angle.set(other.angle.get());
return *this;
}
\ No newline at end of file
......@@ -55,6 +55,10 @@ class shooter {
public:
/// Default contructor (defines ranges for each value)
shooter();
/// Default copy constructor
shooter(const shooter& other) = default;
/// Assign copy operator
shooter& operator=(const shooter& other);
 
/// Current shooter position
ranged_value<uint32_t> position;
......
#include "state.h"
 
#include <exception>
#include <cstring>
 
#include "spdlog/spdlog.h"
 
......@@ -19,4 +20,11 @@ state state::pop() {
}
}
 
state& state::operator=(const state& other) {
player = other.player;
enemies = other.enemies;
projectiles = other.projectiles;
return *this;
}
std::list<state> state::history;
\ No newline at end of file
......@@ -2,7 +2,9 @@
#define STATE_H
 
#include <list>
#include <vector>
 
#include "entities.h"
#include "shooter.h"
 
 
......@@ -16,20 +18,21 @@ public:
state() = default;
/// Copy constructor to store states in history
state(const state& other) = default;
/// Move constructor to store states in history
state(state && other) = default;
 
/// Copy operator to store state in history
state& operator=(const state& other) = default;
/// Move operator to store state in history
state& operator=(state&& other) = default;
state& operator=(const state& other);
 
/// Pushes current state to history
void push() const;
/// Retrurns latest state in history and pops it from stack
static state pop();
/// Player shooter data representation
shooter player;
/// All alive enemies
std::vector<enemy> enemies;
/// All visible projectiles
std::vector<projectile> projectiles;
protected:
/// List of all saved states.
static std::list<state> history;
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment