-
Notifications
You must be signed in to change notification settings - Fork 0
/
moveVehicles.cs
193 lines (171 loc) · 6.63 KB
/
moveVehicles.cs
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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
namespace Frogger
{
public enum Direction
{
North,
South,
East,
West
}
class MovingItemList
{
private MovingItem[] car;
private int carCount = 50; // number of cars
private Direction direction; // direction they are moving
private Rectangle screen; // screen they are drawn on
private float speed;
static Random rnd = new Random();
/// <summary>
/// Create a list of moving items
/// </summary>
/// <param name="image">The image for the cars. All cars use the same image.</param>
/// <param name="position">The location of the first car in the list.</param>
/// <param name="theSpeed">The number of pixels per tick to move.</param>
/// <param name="theDirection">The direction the cars are moving.</param>
/// <param name="screen">The screen on which the cars will be drawn.</param>
public MovingItemList(Texture2D image, Rectangle position, float theSpeed, Direction theDirection, Rectangle theScreen)
{
// Console.WriteLine(screen.Right);
speed = theSpeed;
direction = theDirection;
screen = theScreen;
car = new MovingItem[carCount];
// Cars moving West (left) will start with car[0] on the left edge of the screen.
int gap = rnd.Next(3, 7);
if (direction == Direction.West)
for (int i = 0; i < carCount; i++)
{
position.X = i * position.Width;
car[i] = new MovingItem(image, position, -speed);
car[i].Visible = false;
gap--;
if (gap == 0)
{
car[i].Visible = true;
gap = rnd.Next(3, 9);
}
//Console.WriteLine(i + " " + position + " " + theSpeed);
}
// Cars moving East (right) will start with car[0] on the right edge of the screen.
else if (direction == Direction.East)
for (int i = 0; i < carCount; i++)
{
position.X = screen.Right - i * position.Width;
car[i] = new MovingItem(image, position, speed);
car[i].Visible = false;
gap--;
if (gap == 0)
{
car[i].Visible = true;
gap = rnd.Next(3, 9);
}
}
}
/// <summary>
/// Return the speed of the items in the list.
/// </summary>
public float Speed
{
get{ return speed;}
}
public Direction Direction
{
get { return direction; }
}
/// <summary>
/// Return true if the frog has collided with any visible object on this list.
/// </summary>
/// <param name="frog">The sprite that we are testing to see if it has collided with anything on the list.</param>
/// <returns></returns>
public bool HasCollision(Frogger frog)
{
for (int i = 0; i < carCount; i++)
if (car[i].Visible)
if (car[i].Position.Intersects(frog.thePosition))
return true;
return false;
}
//public bool LandsOnLog(Frog frog)
//{
// for (int i = 0; i < carCount; i++)
// if (car[i].Visible)
// if (car[i].Position.Intersects(frog.Position))
// {
// frog.Speed = car[i].Speed;
// return true;
// }
// return false;
//}
//public bool FallsInRiver(Frog frog)
//{
// // The frog isn't even on this row!
// if (car[0].Position.Y != frog.Position.Y)
// return false;
// for (int i = 0; i < carCount; i++)
// if (car[i].Visible == false) // we're between logs/turtles
// if (car[i].Position.Intersects(frog.Position))
// return true;
// return false;
//}
/// <summary>
/// Return true if the frog has landed on any visible object on this list.
/// </summary>
/// <param name="frog">The sprite that we are testing to see if it has landed on anything on this list.</param>
/// <returns></returns>
//public bool HasLandedOn(Frog frog)
//{
// return HasCollision(frog);
//}
// Update the list by moving each list element
public void Update()
{
Rectangle s;
for (int i = 0; i < carCount; i++)
{
car[i].Move();
s = car[i].Position;
if (direction == Direction.West)
{
if (car[i].Position.Right < screen.Left)
{ // Move it around to the east end
//Console.WriteLine("{0} right edge is {1}", i, s.Right);
if (i != 0)
s.X = car[i - 1].Position.Left + car[i - 1].Position.Width;
else
s.X = car[carCount - 1].Position.Left + car[carCount - 1].Position.Width;
car[i].Position = s;
//Console.WriteLine("moved {0} to {1}.", i, car[i].Position);
}
}
if (direction == Direction.East)
{
if (car[i].Position.Left > screen.Right)
{
if (i != 0)
s.X = car[i - 1].Position.Left - car[i - 1].Position.Width;
else
s.X = car[carCount - 1].Position.Left - car[carCount - 1].Position.Width;
car[i].Position = s;
}
}
//Console.WriteLine("Moving " + car[i].Position);
}
}
public void Draw(SpriteBatch spriteBatch)
{
for (int i = 0; i < carCount; i++)
car[i].Draw(spriteBatch);
//Console.WriteLine(car[0].Position);
}
}
}