-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPowerup.cs
145 lines (109 loc) · 4.01 KB
/
Powerup.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
using System;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
namespace _3DAsteroids
{
class Powerup
{
public Model fuelModel;
public Model speedModel;
public Model multiShotModel;
public Constants.PowerUpType type;
public Quaternion rotation;
public Vector3 position;
Vector3 velocity;
Vector3 rotVelocity;
public bool isActive;
public Random rand;
public int outerLimit;
public Powerup(Random rand, int outLimit)
{
isActive = false;
this.rand = rand;
outerLimit = outLimit;
float Tempx = (float)rand.NextDouble() * 360; //Range from 0 to 360
float Tempy = (float)rand.NextDouble() * 360;
float Tempz = (float)rand.NextDouble() * 360;
rotation = Quaternion.CreateFromYawPitchRoll(Tempx, Tempy, Tempz);
velocity.X = (float)rand.NextDouble() * .05F - .025F; //Range from -0.025 to 0.025
velocity.Y = (float)rand.NextDouble() * .05F - .025F;
velocity.Z = (float)rand.NextDouble() * .05F - .025F;
rotVelocity.X = (float)rand.NextDouble() * 0.05F - 0.025F; //Range from -0.025 to 0.025
rotVelocity.Y = (float)rand.NextDouble() * 0.05F - 0.025F;
rotVelocity.Z = (float)rand.NextDouble() * 0.05F - 0.025F;
}
public void spawnPowerup()
{
position.X = (float)rand.NextDouble() * outerLimit * 2 - outerLimit; //In the playing area
position.Y = (float)rand.NextDouble() * outerLimit * 2 - outerLimit;
position.Z = (float)rand.NextDouble() * outerLimit * 2 - outerLimit;
type = (Constants.PowerUpType) rand.Next(0, 3);
isActive = true;
}
public void updatePosition(float delta)
{
position += velocity * delta;
checkOutOfBounds();
updateRotation();
}
public void updateRotation()
{
rotation *= Quaternion.CreateFromYawPitchRoll(rotVelocity.X, rotVelocity.Y, rotVelocity.Z);
}
public void checkOutOfBounds()
{
if (position.X > outerLimit)
{
position -= velocity;
velocity = Vector3.Reflect(velocity, Vector3.Right);
}
if (position.X < -outerLimit)
{
position -= velocity;
velocity = Vector3.Reflect(velocity, Vector3.Left);
}
if (position.Y > outerLimit)
{
position -= velocity;
velocity = Vector3.Reflect(velocity, Vector3.Up);
}
if (position.Y < -outerLimit)
{
position -= velocity;
velocity = Vector3.Reflect(velocity, Vector3.Down);
}
if (position.Z > outerLimit)
{
position -= velocity;
velocity = Vector3.Reflect(velocity, Vector3.Backward);
}
if (position.Z < -outerLimit)
{
position -= velocity;
velocity = Vector3.Reflect(velocity, Vector3.Forward);
}
}
public Matrix getWorldMatrix()
{
return Matrix.CreateFromQuaternion(rotation) * Matrix.CreateTranslation(position);
}
public void setModel(Model fuel, Model multi, Model speed)
{
this.fuelModel = fuel;
this.multiShotModel = multi;
this.speedModel = speed;
}
public Model getModel()
{
if (type == Constants.PowerUpType.Fuel) return fuelModel;
else if (type == Constants.PowerUpType.MultiShot) return multiShotModel;
return speedModel;
//if (type == Constants.PowerUpType.Speed) return speedModel;
}
public Constants.PowerUpType PickUp()
{
isActive = false;
return type;
}
}
}