File: Player.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 (183 lines) | stat: -rw-r--r-- 4,848 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
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
172
173
174
175
176
177
178
179
180
181
182
183
/* -*- 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 "Player.h"
#include "ControlsArray.h"
#include <math.h>
#include "Team.h"

#define SPEED_FACT_CONST (5)

int Player::maxX() { return _team->xmax(); }
int Player::minX() { return _team->xmin(); }
int Player::minY() { return _team->ymin(); }

int Player::speedX()  {
  if ( (_speedX > 0) &&
       (_x < (_team->xmax() - _frames->width())) )
    return(_speedX);
  if ( (_speedX < 0) &&
       (_x > _team->xmin()) )
    return(_speedX);
  return(0);
}

bool Player::setState(pl_state_t st, bool force) {

  if ((_state == st) && !force) return false;
  
  _state = st;
  
  switch (st) {
  case  PL_STATE_JUMP:
    _currFrameB = configuration.playerFrameConf.playerJmpB - 1;
    _currFrameE = configuration.playerFrameConf.playerJmpE - 1;
    if ( (_currFrameB == _currFrameE) ||
	 (configuration.playerFrameConf.playerJmpP == 0) )
      _currStateFrameDelay = 0;
    else
      _currStateFrameDelay = configuration.playerFrameConf.playerJmpP/
	( _currFrameE - _currFrameB);
    break;
  case PL_STATE_STILL:
    _currFrameB = configuration.playerFrameConf.playerStillB - 1;
    _currFrameE = configuration.playerFrameConf.playerStillE - 1;
    if ( (_currFrameB == _currFrameE) ||
	 (configuration.playerFrameConf.playerStillP == 0) )
      _currStateFrameDelay = 0;
    else
      _currStateFrameDelay = configuration.playerFrameConf.playerStillP/
	( _currFrameE - _currFrameB);
    break;
  case PL_STATE_WALK:
    _currFrameB = configuration.playerFrameConf.playerRunB - 1;
    _currFrameE = configuration.playerFrameConf.playerRunE - 1;
    if ( (_currFrameB == _currFrameE) ||
	 (configuration.playerFrameConf.playerRunP == 0) )
      _currStateFrameDelay = 0;
    else
      _currStateFrameDelay = configuration.playerFrameConf.playerRunP/
	( _currFrameE - _currFrameB);
    break;
  }

  return true;
}

void Player::updateFrame(int ticks, bool changed) {
  _overallPassed += ticks;

  if ( changed ) {
    _overallPassed = 0;
    _frameIdx = _currFrameB;
  } else if ( _currStateFrameDelay &&
	      (_overallPassed > _currStateFrameDelay) ) {
    // update _frameIdx
    if ( ++_frameIdx > _currFrameE )
      _frameIdx = _currFrameB;
    _overallPassed = 0;
  }

}

/* Invoked by the network client in order to draw the proper
   animation frame */
void Player::updateClient(int ticks, pl_state_t st) {
  updateFrame(ticks, setState(st));
}

void Player::update(int ticks, ControlsArray *ca) {
  triple_t input = ca->getCommands(_plId);
  
  int dx = 0;
  bool firstTime = (_overallPassed == 0);

  if ( input.left ) 
    dx--;

  if ( input.right )
    dx++;

  _speedX = dx?(dx * _speed):0;

  _displx += (float) dx * (_speed * ticks / 1000.0); 

  if ( fabs(_displx) >= 1.0 ) {
    _x += (int) _displx;
    _displx -= (int) _displx;
  }

  if ( _x > (_team->xmax() - _frames->width()) )
    _x = (_team->xmax() - _frames->width());
  else if ( _x < _team->xmin() )
    _x = _team->xmin();

  if ( _y == GROUND_LEVEL() && input.jump ) {
    _speedY = -(configuration.SPEEDY);
  }

  if ( _y > GROUND_LEVEL() ) {
    _y = GROUND_LEVEL();
    _speedY = 0;
  }

  _disply = (float) (_speedY * SPEED_FACT_CONST * ((float) ticks / 1000.0));
  
  if ( fabs(_disply) >= 1.0 ) {
    _y += (int) _disply;
    _disply -= (int) _disply;
  }

  if ( _y < GROUND_LEVEL() )
    _speedY +=
      (float) (configuration.SPEEDY * SPEED_FACT_CONST * ticks) / 1000.0;
  
  int _oldState = _state;
  /* detect state changes */
  if ( _y < GROUND_LEVEL() ) {
    setState(PL_STATE_JUMP); // jumping
  } else if (firstTime || (!dx)) { // player still
    setState(PL_STATE_STILL, firstTime);
  } else if ( dx ) { // player running
    setState(PL_STATE_WALK);
  }

  updateFrame(ticks, (_oldState != _state));
}

void Player::draw(SDL_Surface * screen) {
  SDL_Rect rect;
  rect.x = _x;
  rect.y = _y;

  _frames->blit(_frameIdx, screen, &rect);
}

bool
Player::collidesWith(FrameSeq *fs, int idx, SDL_Rect *rect)
{
  SDL_Rect myRect;
  myRect.x = _x;
  myRect.y = _y;

  return(_frames->collidesWith(fs, _state, idx, &myRect, rect));
}