Skip to content

Commit

Permalink
add client test and add directory for server and client
Browse files Browse the repository at this point in the history
  • Loading branch information
michaeldomanek authored and michaeldomanek committed Mar 15, 2021
1 parent 748f5f9 commit 0e896a6
Show file tree
Hide file tree
Showing 30 changed files with 239 additions and 146 deletions.
Empty file modified CHANGELOG.org
100755 → 100644
Empty file.
Empty file modified LICENSE
100755 → 100644
Empty file.
Empty file modified README.md
100755 → 100644
Empty file.
Empty file modified builddir/.gitkeep
100755 → 100644
Empty file.
29 changes: 29 additions & 0 deletions client/include/connection.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#include "robotProperties.h"

#include "asio.hpp"
#include <SFML/Graphics.hpp>

#include <string>

using namespace std;
using namespace asio;
using namespace asio::ip;

class Connection {
private:

public:
Connection(string port, RobotProperties prop) {

tcp::iostream strm{"localhost", port};

try {
if (strm) {
strm << prop.getName() << endl;
strm.close();
}
} catch (asio::system_error& e) {
// return 0;
}
}
};
23 changes: 23 additions & 0 deletions client/src/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#include "connection.h"

#include "CLI11.hpp"
// todo: remvoe cli11 from client

using namespace std;

int main(int argc, char* argv[]) {
CLI::App app("Daytime Server");

string port{"1113"};
app.add_option("-p,--port", port, "port to connect to");

string name{"name"};
app.add_option("-n,--name", name, "name", true);

CLI11_PARSE(app, argc, argv);

RobotProperties properties{name, sf::Color::Blue};

Connection c{port, properties};

}
Empty file modified doc/.gitkeep
100755 → 100644
Empty file.
44 changes: 0 additions & 44 deletions include/bullet.h

This file was deleted.

35 changes: 22 additions & 13 deletions meson.build
100755 → 100644
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
# uncomment as necessary
project('RobotGame', 'cpp',
license : ['Boost'],
version : '0.0.4',
version : '0.1.1',
default_options : ['cpp_std=c++17', 'warning_level=3', 'werror=true'])

# thread_dep = dependency('threads')
thread_dep = dependency('threads')

# # begin asio: https://think-async.com/Asio/
# add_global_arguments('-I' + get_option('asio_include_dir'), language : 'cpp')
# add_global_arguments('-DASIO_STANDALONE', language : 'cpp')
# add_global_arguments('-DASIO_NO_DEPRECATED', language : 'cpp')
# # end asio
# begin asio: https://think-async.com/Asio/
add_global_arguments('-I' + get_option('asio_include_dir'), language : 'cpp')
add_global_arguments('-DASIO_STANDALONE', language : 'cpp')
add_global_arguments('-DASIO_NO_DEPRECATED', language : 'cpp')
# end asio

# CLI11: https://github.com/CLIUtils/CLI11
add_global_arguments('-I' + get_option('cli11_include_dir'), language : 'cpp')
Expand All @@ -31,16 +31,25 @@ add_global_arguments('-I' + get_option('spdlog_include_dir'), language : 'cpp')
# generated = gen.process('src/dummy.proto')
# # end protobuf

inc_dir = include_directories('include')
src = ['src/main.cpp', 'src/bullet.cpp', 'src/robot.cpp', 'src/window.cpp']
inc_dir_server = include_directories('server/include')
src_server = ['server/src/main.cpp', 'server/src/bullet.cpp', 'server/src/robot.cpp', 'server/src/window.cpp']
#protobuf: src = ['src/main.cpp', generated]

inc_dir_client = include_directories('client/include')
src_client = ['client/src/main.cpp']

graphics = dependency('sfml-graphics')
window = dependency('sfml-window')

executable('robo',
sources : src,
include_directories : inc_dir,
executable('robo-server',
sources : src_server,
include_directories : inc_dir_server,
# dependencies : [thread_dep, protobuf_dep]
dependencies : [graphics, window]
dependencies : [thread_dep, graphics, window]
)

executable('robo-client',
sources : src_client,
include_directories : [inc_dir_client,inc_dir_server],
dependencies : [thread_dep, graphics, window]
)
Empty file modified meson_options.txt
100755 → 100644
Empty file.
23 changes: 23 additions & 0 deletions server/include/bullet.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#pragma once

#include "robot.h"
#include "bulletConfiguration.h"

#include <SFML/Graphics.hpp>

class Bullet {
private:
sf::Sprite sprite;
float speed;
float damage;
Robot* attacker;
sf::Vector2f movement;
sf::Vector2f getMoveVector(float speed);
public:
Bullet(sf::Sprite turret, Robot* attacker, BulletConfiguration config);

void move();
sf::Sprite getSprite();
Robot* getAttacker();
float getDamage();
};
File renamed without changes.
46 changes: 1 addition & 45 deletions include/robot.h → server/include/robot.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,51 +48,7 @@ class Robot {
void rotateWeapon();
void shoot();
public:
Robot(RobotProperties properties, RobotConfiguration config):
health(config.getHealth()),
properties(properties),
config(config),
window(Window::getInstance())
{
robotTexture.loadFromFile("../src/resources/body-grey.png");
turretTexture.loadFromFile("../src/resources/turret.png");

robot.setTexture(robotTexture);
turret.setTexture(turretTexture);

robot.setOrigin(robot.getLocalBounds().width / 2, robot.getLocalBounds().height / 2);
turret.setOrigin(turret.getLocalBounds().width / 2, turret.getLocalBounds().height / 2);

robot.setScale(1.5, 1.5);
turret.setScale(1.5, 1.5);

movement = getMoveVector();

sf::FloatRect border{window.getBorder()};
robotBorder = {robot.getLocalBounds().width,
robot.getLocalBounds().height,
border.width - robot.getLocalBounds().width,
border.height - robot.getLocalBounds().height};

RobotStartConfiguration startConfig = window.getAvailablePosition();

sf::Vector2f pos{startConfig.getPosition()};
robot.setPosition(pos);
turret.setPosition(pos);

float rot{startConfig.getRotation()};
robot.setRotation(rot);
turret.setRotation(rot);

font.loadFromFile("../src/resources/ARIAL.TTF");
initialiseText(nameText, properties.getName());
initialiseText(healthText, fmt::format("{:.1f}", health));

nameText.setPosition(pos.x, pos.y + robot.getLocalBounds().width * 1.5);
healthText.setPosition(pos.x, pos.y - robot.getLocalBounds().width * 1.5);

turret.setColor(properties.getColor());
}
Robot(RobotProperties properties, RobotConfiguration config);

void moveForward();
void moveBackward();
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
16 changes: 1 addition & 15 deletions include/window.h → server/include/window.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,21 +29,7 @@ class Window {
void showAllBullets();
void showRobots();

Window(const unsigned int& width, const unsigned int& maxPlayers, const BulletConfiguration& config):
maxPlayers(maxPlayers),
config(config),
border(0, 0, (float)width, (float)width),
window(sf::VideoMode(width, width), "Robotgame"),
startConfigs(RobotStartConfiguration::getStartConfigs(width))
{
window.setFramerateLimit(120);

sf::Image icon;
icon.loadFromFile("../src/resources/icon.png");
window.setIcon(icon.getSize().x, icon.getSize().y, icon.getPixelsPtr());

explosionTexture.loadFromFile("../src/resources/explosion.png");
}
Window(const unsigned int& width, const unsigned int& maxPlayers, const BulletConfiguration& config);

public:
void addBullet(sf::Sprite turret, Robot* attacker);
Expand Down
50 changes: 50 additions & 0 deletions server/src/bullet.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
#include "bullet.h"
#include "robot.h"

#include <math.h>
#include <SFML/Graphics.hpp>

using namespace std;

Bullet::Bullet(sf::Sprite turret, Robot* attacker, BulletConfiguration config):
speed(config.getSpeed()),
damage(config.getDamage()),
attacker(attacker)
{
const unsigned int size{config.getSize()};

sf::Texture tex;
tex.create(size, size);

sprite.setTexture(tex);
sprite.setPosition(turret.getPosition());
sprite.setRotation(turret.getRotation());
sprite.setColor(sf::Color::White);
sprite.setOrigin(size / 2, size / 2);
sprite.setScale(1.5, 1.5);

//move to firepoint
movement = getMoveVector(speed);
sprite.move(25 * 1.5 * movement.x / speed, 25 * 1.5 * movement.y / speed);
}

sf::Vector2f Bullet::getMoveVector(float speed) {
float angle = sprite.getRotation() * M_PI / 180;
return sf::Vector2f{sin(angle) * speed, cos(angle) * -speed};
}

void Bullet::move() {
sprite.move(movement.x, movement.y);
}

sf::Sprite Bullet::getSprite() {
return sprite;
}

Robot* Bullet::getAttacker() {
return attacker;
}

float Bullet::getDamage() {
return damage;
}
29 changes: 28 additions & 1 deletion src/main.cpp → server/src/main.cpp
100755 → 100644
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include "robotProperties.h"
#include "bulletConfiguration.h"

#include "asio.hpp"
#include "CLI11.hpp"
#include <SFML/Graphics.hpp>
#include "spdlog/spdlog.h"
Expand All @@ -13,8 +14,11 @@
#include <vector>
#include <math.h>
#include <random>
#include <vector>

using namespace std;
using namespace asio;
using namespace asio::ip;

int main(int argc, char* argv[]) {
CLI::App app("Robotgame");
Expand All @@ -29,7 +33,7 @@ int main(int argc, char* argv[]) {
app.add_option("--bullet-damage", bulletDamage, "the damage of the bullets", true);

unsigned int bulletSize{3};
app.add_option("--bullet-size", bulletSize, "the size of the bullets in px", true); //->check(CLI::Range(1, 10));
app.add_option("--bullet-size", bulletSize, "the size of the bullets in px", true)->check(CLI::Range(1, 10));

float robotSpeed{2};
app.add_option("--robot-speed", robotSpeed, "the speed of the robots", true)->check(CLI::Range(0.5f, 10.0f));
Expand All @@ -49,6 +53,9 @@ int main(int argc, char* argv[]) {
unsigned int maxPlayers{4};
app.add_option("-m,--max-players", maxPlayers, "the maximum players of the game", true)->check(CLI::Range(2, 4));

unsigned short port{1113};
app.add_option("-p,--port", port, "port to connect to", true);

CLI11_PARSE(app, argc, argv);

auto file_logger = spdlog::rotating_logger_mt("file_logger", "../logs/server.log", 1048576 * 5, 3);
Expand All @@ -58,6 +65,26 @@ int main(int argc, char* argv[]) {

RobotConfiguration config{robotSpeed, health, robotRotation, turretRotation, minFireCountdown};
BulletConfiguration bulletConfig{bulletSpeed, bulletDamage, bulletSize};

for(unsigned int counter = 0; counter < maxPlayers;){
try {
io_context ctx;
tcp::endpoint ep{tcp::v4(), port};
tcp::acceptor acceptor{ctx, ep};

acceptor.listen();

tcp::iostream strm{acceptor.accept()};
string data;
strm >> data;

strm.close();
counter++;
cout << data << endl;
} catch (asio::system_error& e) {
return 0;
}
}

// This properties will be spezified by the client
RobotProperties properties{"Keyboard Controll", sf::Color::Blue};
Expand Down
File renamed without changes.
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
Loading

0 comments on commit 0e896a6

Please sign in to comment.