Skip to content

Commit

Permalink
.
Browse files Browse the repository at this point in the history
  • Loading branch information
Rommmmaha committed Jun 11, 2024
1 parent 0fac00a commit 1393559
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 63 deletions.
4 changes: 2 additions & 2 deletions client/game.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -107,15 +107,15 @@ 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;

// Enemies
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);
}

Expand Down
4 changes: 2 additions & 2 deletions client/game.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ class game
sf::Vector2f scale_vector;
sf::RenderWindow *_RenderWindow;

object player;
std::vector<object> enemies;
_player player;
std::vector<_enemy> enemies;

sf::Clock _Clock;
sf::Clock _GlobalClock;
Expand Down
120 changes: 61 additions & 59 deletions client/object.hpp
Original file line number Diff line number Diff line change
@@ -1,80 +1,82 @@
#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)
{
this->previous = sf::Vector2i(this->x, this->y);
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;
}
}
};

0 comments on commit 1393559

Please sign in to comment.