-
Notifications
You must be signed in to change notification settings - Fork 0
/
Ball.cs
211 lines (165 loc) · 5.26 KB
/
Ball.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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using System.IO;
namespace superProject
{
class Ball
{
protected Gaming parentGaming;
public Model model;
public Quaternion rotationQuaternion;
public Vector3 position;
public Vector3 velocity;
public float mass;
public BoundingSphere boundingSphere;
public Vector3 Home;
public Vector3 rotation;
public float angle;
public float radius = 1.0f;
public enum Material {Marble, Plastic, Stone, Idle};
public Dictionary<Material, Texture2D> textures;
public Material currentMaterial;
float maxVelocity = 60.0f;
public int lives;
public int score;
public int keysLeft = 0;
public bool justDied;
float minHeight;
public Ball(Model model, Gaming parentGaming)
{
this.parentGaming = parentGaming;
this.model = model;
this.boundingSphere = new BoundingSphere(this.position, this.radius);
this.rotationQuaternion = Quaternion.Identity;
this.velocity = new Vector3(0, 0, 0);
this.textures = new Dictionary<Material,Texture2D>();
setMaterial(Material.Marble);
}
public void setData(int lives, int score, float minHeight)
{
this.lives = lives;
this.score = score;
this.minHeight = minHeight;
}
public void setPosition(Vector3 position){
this.Home = this.position = position;
}
public bool applyBonus(Bonus.BonusType type, Vector3 bonusPosition)
{
if (type == Bonus.BonusType.End)
{
if (keysLeft > 0)
{
return false;
}
parentGaming.levelCleared();
}
if (type == Bonus.BonusType.Key)
{
this.keysLeft--;
}
if (type == Bonus.BonusType.Live)
{
this.lives++;
}
if (type == Bonus.BonusType.Score)
{
this.score += 1000;
}
if (type == Bonus.BonusType.Save)
{
this.Home = bonusPosition;
}
return true;
}
public void setMaterial(Material material)
{
this.currentMaterial = material;
if (material == Material.Marble)
{
this.mass = 1f;
}
if (material == Material.Plastic)
{
this.mass = 0.5f;
}
if (material == Material.Stone)
{
this.mass = 5.0f;
}
}
public void applyForce(Vector3 force, GameTime time)
{
if (this.currentMaterial == Ball.Material.Marble)
{
force.Y = Math.Min(2.5f, force.Y);
}
var previousVelocity = this.velocity;
var timePassed = (float)time.ElapsedGameTime.TotalSeconds;
this.velocity += force / mass * timePassed;
if (this.currentMaterial == Ball.Material.Marble && this.velocity.Y > 0.5f && this.velocity.Y - previousVelocity.Y > 0)
{
this.velocity.Y = previousVelocity.Y;
}
if (this.velocity.Length() > maxVelocity )
{
this.velocity = previousVelocity;
}
}
public void applyImpuls(Vector3 impuls)
{
this.velocity = impuls / this.mass;
}
public void Reset()
{
this.position = this.Home;
this.stop();
this.boundingSphere.Center = this.position;
}
public void update(GameTime time)
{
this.position += this.velocity * (float)time.ElapsedGameTime.TotalSeconds;
this.boundingSphere.Center = this.position;
this.rotation = this.velocity * MathHelper.ToRadians(-1.5f);
this.angle += this.velocity.Length() /(-100.0f / this.radius);
Vector3 axis = Vector3.Cross(this.velocity, Vector3.Up);
if (axis != Vector3.Zero)
{
axis.Normalize();
}
rotationQuaternion = Quaternion.CreateFromAxisAngle(axis, angle);
this.score -= 1;
if (this.position.Y < minHeight)
{
this.die();
}
}
public void die()
{
this.Reset();
this.justDied = true;
this.lives--;
}
public bool isDead()
{
if (this.justDied)
{
this.justDied = false;
return true;
}
return false;
}
public void stop()
{
this.velocity = new Vector3(0, 0, 0);
}
}
}