Project::OSiRiON - Git repositories
Project::OSiRiON
News . About . Screenshots . Downloads . Forum . Wiki . Tracker . Git
summaryrefslogtreecommitdiff
blob: 3b2db09ed41a900a851bfa7f50b406adcfe9c11a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
/*
   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/chat.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;
// last controlled entity
unsigned int last_control = 0;

void init()
{
	con_print << "Initializing input..." << std::endl;
	client::setkeyboardmode(console::visible());
//	SDL_EnableUNICODE(1);
}

void shutdown()
{
	con_print << "Shutting down input..." << std::endl;
//	SDL_EnableUNICODE(0);
}

// 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 - core::localcontrol()->direction()) > 90)
				local_turn = core::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 - core::localcontrol()->direction()) < -90)
				local_turn = core::localcontrol()->direction() - 90;
			if (local_turn < 0)
				local_turn += 360;
			break;
		default:
			break;
	}
	
}

void frame(float seconds)
{
	if (core::localcontrol() && (last_control != core::localcontrol()->id())) {
		local_turn = core::localcontrol()->direction();
		local_thrust = core::localcontrol()->thrust();
		last_control = core::localcontrol()->id();
	}

	SDL_Event event;
	
	while (SDL_PollEvent(&event)) {
	
		switch (event.type) {
			case SDL_KEYUP:
				if (!chat::visible() && !console::visible() && 
					core::application()->connected() && core::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 == '~') {
					last_control = 0;
					console::toggle();
					setkeyboardmode(console::visible());
					if (console::visible() && chat::visible())
						chat::toggle();
				} else if (console::visible()) {
					// send key events to the console
					console::keypressed(translate_keysym(event.key.keysym));

				} else if (chat::visible()) {
					if(event.key.keysym.sym == SDLK_ESCAPE) {
						chat::toggle();
						setkeyboardmode(chat::visible());
					} else {
						// send key events to the chatbox
						chat::keypressed(translate_keysym(event.key.keysym));
					}
				} else if(core::application()->connected()) {
					if ((event.key.keysym.sym == 't') || (event.key.keysym.sym == 'T')) {
						chat::toggle();
						setkeyboardmode(chat::visible());
					} else if (core::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() && core::localcontrol()) {
		core::localcontrol()->set_thrust(local_thrust);
		core::localcontrol()->set_direction(local_turn);
	}
}

} // namespace input

} // namespace client