File: ControlsArray.cpp

package info (click to toggle)
gav 0.9.0-3
  • links: PTS
  • area: main
  • in suites: bullseye, buster, jessie, jessie-kfreebsd, lenny, sid, squeeze, stretch, wheezy
  • size: 660 kB
  • ctags: 814
  • sloc: cpp: 4,401; makefile: 147; sh: 15
file content (100 lines) | stat: -rw-r--r-- 2,954 bytes parent folder | download | duplicates (2)
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
/* -*- C++ -*- */
/*
  GAV - Gpl Arcade Volleyball
  
  Copyright (C) 2002
  GAV team (http://sourceforge.net/projects/gav/)

  This program is free software; you can redistribute it and/or modify
  it under the terms of the GNU General Public License as published by
  the Free Software Foundation; either version 2 of the License, or
  (at your option) any later version.

  This program is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  GNU General Public License for more details.

  You should have received a copy of the GNU General Public License
  along with this program; if not, write to the Free Software
  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/

#include <stdlib.h>
#include <SDL.h>
#include "InputState.h"
#include "ControlsArray.h"
#include "vector"
#include "Player.h"
#include "Team.h"
#include "PlayerAI.h"
#ifndef NONET
#include "NetServer.h"
#endif

#define JOY_THRESHOLD (10000)

void ControlsArray::initializeControls() {
  _keyMapping[0].left_key = SDLK_z;
  _keyMapping[0].right_key = SDLK_c;
  _keyMapping[0].jump_key = SDLK_LSHIFT;
  
  _keyMapping[1].left_key = SDLK_LEFT;
  _keyMapping[1].right_key = SDLK_RIGHT;
  _keyMapping[1].jump_key = SDLK_UP;
  
  _keyMapping[2].left_key = SDLK_h;
  _keyMapping[2].right_key = SDLK_k;
  _keyMapping[2].jump_key = SDLK_u;

  _keyMapping[3].left_key = SDLK_s;
  _keyMapping[3].right_key = SDLK_f;
  _keyMapping[3].jump_key = SDLK_e;
  
  // _joyMapping[0] = SDL_JoystickOpen(0);
}

void ControlsArray::setControlsState(InputState *is, Team * tl, Team * tr) {
  Uint8 *keystate = is->getKeyState();
  int i = 0;
  std::vector<Player *> players;

  SDL_JoystickUpdate();

#ifndef NONET
  int player;
  char cmd;
  if (nets)
    while ( nets->ReceiveCommand(&player, &cmd) != -1 ) {
      _inputs[player].left = cmd & CNTRL_LEFT;
      _inputs[player].right = cmd & CNTRL_RIGHT;
      _inputs[player].jump = cmd & CNTRL_JUMP;
    }
#endif

  players = tl->players();

  for (i = 0; i < 2; i++) {
    for ( std::vector<Player *>::const_iterator it = players.begin();
	  it != players.end(); it++ ) {
      if ( (*it)->getCtrl() == PL_CTRL_HUMAN ) {
	int plId = (*it)->id();
	/* first check the keyboard */
	_inputs[plId].left  = keystate[_keyMapping[plId].left_key];
	_inputs[plId].right = keystate[_keyMapping[plId].right_key];
	_inputs[plId].jump  = keystate[_keyMapping[plId].jump_key];
	/* then check the joystick */
	SDL_Joystick *js = _joyMapping[plId];
	if ( js ) {
	  _inputs[plId].left |= (SDL_JoystickGetAxis(js, 0) < -JOY_THRESHOLD);
	  _inputs[plId].right |= (SDL_JoystickGetAxis(js, 0) > JOY_THRESHOLD);
	  _inputs[plId].jump |= SDL_JoystickGetButton(js, 0);
	}
      } else if ( (*it)->getCtrl() == PL_CTRL_AI ) {
	_inputs[(*it)->id()] = ((PlayerAI *) (*it))->planAction();
      }
    }
    players = tr->players();
  }
}