Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
branch: master
Fetching contributors…

Cannot retrieve contributors at this time

100 lines (92 sloc) 2.347 kb
#include <glm/gtc/matrix_transform.hpp>
#include "input.h"
#include "core.h"
#include "phx.h"
#include "human.h"
#include "sprite.h"
#include "item.h"
#include "inventory.h"
#include "weapon.h"
#include "framework/utils/logger.h"
namespace Systems
{
void Input::onInit()
{
registerMsg(0);
registerMsg(1);
registerMsg(2);
registerMsg(3);
registerMsg(4);
}
void Input::addInput(EntityID id, Components::Input* input)
{
input->entity = id;
inputs[id] = input;
}
Components::Input* Input::getInput(EntityID id)
{
if(inputs.find(id) == inputs.end()) return nullptr;
return inputs[id];
}
void Input::deleteInput(EntityID id)
{
delete inputs[id];
inputs.erase(id);
}
void Input::onMsg(Core::Msg& msg)
{
Components::Input* input = getInput(msg.receiver);
if(!input) {
return;
}
Components::Human* human = sHuman.getHuman(msg.receiver);
if(human) {
if(human->state == Components::Human::S_DEAD ||
human->state == Components::Human::S_WEAK) {
return;
}
}
if(msg.id == msgId[0] && msg.data) { //Movement
int move = *reinterpret_cast<int*>(msg.data);
input->move.y = move;
} else if(msg.id == msgId[1] && msg.data) { //Direction
b2Vec2 dir = *reinterpret_cast<b2Vec2*>(msg.data);
dir *= 0.01f;
input->dir += dir;
} else if(msg.data) {
bool val = *reinterpret_cast<bool*>(msg.data);
if(msg.id == msgId[2]) input->take = val;
else if(msg.id == msgId[3]) input->drop = val;
else if(msg.id == msgId[4]) input->shoot = val;
}
b2Body* body = sPhx.getBody(msg.receiver);
if(input->sprite) {
b2Vec2 pos = input->dir;
input->sprite->mat = glm::translate(mat4(1.0f),glm::vec3(pos.x,pos.y,0.0f));
input->sprite->mat = glm::scale(input->sprite->mat,glm::vec3(0.2f,0.2f,1.0f));
}
if(input->shoot) { //Shoot/use
Components::Inventory* inventory = sInventory.getInventory(msg.receiver);
if(body && inventory) {
struct T {
b2Vec2 place;
b2Vec2 d;
}*t = new T();
t->place = body->GetPosition();
t->d = input->dir;
core.sendMsg<Systems::Weapon>(true,sWeapon.getMsg(0),(void*)t,inventory->slots[0]);
}
}
}
void Input::onUpdate(double dT)
{
for(auto c: inputs) {
if(c.second) {
Components::Input* input = c.second;
if(input->sprite) {
sRender.drawSprite(input->sprite);
}
}
}
}
}
Jump to Line
Something went wrong with that request. Please try again.