-
Notifications
You must be signed in to change notification settings - Fork 3
/
RealPlayer.cpp
104 lines (89 loc) · 2.19 KB
/
RealPlayer.cpp
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
#include "RealPlayer.hpp"
#include <iostream>
using namespace std;
/*
RealPlayer::RealPlayer ( float width, float height )
{
init( width, height);
_number = 0;
}
*/
bool RealPlayer::update( const osgGA::GUIEventAdapter& ea, osg::Group* root )
{
bool accelBall = false;
osg::Vec3 pos = getMatrix().getTrans();
float halfW = width() * 0.5f, halfH = height() * 0.5f;
switch ( _number )
{
// TODO
case 1:
if ( ea.getEventType()==osgGA::GUIEventAdapter::KEYDOWN )
{
switch ( ea.getKey() )
{
// TODO: 0.2 should not be harcoded, but a property of the game class
case osgGA::GUIEventAdapter::KEY_Up:
_speedVec = osg::Vec3(0.0f, 0.2f, 0.0f);
break;
case osgGA::GUIEventAdapter::KEY_Down:
_speedVec = osg::Vec3(0.0f, -0.2f, 0.0f);
break;
// TODO: implement accelBall?
case osgGA::GUIEventAdapter::KEY_Space:
accelBall = true;
break;
default: break;
}
}
else if ( ea.getEventType()==osgGA::GUIEventAdapter::KEYUP )
_speedVec = osg::Vec3();
if ( pos.x()+ _speedVec.x() <halfW || pos.x()+ _speedVec.x() >160-halfW ) // TODO: remove hardcoded 160
{
return false;
}
if ( pos.y()+ _speedVec.y() <halfH || pos.y()+ _speedVec.y() >90-halfH ) // TODO: remove harcoded 90
{
return false;
}
break;
// TODO: implement Player2 movement
case 2:
break;
default: break;
}
// only update for new frames
if ( ea.getEventType() !=osgGA::GUIEventAdapter::FRAME )
return true;
pos += _speedVec;
setMatrix( osg::Matrix::translate(pos) );
return true;
}
const int RealPlayer::getScore()
{
return _score.getScore();
}
void RealPlayer::incrScore()
{
_score.incrScore();
}
void RealPlayer::resetScore()
{
_score.resetScore();
}
bool RealPlayer::setPlayerNumber ( const int number )
{
_number = number;
/* if ( _number == 1 )
{
_scoreText.setScoreSide(PongScore::LEFT);
return true;
}
else if ( _number == 2 )
{
_scoreText.setScoreSide(PongScore::RIGHT);
return true;
}
else
return false;*/
return true;
}