Skip to content

Commit

Permalink
Merge pull request #7 from treplen/Plugins
Browse files Browse the repository at this point in the history
Plugins
  • Loading branch information
treplen authored Jun 4, 2017
2 parents d446476 + f14218d commit 978cf99
Show file tree
Hide file tree
Showing 16 changed files with 285 additions and 52 deletions.
21 changes: 20 additions & 1 deletion AtomGame/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,41 @@ project(AtomGame)

set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_FLAGS " -pthread")
set(CMAKE_POSITION_INDEPENDENT_CODE ON)

add_subdirectory(objects)
add_subdirectory(model)
add_subdirectory(view)
add_subdirectory(controller)
add_subdirectory(tests)

add_subdirectory(plugins/plugin)

add_custom_target(PROPS SOURCES log4cpp.properties)

set(SOURCE_FILES main.cpp)

add_executable(Run ${SOURCE_FILES})

add_library(RToRestart SHARED plugins/RToRestart.cpp plugins/RToRestart.h)

add_library(Rain SHARED plugins/Rain.cpp plugins/Rain.h)

target_link_libraries(Run log4cpp)
target_link_libraries(Run dl)
target_link_libraries(Run Objects)
#target_link_libraries(Game sfml-graphics sfml-system sfml-window sfml-graphics sfml-network sfml-audio)
target_link_libraries(Run Mechanics)
target_link_libraries(Run Graphics)
target_link_libraries(Run Plugin)

target_link_libraries(RToRestart log4cpp)
target_link_libraries(RToRestart Objects)
target_link_libraries(RToRestart Mechanics)
target_link_libraries(RToRestart Graphics)
target_link_libraries(RToRestart Plugin)

target_link_libraries(Rain log4cpp)
target_link_libraries(Rain Objects)
target_link_libraries(Rain Mechanics)
target_link_libraries(Rain Graphics)
target_link_libraries(Rain Plugin)
10 changes: 10 additions & 0 deletions AtomGame/controller/Controller.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,3 +58,13 @@ void Controller::tick ()
if (!movementKeyPressed)
onNoMovementKeyPress ();
}

bool Controller::isPressed (sf::Keyboard::Key key)
{
return sf::Keyboard::isKeyPressed (key);
}

bool Controller::isEnd ()
{
return sf::Keyboard::isKeyPressed (sf::Keyboard::Key::Escape);
}
5 changes: 5 additions & 0 deletions AtomGame/controller/Controller.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#ifndef ATOMGAME_ATOMGAME_H
#define ATOMGAME_ATOMGAME_H

#include <SFML/Window.hpp>
#include "../model/Model.h"
#include "../view/View.h"

Expand All @@ -28,6 +29,10 @@ class Controller

void tick ();

bool isPressed (sf::Keyboard::Key key);

bool isEnd ();

private:
static log4cpp::Category &logger;
Model *model;
Expand Down
33 changes: 30 additions & 3 deletions AtomGame/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,26 +4,53 @@

#include <log4cpp/Category.hh>
#include <log4cpp/PropertyConfigurator.hh>
#include <dlfcn.h>
#include "objects/Player.h"
#include "model/Model.h"
#include "view/View.h"
#include "plugins/plugin/Plugin.h"


int main ()
int main (int argc, char **argv)
{
std::string initFileName = "log4cpp.properties";
log4cpp::PropertyConfigurator::configure (initFileName);
std::vector<std::pair<void *, Plugin *>> plugins;
for (int i = 1; i < argc; i++)
{
const char *so = argv[i];
if (void *handle = dlopen (argv[i], RTLD_LAZY))
{
if (Plugin *(*create) () = (Plugin *(*) ()) dlsym (handle, "create"))
plugins.push_back (std::pair<void *, Plugin *> (handle, create ()));
else
dlclose (handle);
} else
std::cout << dlerror () << '\n';

}
Model model;

model.startGame ();

View view (&model, 600, 400);
Controller controller (&model, &view);
for (;;)

for (std::vector<std::pair<void *, Plugin *>>::iterator i = plugins.begin (); i != plugins.end (); i++)
i->second->start (&model, &controller, &view);

while (!controller.isEnd ())
{
controller.tick ();
model.tick ();
if (!view.tick ()) return 0;
if (!view.tick ()) break;
for (std::vector<std::pair<void *, Plugin *>>::iterator i = plugins.begin (); i != plugins.end (); i++)
i->second->tick ();
}

for (std::vector<std::pair<void *, Plugin *>>::const_iterator i = plugins.cbegin (); i != plugins.cend (); i++)
{
delete i->second;
dlclose (i->first);
}
}
33 changes: 15 additions & 18 deletions AtomGame/model/Model.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -64,16 +64,11 @@ const GameField &Model::getGameField () const
return gameField;
}

const std::vector<PhysicalObject *> &Model::getObjs () const
std::vector<PhysicalObject *> &Model::getObjs ()
{
return objs;
}

const std::vector<PhysicalObject *> &Model::getBlocks () const
{
return blocks;
}

void Model::movePlayer (Actor::Direction direction)
{
switch (direction)
Expand Down Expand Up @@ -238,8 +233,9 @@ Model::collidesOnY (const PhysicalObject &obj1, const PhysicalObject &obj2, Phys

Model::~Model ()
{
for (std::vector<PhysicalObject *>::const_iterator i = blocks.cbegin (); i != blocks.cend (); i++)
delete *i;
for (std::vector<PhysicalObject *>::const_iterator i = objs.cbegin (); i != objs.cend (); i++)
if (*i != &player)
delete *i;
}

void Model::load (const char *xmlfile, const char *name)
Expand Down Expand Up @@ -288,29 +284,30 @@ void Model::load (tinyxml2::XMLDocument &xmlDocument, const char *name)

void Model::load (tinyxml2::XMLElement *map)
{
for (std::vector<PhysicalObject *>::const_iterator i = objs.cbegin (); i != objs.cend (); i++)
if (*i != &player)
delete *i;
objs.clear ();
for(std::vector<PhysicalObject*>::const_iterator i = blocks.cbegin (); i != blocks.cend (); i++)
delete *i;
blocks.clear ();
for(tinyxml2::XMLElement* block = map->FirstChildElement ("Block"); block != nullptr; block = block->NextSiblingElement ("Block") )
{
if(const char* type = block->Attribute ("Type"))
{
if(strcmp (type, "Portal") == 0)
blocks.push_back (new Teleporter(0,0,10,10,block));
objs.push_back (new Teleporter (0, 0, 10, 10, block));
else if(strcmp (type, "MapChange") == 0)
blocks.push_back (new TransMapTeleporter(0,0,10,10, block));
objs.push_back (new TransMapTeleporter (0, 0, 10, 10, block));
else
blocks.push_back (new CustomObject(0,0,10,10, block));
objs.push_back (new CustomObject (0, 0, 10, 10, block));
}
else
logger.warn ("Block misses type, ignoring");
}
objs.emplace_back (&player);
/*for (std::vector<Bot>::iterator i = bots.begin (); i != bots.end (); ++i)
objs.emplace_back (&(*i));*/
for (std::vector<PhysicalObject *>::iterator i = blocks.begin (); i != blocks.end (); ++i)
objs.emplace_back (*i);
}

bool Model::isReloading () const
{
return teleporter != nullptr;
}


Expand Down
7 changes: 3 additions & 4 deletions AtomGame/model/Model.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,7 @@ class Model

const GameField &getGameField () const;

const std::vector<PhysicalObject *> &getBlocks () const;

const std::vector<PhysicalObject *> &getObjs () const;
std::vector<PhysicalObject *> &getObjs ();

Player &getPlayer ();

Expand All @@ -49,11 +47,12 @@ class Model

void load(tinyxml2::XMLElement* map);

bool isReloading () const;

private:
static log4cpp::Category &logger;
Player player;
//std::vector<Bot> bots;
std::vector<PhysicalObject *> blocks;
std::vector<PhysicalObject *> objs; //for movement algorithm standardization
GameField gameField;
TransMapTeleporter *teleporter;
Expand Down
27 changes: 27 additions & 0 deletions AtomGame/plugins/RToRestart.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
//
// Created by svuatoslav on 5/23/17.
//

#include "RToRestart.h"

void RToRestart::start (Model *model, Controller *controller, View *view)
{
_model = model;
_controller = controller;
}

void RToRestart::tick ()
{
if (_model && _controller)
if (_controller->isPressed (sf::Keyboard::Key::R))
_model->getPlayer ().respawn ();
}

RToRestart::RToRestart () : _model (nullptr), _controller (nullptr)
{
}

extern "C" Plugin *create ()
{
return new RToRestart;
}
23 changes: 23 additions & 0 deletions AtomGame/plugins/RToRestart.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
//
// Created by svuatoslav on 5/23/17.
//

#ifndef ATOMGAME_RTORESTART_H
#define ATOMGAME_RTORESTART_H

#include "plugin/Plugin.h"

class RToRestart : public Plugin
{
private:
Model *_model;
Controller *_controller;
public:
RToRestart ();

virtual void start (Model *model, Controller *controller, View *view);

virtual void tick ();
};

#endif //ATOMGAME_RTORESTART_H
53 changes: 53 additions & 0 deletions AtomGame/plugins/Rain.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
//
// Created by svuatoslav on 5/23/17.
//

#include "Rain.h"

void Rain::start (Model *model, Controller *controller, View *view)
{
_model = model;
timer = 0;
srand (time (0));
}

void Rain::tick ()
{
if (!_model->isReloading ())
{
if (timer == period)
{
PhysicalObject *drop = new PhysicalObject (rand () % diameter - diameter / 2 + _model->getPlayer ().getX (),
_model->getPlayer ().getY () - height, size, size);
drop->setVelocity (0, 1);
drop->setAcceleration (0, 1);
drops.push_back (drop);
_model->getObjs ().push_back (drop);
timer = 0;
} else
timer++;
for (std::vector<PhysicalObject *>::const_iterator i = drops.cbegin (); i != drops.cend (); i++)
{
if ((*i)->getVy () <= 0)
{
for (std::vector<PhysicalObject *>::const_iterator j = _model->getObjs ().cbegin ();
j != _model->getObjs ().cend (); j++)
{
if (*j == *i)
{
_model->getObjs ().erase (j);
break;
}
}
delete *i;
i = drops.erase (i);
}
}
} else
drops.clear ();
}

extern "C" Plugin *create ()
{
return new Rain;
}
30 changes: 30 additions & 0 deletions AtomGame/plugins/Rain.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
//
// Created by svuatoslav on 5/23/17.
//

#ifndef ATOMGAME_RAIN_H
#define ATOMGAME_RAIN_H


#include "plugin/Plugin.h"

class Rain : public Plugin
{
private:
const int period = 10;
const int diameter = 1000;
const int height = 1000;
const int size = 5;
int timer;
Model *_model;
std::vector<PhysicalObject *> drops;
public:


virtual void start (Model *model, Controller *controller, View *view);

virtual void tick ();
};


#endif //ATOMGAME_RAIN_H
8 changes: 8 additions & 0 deletions AtomGame/plugins/plugin/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
cmake_minimum_required(VERSION 3.6)
project(AtomGame)

set(CMAKE_CXX_STANDARD 11)

set(SOURCE_FILES Plugin.h Plugin.cpp)

add_library(Plugin STATIC ${SOURCE_FILES})
10 changes: 10 additions & 0 deletions AtomGame/plugins/plugin/Plugin.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
//
// Created by svuatoslav on 5/23/17.
//

#include "Plugin.h"

Plugin::~Plugin ()
{

}
Loading

0 comments on commit 978cf99

Please sign in to comment.