123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244 |
- #include <Globals.h>
- #include "PlayerKam.h"
- #include "Globals.h"
- #include "Game.h"
- PlayerKam::PlayerKam(Framework::Bildschirm3D* zScreen)
- : Kam3D()
- {
- kameraControll = 0;
- setBildschirmPosition(0, 0);
- setBildschirmSize(zScreen->getBackBufferSize());
- setStyle(Kam3D::Style::Tick | Kam3D::Style::Movable | Kam3D::Style::Rotatable);
- setRotation({ (float)PI / 2.f, 0, 0 });
- entityId = -1;
- }
- void PlayerKam::setDirection(Framework::Vec3<float> direction)
- {
- if (direction.getLengthSq() > 0)
- {
- float rotZ = std::atan2(direction.y, direction.x) + (float)PI / 2;
- setRotation({ getRotation().x, getRotation().y, rotZ });
- }
- }
- void PlayerKam::doTastaturEreignis(Framework::TastaturEreignis& te)
- {
- char action[2];
- if (te.id == TE_Press)
- {
- action[0] = 1;
- if (te.taste == 'w' || te.taste == 'W')
- {
- action[1] = 0;
- network->zFactoryClient()->sendPlayerAction(action, 2);
- }
- if (te.taste == 'a' || te.taste == 'A')
- {
- action[1] = 1;
- network->zFactoryClient()->sendPlayerAction(action, 2);
- }
- if (te.taste == 's' || te.taste == 'S')
- {
- action[1] = 2;
- network->zFactoryClient()->sendPlayerAction(action, 2);
- }
- if (te.taste == 'd' || te.taste == 'D')
- {
- action[1] = 3;
- network->zFactoryClient()->sendPlayerAction(action, 2);
- }
- if (te.taste == T_Shift)
- {
- action[1] = 4;
- network->zFactoryClient()->sendPlayerAction(action, 2);
- }
- if (te.taste == 'q' || te.taste == 'Q')
- {
- action[1] = 5;
- network->zFactoryClient()->sendPlayerAction(action, 2);
- }
- if (te.taste == 'e' || te.taste == 'E')
- {
- action[1] = 6;
- network->zFactoryClient()->sendPlayerAction(action, 2);
- }
- if (te.taste == T_Space)
- {
- action[1] = 7;
- network->zFactoryClient()->sendPlayerAction(action, 2);
- }
- if (te.taste >= '0' && te.taste <= '9')
- {
- char action[5];
- action[0] = 3;
- *(int*)(action + 1) = te.taste - '1';
- if (*(int*)(action + 1) < 0)
- *(int*)(action + 1) = 9;
- network->zFactoryClient()->sendPlayerAction(action, 5);
- }
- }
- if (te.id == TE_Release)
- {
- action[0] = 0;
- if (te.taste == 'w' || te.taste == 'W')
- {
- action[1] = 0;
- network->zFactoryClient()->sendPlayerAction(action, 2);
- }
- if (te.taste == 'a' || te.taste == 'A')
- {
- action[1] = 1;
- network->zFactoryClient()->sendPlayerAction(action, 2);
- }
- if (te.taste == 's' || te.taste == 'S')
- {
- action[1] = 2;
- network->zFactoryClient()->sendPlayerAction(action, 2);
- }
- if (te.taste == 'd' || te.taste == 'D')
- {
- action[1] = 3;
- network->zFactoryClient()->sendPlayerAction(action, 2);
- }
- if (te.taste == T_Shift)
- {
- action[1] = 4;
- network->zFactoryClient()->sendPlayerAction(action, 2);
- }
- if (te.taste == 'q' || te.taste == 'Q')
- {
- action[1] = 5;
- network->zFactoryClient()->sendPlayerAction(action, 2);
- }
- if (te.taste == 'e' || te.taste == 'E')
- {
- action[1] = 6;
- network->zFactoryClient()->sendPlayerAction(action, 2);
- }
- if (te.taste == T_Space)
- {
- action[1] = 7;
- network->zFactoryClient()->sendPlayerAction(action, 2);
- }
- if (te.taste == T_Esc)
- {
- bool oldControl = kameraControll;
- kameraControll = 0;
- setShowCursor(true);
- if(!oldControl)
- ((Game*)(Menu*)menuRegister->get("game"))->closeCurrentDialog();
- }
- if (te.taste == T_Tab)
- {
- action[0] = 4;
- network->zFactoryClient()->sendPlayerAction(action, 1);
- }
- }
- }
- void PlayerKam::doMausEreignis(Framework::MausEreignis& me)
- {
- if (me.verarbeitet)
- {
- kameraControll = 0;
- setShowCursor(true);
- }
- else
- {
- if (!kameraControll)
- {
- if (me.id == ME_PLinks)
- {
- lastMousePos = { me.originalX, me.originalY };
- kameraControll = 1;
- }
- }
- else
- {
- if (kameraControll)
- {
- if (me.id == ME_Bewegung)
- {
- int yDir = lastMousePos.y - me.originalY;
- int xDir = lastMousePos.x - me.originalX;
- setRotation({ min(max(getRotation().x - yDir * 0.005f, 0.1f), 2.5f), getRotation().y, getRotation().z - xDir * 0.005f });
- if (getRotation().z > 2 * PI)
- setRotation({ getRotation().x, getRotation().y, getRotation().z - 2.f * (float)PI });
- if (getRotation().z < -2 * PI)
- setRotation({ getRotation().x, getRotation().y, getRotation().z + 2.f * (float)PI });
- Vec3<float> direction = getWorldDirection(getScreenPos() + getScreenSize() / 2);
- char action[13];
- action[0] = 2;
- *(float*)(action + 1) = direction.x;
- *(float*)(action + 5) = direction.y;
- *(float*)(action + 9) = direction.z;
- network->zFactoryClient()->sendPlayerAction(action, 13);
- SetCursorPos(lastMousePos.x + window->getPosition().x, lastMousePos.y + window->getPosition().y);
- setShowCursor(false);
- }
- if (me.id == ME_PLinks)
- {
- char action[2] = { 1, 8 };
- network->zFactoryClient()->sendPlayerAction(action, 2);
- }
- if (me.id == ME_RLinks)
- {
- char action[2] = { 0, 8 };
- network->zFactoryClient()->sendPlayerAction(action, 2);
- }
- if (me.id == ME_PRechts)
- {
- char action[2] = { 1, 9 };
- network->zFactoryClient()->sendPlayerAction(action, 2);
- }
- if (me.id == ME_RRechts)
- {
- char action[2] = { 0, 9 };
- network->zFactoryClient()->sendPlayerAction(action, 2);
- }
- }
- }
- me.verarbeitet = 1;
- }
- }
- bool PlayerKam::tick(double time)
- {
- __int64 style = 0;
- if (hatStyle(Style::Movable))
- style |= Style::Movable;
- if (hatStyle(Style::Rotatable))
- style |= Style::Rotatable;
- if (hatStyle(Style::Zoomable))
- style |= Style::Zoomable;
- removeStyle(Style::Movable | Style::Rotatable | Style::Zoomable);
- bool result = Kam3D::tick(time);
- addStyle(style);
- if (entityId >= 0)
- {
- Entity* entity = currentGame->zEntity(entityId);
- if (entity)
- {
- // update camera position
- setPosition(entity->getPos() + Vec3<float>(0.f, 0.f, 1.5f));
- ((Game*)(Menu*)menuRegister->get("game"))->updatePosition(entity->getPos(), 0, { 0, 0, 0 });
- }
- }
- return result;
- }
- void PlayerKam::setEntityId(int id)
- {
- entityId = id;
- }
- void PlayerKam::setControlEnabled(bool enabled)
- {
- kameraControll = enabled;
- setShowCursor(!kameraControll);
- }
|