From 1393559cfb7a2ed2022afe29b43642e4dcbe7ba5 Mon Sep 17 00:00:00 2001 From: Rommmmaha Date: Tue, 11 Jun 2024 14:38:14 +0300 Subject: [PATCH] . --- client/game.cpp | 4 +- client/game.hpp | 4 +- client/object.hpp | 120 +++++++++++++++++++++++----------------------- 3 files changed, 65 insertions(+), 63 deletions(-) diff --git a/client/game.cpp b/client/game.cpp index 3292a0c..0b6b421 100644 --- a/client/game.cpp +++ b/client/game.cpp @@ -107,7 +107,7 @@ void game::init() } // Player - player = object(1, int(map_size.x) / 2, 1); + player = _player(int(map_size.x) / 2, 1); player.shape = sf::RectangleShape(scale_vector); player.direction = -1; @@ -115,7 +115,7 @@ void game::init() enemies.clear(); for (; enemies.size() < number_of_enemies;) { - enemies.push_back(object(2, 3 + rand() % (map_size.x - 6), 3 + rand() % (map_size.y - 6))); + enemies.push_back(_enemy(3 + rand() % (map_size.x - 6), 3 + rand() % (map_size.y - 6))); enemies.back().shape = sf::RectangleShape(scale_vector); } diff --git a/client/game.hpp b/client/game.hpp index e64bcfd..b489d40 100644 --- a/client/game.hpp +++ b/client/game.hpp @@ -31,8 +31,8 @@ class game sf::Vector2f scale_vector; sf::RenderWindow *_RenderWindow; - object player; - std::vector enemies; + _player player; + std::vector<_enemy> enemies; sf::Clock _Clock; sf::Clock _GlobalClock; diff --git a/client/object.hpp b/client/object.hpp index 3bc6a04..f281346 100644 --- a/client/object.hpp +++ b/client/object.hpp @@ -1,76 +1,20 @@ #pragma once #include "SFML/System.hpp" -class object : public sf::Vector2i +class _object : public sf::Vector2i { public: - int type; // 1 - player, 2 - enemy int direction; sf::RectangleShape shape; sf::Vector2i previous = sf::Vector2i(0, 0); - object() + _object(int x = 0, int y = 0) { - this->x = 0; - this->y = 0; - this->type = 0; - this->direction = 0; - this->previous = sf::Vector2i(0, 0); - } - object(int type, int x = 0, int y = 0) - { - this->type = type; this->x = x; this->y = y; this->direction = 0; this->previous = sf::Vector2i(0, 0); - if (this->type == 2) - this->direction = rand() % 4; } - void move() + virtual void move() { - if (type == 1) // player movement - { - switch (direction) - { - case -1: - break; - case sf::Keyboard::Up: - --y; - break; - case sf::Keyboard::Down: - ++y; - break; - case sf::Keyboard::Left: - --x; - break; - case sf::Keyboard::Right: - ++x; - break; - } - } - if (type == 2) // enemy movement - { - switch (direction) - { - case -1: - break; - case 0: // UL - --x; - --y; - break; - case 1: // DL - --x; - ++y; - break; - case 2: // UR - ++x; - --y; - break; - case 3: // DR - ++x; - ++y; - break; - } - } } void updateShape(sf::Vector2f scale) { @@ -78,3 +22,61 @@ class object : public sf::Vector2i this->shape.setPosition(sf::Vector2f(x * scale.x, y * scale.y)); } }; + +class _player : public _object +{ + public: + using _object::_object; + void move() + { + switch (direction) + { + case -1: + break; + case sf::Keyboard::Up: + --y; + break; + case sf::Keyboard::Down: + ++y; + break; + case sf::Keyboard::Left: + --x; + break; + case sf::Keyboard::Right: + ++x; + break; + } + } +}; + +class _enemy : public _object +{ + public: + using _object::_object; + void move() + { + if (direction == -1) + this->direction = rand() % 4; + switch (direction) + { + case -1: + break; + case 0: // UL + --x; + --y; + break; + case 1: // DL + --x; + ++y; + break; + case 2: // UR + ++x; + --y; + break; + case 3: // DR + ++x; + ++y; + break; + } + } +};