Implemented many things. Refactored parsers. Added some behaviors and items. Added weapons.

This commit is contained in:
2016-12-02 20:29:40 +02:00
parent 0b6a988184
commit f255905c73
70 changed files with 1614 additions and 908 deletions

View File

@@ -10,6 +10,8 @@
#include <controller/DebugController.h>
#include <input/Input.h>
#include <iostream>
using namespace farmlands::input;
namespace farmlands {
@@ -19,6 +21,7 @@ static const float ScaleVelocity = 0.5f;
static const float ScaleShiftVelocity = 2.0f;
DebugController::DebugController()
: m_camera(nullptr)
{
}
@@ -26,6 +29,11 @@ DebugController::~DebugController()
{
}
base::Component* DebugController::clone()
{
return new DebugController();
}
void DebugController::onInitialize()
{
m_camera = GameState::current().renderContext.camera();
@@ -49,5 +57,13 @@ void DebugController::onUpdateLogic()
m_camera->scale *= 1 - vel;
}
void DebugController::dump(unsigned level)
{
for (unsigned i = 0; i < level; i++)
std::cout<<" ";
std::cout << " .Component: DebugController\n";
}
} /* namespace controller */
} /* namespace farmlands */

View File

@@ -20,6 +20,9 @@ namespace controller {
DebugController();
virtual ~DebugController();
virtual base::Component* clone() override;
virtual void dump(unsigned level) override;
virtual void onInitialize() override;
virtual void onUpdateLogic() override;

View File

@@ -51,6 +51,7 @@ bool FarmlandsGame::initialize()
// Finish initialization
GameState::current().scene->root.onInitialize();
GameState::current().gameInitialized = true;
return true;
}

View File

@@ -9,10 +9,10 @@
#include <controller/GuiController.h>
#include <gui/widgets/TextArea.h>
namespace farmlands
{
namespace controller
{
#include <iostream>
namespace farmlands {
namespace controller {
GuiController::GuiController()
: m_canvas()
@@ -23,6 +23,11 @@ GuiController::~GuiController()
{
}
base::Component* GuiController::clone()
{
return new GuiController();
}
void GuiController::onInitialize()
{
m_context = &GameState::current().renderContext;
@@ -70,5 +75,13 @@ void GuiController::onRender()
m_canvas.render(m_context);
}
void GuiController::dump(unsigned level)
{
for (unsigned i = 0; i < level; i++)
std::cout<<" ";
std::cout << " .Component: DebugController\n";
}
} /* namespace controller */
} /* namespace farmlands */

View File

@@ -23,6 +23,9 @@ namespace controller {
GuiController();
virtual ~GuiController();
virtual base::Component* clone() override;
virtual void dump(unsigned level) override;
/**
* Initializes game renderer
*/

View File

@@ -0,0 +1,50 @@
/*
* Giftable.cpp
*
* Created on: Dec 2, 2016
* Author: tibi
*/
#include <controller/items/Giftable.h>
#include <iostream>
namespace farmlands {
namespace controller {
namespace items {
Giftable::Giftable()
{
}
Giftable::~Giftable()
{
}
base::Component* Giftable::clone()
{
return new Giftable();
}
bool Giftable::canGift(float x, float y, model::Direction d)
{
// TODO: implement Giftable::canGift
return false;
}
void Giftable::offerGift(float x, float y, model::Direction d)
{
// TODO: implement Giftable::offerGift
}
void Giftable::dump(unsigned level)
{
for (unsigned i = 0; i < level; i++)
std::cout<<" ";
std::cout << " .Component: Giftable\n";
}
} /* namespace items */
} /* namespace controller */
} /* namespace farmlands */

View File

@@ -0,0 +1,35 @@
/*
* Giftable.h
*
* Created on: Dec 2, 2016
* Author: tibi
*/
#ifndef CONTROLLER_ITEMS_GIFTABLE_H_
#define CONTROLLER_ITEMS_GIFTABLE_H_
#include <base/Component.h>
#include <model/Direction.h>
namespace farmlands {
namespace controller {
namespace items {
class Giftable: public base::Component
{
public:
Giftable();
virtual ~Giftable();
virtual base::Component* clone() override;
virtual void dump(unsigned level) override;
bool canGift(float x, float y, model::Direction d);
void offerGift(float x, float y, model::Direction d);
};
} /* namespace items */
} /* namespace controller */
} /* namespace farmlands */
#endif /* CONTROLLER_ITEMS_GIFTABLE_H_ */

View File

@@ -1,30 +0,0 @@
/*
* ItemController.cpp
*
* Created on: Nov 30, 2016
* Author: tibi
*/
#include <controller/items/ItemController.h>
namespace farmlands
{
namespace controller
{
namespace items
{
ItemController::ItemController()
{
// TODO Auto-generated constructor stub
}
ItemController::~ItemController()
{
// TODO Auto-generated destructor stub
}
} /* namespace items */
} /* namespace controller */
} /* namespace farmlands */

View File

@@ -1,30 +0,0 @@
/*
* ItemController.h
*
* Created on: Nov 30, 2016
* Author: tibi
*/
#ifndef CONTROLLER_ITEMS_ITEMCONTROLLER_H_
#define CONTROLLER_ITEMS_ITEMCONTROLLER_H_
#include <base/Component.h>
namespace farmlands {
namespace controller {
namespace items {
class ItemController: public base::Component
{
public:
ItemController();
virtual ~ItemController();
float enemyDamage;
};
} /* namespace items */
} /* namespace controller */
} /* namespace farmlands */
#endif /* CONTROLLER_ITEMS_ITEMCONTROLLER_H_ */

View File

@@ -0,0 +1,83 @@
/*
* Weapon.cpp
*
* Created on: Dec 2, 2016
* Author: tibi
*/
#include <GameState.h>
#include <controller/items/Weapon.h>
#include <iostream>
namespace farmlands {
namespace controller {
namespace items {
Weapon::Weapon()
: damage(0),
critProbability(0),
critDamage(0),
attackDuration(1.0f),
m_spriteRenderer(nullptr),
m_attackTimeLeft(0)
{
}
Weapon::~Weapon()
{
}
base::Component* Weapon::clone()
{
Weapon* clone = new Weapon();
clone->damage = damage;
clone->critProbability = critProbability;
clone->critDamage = critDamage;
clone->attackDuration = attackDuration;
return clone;
}
void Weapon::onInitialize()
{
m_spriteRenderer = gameObject->component<graphics::SpriteRenderer>();
}
void Weapon::onPreRender()
{
m_spriteRenderer->sprite->setState(m_attackTimeLeft > 0);
}
void Weapon::onUpdateLogic()
{
if (m_attackTimeLeft > 0)
{
m_attackTimeLeft -= GameState::current().elapsedTime;
}
}
void Weapon::performAttack(float x, float y, model::Direction d)
{
if (m_attackTimeLeft <= 0)
{
m_attackTimeLeft = attackDuration;
}
}
void Weapon::dump(unsigned level)
{
for (unsigned i = 0; i < level; i++)
std::cout<<" ";
std::cout << " .Component: Transform ";
std::cout << "damage="<<damage<<" ";
std::cout << "critProbability="<<critProbability<<" ";
std::cout << "critDamage="<<critDamage<<" ";
std::cout << "attackDuration="<<attackDuration<<"\n";
}
}
} /* namespace model */
} /* namespace farmlands */

View File

@@ -0,0 +1,53 @@
/*
* Weapon.h
*
* Created on: Dec 2, 2016
* Author: tibi
*/
#ifndef CONTROLLER_ITEMS_WEAPON_H_
#define CONTROLLER_ITEMS_WEAPON_H_
#include <base/Component.h>
#include <graphics/SpriteRenderer.h>
#include <model/Direction.h>
namespace farmlands {
namespace controller {
namespace items {
class Weapon: public base::Component
{
public:
Weapon();
virtual ~Weapon();
virtual base::Component* clone() override;
virtual void dump(unsigned level) override;
virtual void onInitialize() override;
virtual void onUpdateLogic() override;
virtual void onPreRender() override;
/**
* Performs everything required to attacking an enemy with a weapon. To be called from player controller.
*/
void performAttack(float x, float y, model::Direction d);
// Properties
float damage;
float critProbability;
float critDamage;
float attackDuration; // In seconds
private:
graphics::SpriteRenderer* m_spriteRenderer;
float m_attackTimeLeft;
};
}
} /* namespace model */
} /* namespace farmlands */
#endif /* CONTROLLER_ITEMS_WEAPON_H_ */

View File

@@ -6,13 +6,20 @@
*/
#include <GameState.h>
#include <controller/items/Giftable.h>
#include <controller/items/Weapon.h>
#include <controller/player/PlayerController.h>
#include <graphics/SpriteRenderer.h>
#include <input/Input.h>
#include <model/Item.h>
#include <utils/Assert.h>
using namespace farmlands::input;
#include <iostream>
using namespace farmlands::base;
using namespace farmlands::controller::items;
using namespace farmlands::graphics;
using namespace farmlands::input;
using namespace farmlands::model;
namespace farmlands {
@@ -35,10 +42,10 @@ static const Direction VelocitySignDirections[3][3] =
PlayerController::PlayerController()
: m_transform(nullptr),
m_attackTimeLeft(0),
m_facingDirection(Direction::South),
m_walking(false),
m_running(false)
m_running(false),
m_currentItem(nullptr)
{
}
@@ -46,6 +53,11 @@ PlayerController::~PlayerController()
{
}
base::Component* PlayerController::clone()
{
return new PlayerController();
}
void PlayerController::onInitialize()
{
m_transform = gameObject->component<base::Transform>();
@@ -53,18 +65,68 @@ void PlayerController::onInitialize()
bool PlayerController::onEvent(SDL_Event& event)
{
bool attack1 = (Input::instance().down(GameKey::Action, event));
bool attack2 = (Input::instance().down(GameKey::Action2, event));
if (m_attackTimeLeft == 0 && (attack1 || attack2))
{
attack();
m_attackTimeLeft = 20 + attack2 * 20;
}
handleAttackEvents(event);
handleInventoryEvents(event);
return false;
}
void PlayerController::handleAttackEvents(SDL_Event& event)
{
bool attack = (Input::instance().down(GameKey::Action, event));
if (attack && m_currentItem != nullptr)
{
// Weapon behavior
Weapon* weapon = m_currentItem->component<Weapon>();
if (weapon)
weapon->performAttack(m_transform->x, m_transform->y, m_facingDirection);
// Gift behavior
Giftable* giftable = m_currentItem->component<Giftable>();
if (giftable)
giftable->offerGift(m_transform->x, m_transform->y, m_facingDirection);
}
}
void PlayerController::handleInventoryEvents(SDL_Event& event)
{
// See what key was pressed
int slot = -1;
if (Input::instance().down(GameKey::Inventory1, event)) slot = 1;
if (Input::instance().down(GameKey::Inventory2, event)) slot = 2;
if (Input::instance().down(GameKey::Inventory3, event)) slot = 3;
if (Input::instance().down(GameKey::Inventory4, event)) slot = 4;
if (Input::instance().down(GameKey::Inventory5, event)) slot = 5;
if (Input::instance().down(GameKey::Inventory6, event)) slot = 6;
if (Input::instance().down(GameKey::Inventory7, event)) slot = 7;
if (Input::instance().down(GameKey::Inventory8, event)) slot = 8;
if (Input::instance().down(GameKey::Inventory9, event)) slot = 9;
if (Input::instance().down(GameKey::Inventory10, event)) slot = 10;
// For now we don't have an inventory, so just instantiate some object
if (0 <= slot && slot <= GameState::current().itemPrefabs.size())
{
GameObject* itemPrefab = GameState::current().itemPrefabs[slot - 1];
// Get rid of old object
if (m_currentItem != nullptr)
gameObject->destroyChild(m_currentItem);
// Instantiate new object
m_currentItem = GameObject::instantiate(itemPrefab, "Current item", gameObject);
// Print some information
// Item* item = itemPrefab->component<Item>();
// std::string name = (item == nullptr) ? "unknown" : item->name;
// std::cout << "Instantiated object " << slot << " (" << name << ")\n";
}
else if (0 <= slot)
{
std::cout << "Slot " << slot << "empty\n";
}
}
void PlayerController::onUpdateLogic()
{
m_running = false;
@@ -73,13 +135,13 @@ void PlayerController::onUpdateLogic()
// Compute movement velocity
float velMultiplier = PlayerWalkVelocity;
if (m_attackTimeLeft)
{
velMultiplier = PlayerAttackVelocity;
--m_attackTimeLeft;
}
// if (m_attackTimeLeft)
// {
// velMultiplier = PlayerAttackVelocity;
// --m_attackTimeLeft;
// } else
else if (Input::instance().pressed(GameKey::Run))
if (Input::instance().pressed(GameKey::Run))
{
velMultiplier = PlayerRunVelocity;
m_running = true;
@@ -127,11 +189,19 @@ void PlayerController::onPreRender()
// Set animation velocity
float animVelocity = (m_running) ? 1.0f : 0.7f;
if (m_attackTimeLeft)
animVelocity = 0.1f;
// if (m_attackTimeLeft)
// animVelocity = 0.1f;
spriteRenderer->sprite->setAnimationVelocity(animVelocity);
// Set weapon
if (m_currentItem)
{
Transform* itemTransf = m_currentItem->component<Transform>();
itemTransf->x = 0.2f;
itemTransf->y = -0.8f;
}
// Set camera
base::Transform* cam = GameState::current().renderContext.cameraTransform();
cam->x = m_transform->x;
@@ -157,6 +227,14 @@ void PlayerController::attack()
// For now - nothing
}
void PlayerController::dump(unsigned level)
{
for (unsigned i = 0; i < level; i++)
std::cout<<" ";
std::cout << " .Component: PlayerController\n";
}
}
} /* namespace controller */
} /* namespace farmlands */

View File

@@ -24,6 +24,9 @@ namespace player {
PlayerController();
virtual ~PlayerController();
virtual base::Component* clone() override;
virtual void dump(unsigned level) override;
virtual void onInitialize() override;
virtual bool onEvent(SDL_Event& event) override;
virtual void onUpdateLogic() override;
@@ -32,14 +35,17 @@ namespace player {
private:
static model::Direction getDirection(float vx, float vy);
void handleAttackEvents(SDL_Event& event);
void handleInventoryEvents(SDL_Event& event);
bool canMove(float x, float y);
void attack();
base::Transform* m_transform;
uint32_t m_attackTimeLeft;
model::Direction m_facingDirection;
bool m_walking, m_running;
base::GameObject* m_currentItem;
};
}