Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/orxonox/branches/particle_system/src/command_node.cc @ 3378

Last change on this file since 3378 was 3236, checked in by patrick, 20 years ago

/orxonox/trunk: unstable - problems getting collision to compile

File size: 6.4 KB
Line 
1/*
2   orxonox - the future of 3D-vertical-scrollers
3
4   Copyright (C) 2004 orx
5
6   This program is free software; you can redistribute it and/or modify
7   it under the terms of the GNU General Public License as published by
8   the Free Software Foundation; either version 2, or (at your option)
9   any later version.
10
11   ### File Specific:
12   main-programmer: Christian Meyer
13   co-programmer: Patrick Boenzli
14*/
15
16
17#include "command_node.h"
18#include "keynames.h"
19#include "ini_parser.h"
20#include "world_entity.h"
21#include "game_loader.h"
22#include "world.h"
23
24#include <stdio.h>
25#include <string.h>
26#include <stdlib.h>
27
28using namespace std;
29
30/**
31   \brief constructs a CommandNode to handle remote input
32   \param ID: unique denumerator to identify the node in the network
33*/
34CommandNode::CommandNode (int ID)
35{
36  this->bound = new tList<WorldEntity>();
37  this->aliases = NULL;
38  this->netID = ID;
39  this->bLocalInput = false;
40  this->bEnabled = true;
41  this->world = NULL;
42}
43
44/**
45   \brief constructs a CommandNode to handle local input
46   \param filename: The path and name of the file to load the key bindings from
47*/
48CommandNode::CommandNode (char* filename = DEFAULT_KEYBIND_FILE)
49{
50  this->aliases = NULL;
51  this->bLocalInput = true;
52  this->netID = 0;
53  this->bound = new tList<WorldEntity>();
54  this->bEnabled = true;
55  this->world = NULL;
56  this->loadBindings (filename);
57}
58
59/**
60   \brief removes the CommandNode from memory
61*/
62CommandNode::~CommandNode ()
63{
64  if( aliases != NULL) free (aliases);
65  if( bound != NULL) delete bound; /* \todo should this delete bound? dangerous FIX */
66}
67
68
69/**
70  \brief this resets the command node
71
72   deleting all data contained in the command node to fill it up again
73
74  \todo coppling to different game-entities
75  \todo reset/destroy has to be redesigned
76*/
77
78void CommandNode::reset()
79{
80  this->bound->destroy();
81  //this->bound = NULL; /* \todo this produces a NULLpointer error.. FIX */
82  this->bEnabled = false;
83  this->world = NULL;
84}
85
86void CommandNode::enable(bool bEnabled)
87{
88  this->bEnabled = bEnabled;
89}
90
91
92/**
93  \brief adds Node to a GameWorld
94
95   this is usefull, if you want to catch events in a world class. usualy
96   this is done automaticaly via GameLoader. Reset it via
97   CommandNode::reset()
98
99*/
100void CommandNode::addToWorld(World* world)
101{
102  this->world = world;
103}
104
105
106/**
107   \brief loads new key bindings from a file
108   \param filename: The path and name of the file to load the bindings from
109*/
110void CommandNode::loadBindings (char* filename)
111{
112  FILE* stream;
113 
114  printf("Loading key bindings from %s\n", filename);
115 
116  if( filename == NULL) filename = DEFAULT_KEYBIND_FILE;
117 
118  // remove old bindings if present
119  if( aliases != NULL)
120    {
121      free (aliases);
122      aliases = NULL;
123    }
124 
125  // create parser
126  IniParser parser (filename);
127  if( parser.getSection ("Bindings") == -1)
128    {
129      printf("Could not find key bindings in %s\n", filename);
130      return;
131    }
132  // allocate empty lookup table
133  aliases = (KeyBindings*) calloc (1, sizeof (KeyBindings));
134 
135  char namebuf[256];
136  char valuebuf[256];
137  memset (namebuf, 0, 256);
138  memset (valuebuf, 0, 256);
139  int* index;
140 
141  while( parser.nextVar (namebuf, valuebuf) != -1)
142    {
143      index = nameToIndex (namebuf);
144      switch( index[0])
145        {
146        case 0:
147          printf("Key binding %d(%s) set to %s\n", index[1], SDLKToKeyname( index[1]), valuebuf);
148          strcpy (aliases->keys[index[1]], valuebuf);
149          break;
150        case 1:
151          printf("Button binding %d(%s) set to %s\n", index[1], SDLBToButtonname( index[1]), valuebuf);
152          strcpy (aliases->buttons[index[1]], valuebuf);
153          break;
154        default:
155          break;
156        }
157      memset (namebuf, 0, 256);
158      memset (valuebuf, 0, 256);
159    }
160}
161
162/**
163   \brief binds a WorldEntity to the CommandNode
164   \param entity: Pointer to the entity to bind
165*/
166void CommandNode::bind (WorldEntity* entity)
167{
168  bound->add (entity);
169}
170
171/**
172   \brief removes an entity from the list of the CommandNode
173   \param entity: Pointer to the entity to relese
174*/
175void CommandNode::unbind (WorldEntity* entity)
176{
177  bound->remove (entity);
178}
179
180int* CommandNode::nameToIndex (char* name)
181{
182  coord[0] = -1;
183  coord[1] = -1;
184  int c;
185  if( (c = keynameToSDLK (name)) != -1)
186    {
187      coord[1] = c;
188      coord[0] = 0;
189    }
190  if( (c = buttonnameToSDLB (name)) != -1)
191    {
192      coord[1] = c;
193      coord[0] = 1;
194    }
195  return coord;
196}
197
198/**
199   \brief tells the CommandNode to run through all pending events and relay them accordingly
200*/
201void CommandNode::process ()
202{
203  if( this->bEnabled) 
204    {
205      if( bLocalInput) processLocal ();
206      else processNetwork ();
207    }
208}
209
210void CommandNode::processLocal ()
211{
212  SDL_Event event;
213  Command cmd;
214  while( SDL_PollEvent (&event))
215    {
216      memset (cmd.cmd, 0, CMD_LENGHT); 
217      switch( event.type)
218        {
219        case SDL_KEYDOWN:
220          strcpy (cmd.cmd, aliases->keys[event.key.keysym.sym]);
221          cmd.bUp = false;
222          if( strlen (cmd.cmd) > 0) relay(&cmd);
223          break;
224        case SDL_KEYUP:
225          strcpy( cmd.cmd, aliases->keys[event.key.keysym.sym]);
226          cmd.bUp = true;
227          if( strlen (cmd.cmd) > 0) relay(&cmd);
228          break;
229        case SDL_MOUSEMOTION:
230          strcpy( cmd.cmd, "cursor");
231          cmd.x = event.motion.x;
232          cmd.y = event.motion.y;
233          cmd.xrel = event.motion.xrel;
234          cmd.yrel = event.motion.yrel;
235          break;
236        case SDL_MOUSEBUTTONUP:
237          strcpy( cmd.cmd, aliases->buttons[event.button.button]);
238          cmd.bUp = true;
239          if( strlen (cmd.cmd) > 0) relay(&cmd);
240          break;
241        case SDL_MOUSEBUTTONDOWN:
242          strcpy( cmd.cmd, aliases->buttons[event.button.button]);
243          cmd.bUp = false;
244          if( strlen (cmd.cmd) > 0) relay(&cmd);
245          break;
246        case SDL_JOYAXISMOTION:
247        case SDL_JOYBALLMOTION:
248        case SDL_JOYHATMOTION:
249        case SDL_JOYBUTTONDOWN:
250        case SDL_JOYBUTTONUP:
251          break;
252        default:
253          Orxonox *orx = Orxonox::getInstance();
254          orx->eventHandler(&event);
255          break;
256        }
257    }
258}
259
260
261void CommandNode::processNetwork ()
262{
263
264}
265
266
267void CommandNode::relay (Command* cmd)
268{
269
270  Orxonox *orx = Orxonox::getInstance();
271  if( orx->systemCommand (cmd)) return;
272
273  GameLoader* gl = GameLoader::getInstance();
274  if( gl->worldCommand(cmd)) return;
275
276  if( bLocalInput) sendOverNetwork (cmd);
277 
278  if( this->world->command(cmd)) return;
279
280  WorldEntity* entity = bound->enumerate();
281  while( entity != NULL)
282    {
283      entity->command (cmd);
284      entity = bound->nextElement();
285    }
286}
287
288
289/**
290   \brief sets the network identifier of the CommandNode
291   \param ID: the new ID to use
292*/
293void CommandNode::setNetID (int ID)
294{
295  netID = ID;
296}
297
298void CommandNode::sendOverNetwork (Command* cmd)
299{
300}
Note: See TracBrowser for help on using the repository browser.