/* client/input.h This file is part of the Osirion project and is distributed under the terms and conditions of the GNU General Public License version 2 */ #include "core/core.h" #include "client/client.h" #include "client/input.h" #include "client/console.h" #include "client/camera.h" #include "client/keyboard.h" #include "SDL/SDL.h" namespace client { namespace input { // local offset to make turns float local_turn; // local thrust setting float local_thrust; core::Player *localplayer() { return core::game()->localplayer(); } core::EntityControlable *localcontrol() { if (core::game()->localplayer()) return core::game()->localplayer()->control; else return 0; } void init() { con_print << "Initializing input..." << std::endl; client::setkeyboardmode(console::visible()); } void shutdown() { con_print << "Shutting down input..." << std::endl; } // handle key release for the game world void keyreleased(const SDL_keysym &keysym) { switch (keysym.sym) { case SDLK_SPACE: camera::next_mode(); break; default: break; } } // handle key press events for the game world void keypressed(const SDL_keysym &keysym) { switch (keysym.sym) { case SDLK_LEFT: camera::key_left(); break; case SDLK_RIGHT: camera::key_right(); break; case SDLK_UP: camera::key_up(); break; case SDLK_DOWN: camera::key_down(); break; case SDLK_KP_PLUS: // TODO set core entity params local_thrust += 0.08f; if (local_thrust > 1.0f) local_thrust = 1.0f; break; case SDLK_KP_MINUS: // TODO set core entity params local_thrust -= 0.1f; if (local_thrust < 0.0f) local_thrust = 0.0f; break; case SDLK_KP4: // TODO set core entity params local_turn += 5; if (math::degrees180f(local_turn - localcontrol()->direction()) > 90) local_turn = localcontrol()->direction() + 90; if (local_turn > 360) local_turn -= 360; break; case SDLK_KP6: // TODO set core entity params local_turn -= 5; if (math::degrees180f(local_turn - localcontrol()->direction()) < -90) local_turn = localcontrol()->direction() - 90; if (local_turn < 0) local_turn += 360; break; default: break; } } void frame(float seconds) { SDL_Event event; while (SDL_PollEvent(&event)) { switch (event.type) { case SDL_KEYUP: if (!console::visible() && core::application()->connected() && localcontrol()) // send key events to the game world keyreleased(event.key.keysym); break; case SDL_KEYDOWN: if (event.key.keysym.sym == '`' || event.key.keysym.sym == '~') { console::toggle(); } else if (console::visible()) { // send key events to the console console::keypressed(event.key.keysym); } else if (core::application()->connected() && localcontrol()) { // send key events to the game world keypressed(event.key.keysym); } break; case SDL_QUIT: core::application()->shutdown(); break; } } if (!console::visible() && core::application()->connected() && localcontrol()) { localcontrol()->set_thrust(local_thrust); localcontrol()->set_direction(local_turn); } } } // namespace input } // namespace client