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

@ -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 */