-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathChallenge.cpp
175 lines (137 loc) · 3.56 KB
/
Challenge.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
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
#include "Challenge.h"
#include "draw_shape.h"
#include "Draw.h"
Package::WeakGoalPtr Package::goal;
Package::WeakPackagePtr Package::pack;
const float Package::RADIUS_TO_START = 200;
const float Obsticle::SIZE = 15;
const float Goal::SIZE = 23;
Package::Package( const vector_type& pos, const vector_type& )
: Orbital( pos, vector_type(0,0), false )
{
activationDelay = 1;
started = false;
isMovable = false;
reachedGoal = false;
}
Package::State Package::on_off_screen( State s )
{
if( isActive )
return Orbital::on_off_screen( s );
return s;
}
void Package::move( float dt )
{
std::tr1::shared_ptr<Player> target = Orbital::target.lock();
if( !started && target &&
magnitude(target->s-s) < RADIUS_TO_START+target->radius() )
{
started = true;
isMovable = true;
isActive = true;
}
Orbital::move( dt );
}
void Package::draw()
{
Orbital::draw();
if( !started ) {
glTranslatef( s.x(), s.y(), 0 );
glColor3f( 1, 1, 1 );
const int N_VERTS = 40;
draw_loop( RADIUS_TO_START, RADIUS_TO_START + 10, N_VERTS );
glLoadIdentity();
}
}
Package::value_type Package::mass()
{
return 40;
}
Color Package::color()
{
return Color( 0.7, 0.5, 0.2, 1 );
}
void Package::collide_with( CircleActor& collider )
{
if( &collider == goal.lock().get() ) {
isMovable = false;
reachedGoal = true;
} else {
deleteMe = true;
}
}
Obsticle::Obsticle( const vector_type& pos, const vector_type& )
: Orbital( pos, vector_type(0,0), false )
{
isMovable = false;
isActive = true;
activationDelay = 0;
}
void Obsticle::move( float dt )
{
collisionChecked = false;
const float MAX_VEL = 0.55f;
if( magnitude( v ) > MAX_VEL )
v = magnitude( v, MAX_VEL );
s += v*dt;
state( Orbital::on_off_screen( state() ) );
}
Color Obsticle::color()
{
return Color( 1, 1, 1 );
}
Obsticle::value_type Obsticle::radius() const
{
return SIZE;
}
void Obsticle::collide_with( CircleActor& other )
{
if( collisionChecked )
return;
Obsticle* otherPtr = dynamic_cast<Obsticle*>( &other );
if( otherPtr )
otherPtr->collisionChecked = true;
isMovable = true;
other.isMovable = true;
Vector<float,2> diff = other.s - s;
float d = magnitude( diff );
float combRad = other.radius() + radius();
// Move them off each other.
s -= magnitude( diff, magnitude(diff) - combRad + 1.01 );
Vector<float,2> mtd = diff * (((radius() + other.radius())-d)/d);
Vector<float,2> vel = v - other.v;
float vn = vel * unit(mtd);
if( vn < 0 )
// They are moving away from each other. No extra work needed.
return;
float i = vn * ( mass() + other.mass() );
Vector<float,2> impulse = mtd * i;
v -= impulse / mass();
other.v += impulse / other.mass();
}
Goal::Goal( const vector_type& pos, const vector_type& )
: Obsticle( pos, vector_type(0,0) )
{
isMovable = false;
isActive = true;
activationDelay = 0;
}
Color Goal::color()
{
return Color( 0.3, 1.8, 0.8 );
}
Goal::value_type Goal::mass()
{
return 50;
}
Goal::value_type Goal::radius() const
{
return SIZE;
}
void Goal::collide_with( CircleActor& other )
{
if( &other == Package::pack.lock().get() )
deleteMe = true;
else
Obsticle::collide_with( other );
}