My favorites | Sign in
Project Home Downloads Wiki Issues Source
Checkout   Browse   Changes    
 
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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Engine;

namespace Editor
{
class PlayerCharacter
{
enum Direction
{
Up,
Down,
Left,
Right,
}
Direction _direction = Direction.Down;

AnimatedSprite _sprite = new AnimatedSprite();
TextureManager _textureManager;
public bool FollowingPath { get; set; }

public PlayerCharacter(TextureManager textureManager)
{
_textureManager = textureManager;
FollowingPath = false;
SetToWalkUp();
}

// Take a normalized vector and return the direction it's facing.
// There's a quite of lot of operations here I'm sure it could be made more efficent.
private Direction VectorToDirection(Vector direction)
{
Vector up = new Vector(0, 1, 0);
Vector down = new Vector(0, -1, 0);
Vector left = new Vector(-1, 0, 0);
Vector right = new Vector(1, 0, 0);

double upDiff = Math.Acos(direction.DotProduct(up));
double downDiff = Math.Acos(direction.DotProduct(down));
double leftDiff = Math.Acos(direction.DotProduct(left));
double rightDiff = Math.Acos(direction.DotProduct(right));

double smallest = Math.Min(Math.Min(upDiff, downDiff), Math.Min(leftDiff, rightDiff));

// yes there's a precidence if they're the same value, it doesn't matter
if (smallest == upDiff)
{
return Direction.Up;
}
else if (smallest == downDiff)
{
return Direction.Down;
}
else if (smallest == leftDiff)
{
return Direction.Left;
}
else
{
return Direction.Right;
}
}

private void SetToIdle()
{
_sprite.Texture = _textureManager.Get("pc_stand_still");
_sprite.SetAnimation(3, 1);
_sprite.SetWidth(_sprite.Texture.Width / 3);
_sprite.SetHeight(_sprite.Texture.Height);
_sprite.FlipHorizontal = false;
_sprite.Pause = true;
}

private void SetToWalkUp()
{
_sprite.Texture = _textureManager.Get("pc_walk_up");
_sprite.SetAnimation(5, 2);
_sprite.SetWidth(_sprite.Texture.Width / 5);
_sprite.SetHeight(_sprite.Texture.Height / 2);
_sprite.Looping = true;
_sprite.Pause = false;
_sprite.Finished = false;
_sprite.FlipHorizontal = false;
_sprite.Speed = 0.1;
}

private void SetToWalkDown()
{
_sprite.Texture = _textureManager.Get("pc_walk_front");
_sprite.SetAnimation(5, 2);
_sprite.SetWidth(_sprite.Texture.Width / 5);
_sprite.SetHeight(_sprite.Texture.Height / 2);
_sprite.Looping = true;
_sprite.Pause = false;
_sprite.Finished = false;
_sprite.FlipHorizontal = false;
_sprite.Speed = 0.1;
}


private void SetToWalkRight()
{
_sprite.Texture = _textureManager.Get("pc_walk_side");
_sprite.SetAnimation(5, 2);
_sprite.SetWidth(_sprite.Texture.Width / 5);
_sprite.SetHeight(_sprite.Texture.Height / 2);
_sprite.Looping = true;
_sprite.Pause = false;
_sprite.Finished = false;
_sprite.FlipHorizontal = false;
_sprite.Speed = 0.1;
}

private void SetToWalkLeft()
{
_sprite.Texture = _textureManager.Get("pc_walk_side");
_sprite.SetAnimation(5, 2);
_sprite.SetWidth(_sprite.Texture.Width / 5);
_sprite.SetHeight(_sprite.Texture.Height / 2);
_sprite.Looping = true;
_sprite.Pause = false;
_sprite.Finished = false;
_sprite.FlipHorizontal = true;
_sprite.Speed = 0.1;
}


public void FaceDown()
{
SetToIdle();
_sprite.SetFrame(1);
}

public void FaceUp()
{
SetToIdle();
_sprite.SetFrame(2);
}

public void FaceRight()
{
SetToIdle();
_sprite.SetFrame(0);
}

public void FaceLeft()
{
SetToIdle();
_sprite.FlipHorizontal = true;
_sprite.SetFrame(0);
}

public void Update(double elapsedTime)
{
if (FollowingPath)
{
UpdatePath(elapsedTime);
}
_sprite.Update(elapsedTime);

}

public void Render(Renderer renderer)
{
renderer.DrawSprite(_sprite);
}

/// <summary>
/// The player position should be defined as the bottom of the sprite in the middle.
/// </summary>
/// <param name="position"></param>
internal void SetPosition(Point position)
{
// floor position so at least if pixel resolution matches the won't wobble.
_sprite.SetPosition(Math.Floor(position.X), Math.Floor(position.Y + _sprite.GetHeight() / 2));
}

internal Point GetPosition()
{
Vector position = _sprite.GetPosition();
return new Point((float)position.X, (float)(position.Y - _sprite.GetHeight() / 2));
}


List<Point> _path;
int _currentPathIndex = 1;
double _walkSpeed = 75;
Vector _travelPosition; // keep accurate position here
internal void FollowPath(List<Point> path)
{

FollowingPath = true;
_path = path;
_travelPosition = new Vector(GetPosition().X, GetPosition().Y, 0);

// Setup the initial animation
Vector direction = Vector.Normalize(new Vector(path[0].X, path[0].Y, 0) - new Vector(path[1].X, path[1].Y, 0));
_direction = VectorToDirection(direction);
ChangeWalkingAnimation();


}

internal void UpdatePath(double elapsedTime)
{
System.Diagnostics.Debug.Assert(_currentPathIndex != _path.Count);

Vector position = _travelPosition;
Vector target = new Vector(_path[_currentPathIndex].X, _path[_currentPathIndex].Y, 0);
Vector fromPositionToTarget = target - position;
Vector direction = Vector.Normalize(fromPositionToTarget);

Direction walkDirection = VectorToDirection(direction);
if (_direction != walkDirection)
{
_direction = walkDirection;
ChangeWalkingAnimation();
}

Vector distanceToMove = (direction * _walkSpeed) * elapsedTime;

if (distanceToMove.Length() <= fromPositionToTarget.Length())
{
position += distanceToMove;
}
else
{
position = target;
}



_travelPosition = position;
SetPosition(new Point((float)position.X, (float)position.Y));

if ((target - position).Length() <= 1)
{
_currentPathIndex++;
if (_currentPathIndex == _path.Count)
{
_currentPathIndex = 1;
FollowingPath = false;
_path.Clear();
StopWalkingAnimation();
return;
}
}

}

private void StopWalkingAnimation()
{
if (_direction == Direction.Down)
{
FaceDown();
}
else if (_direction == Direction.Up)
{
FaceUp();
}
else if (_direction == Direction.Left)
{
FaceLeft();
}
else
{
FaceRight();
}
}

private void ChangeWalkingAnimation()
{
if (_direction == Direction.Up)
{
SetToWalkUp();
}
else if (_direction == Direction.Down)
{
SetToWalkDown();
}
else if (_direction == Direction.Left)
{
SetToWalkLeft();
}
else
{
SetToWalkRight();
}
}

/*
/// <summary>
/// Player position is a vector, it's being compared to floats
/// There's going to be accuracy lost of the compare method needs to have a tolerance.
/// </summary>
/// <param name="point"></param>
/// <returns></returns>
private bool HasReachedPoint(Point point)
{
float distance = Distance(_travelPosition, point);
return (distance <= 1); // within a pixel
}

private float Distance(Point point1, Point point2)
{
float xd = point2.X - point1.X;
float yd = point2.Y - point1.Y;
return (float)Math.Sqrt(xd*xd + yd*yd);

}
* */
}
}

Change log

r19 by balaam on Aug 11, 2010   Diff
... never added PlayerCharacter.cs oops.
Fixed a bug where if the time step is very
large the character could overshoot the
end of the path.
Fixed walking bug in the UpdatePath
function.
Go to: 
Project members, sign in to write a code review

Older revisions

All revisions of this file

File info

Size: 9739 bytes, 315 lines
Powered by Google Project Hosting