/
PlayerController.cs
executable file
·323 lines (271 loc) · 10.1 KB
/
PlayerController.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
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
316
317
318
319
320
321
322
323
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 Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;
namespace Sputnik
{
class PlayerController : ShipController
{
private GameEnvironment m_env;
private const float timeBetweenControls = 0.0f;
private BlackHole.Pair m_playerBlackHoles;
private BlackHole.Pair previousBlackHoles;
private Tractorable itemBeingTractored;
private Ship controlled;
private static bool s_captureMouse = true;
private bool creatingBlackHole; // will make it so only 1 black hole can be created at a time.
private bool m_justTeleported = false;
private Cue m_tractorSound;
const float k_speed = 600.0f; // pixels per second
/// <summary>
/// Creates a new Player
/// </summary>
public PlayerController(GameEnvironment env)
{
m_env = env;
}
public void GotShotBy(Ship s, GameEntity f)
{
if (controlled is TriangulusShip)
{
foreach (TriangulusShip t in m_env.triangles)
{
t.ai.DistressCall(controlled, f);
}
}
else if (controlled is SquaretopiaShip)
{
foreach (SquaretopiaShip sq in m_env.squares)
{
sq.ai.DistressCall(controlled, f);
}
}
else if (controlled is CircloidShip)
{
foreach (CircloidShip c in m_env.circles)
{
c.ai.DistressCall(controlled, f);
}
}
}
public void GotTractored(GameEntity s)
{
//Chaos
}
public void GotFrozen(GameEntity s)
{
//Chaos
}
public void DistressCall(Ship s, GameEntity f)
{
//Player doesn't care about distress calls
}
public void HitWall(Vector2 collidePosition)
{
//Players dont care if they hit the wall
}
public bool IsAlliedWithPlayer()
{
//Of coruse the player is allied with the player
return true;
}
public bool IsDisabled()
{
return false; //Currently this is impossible, so yeah
}
public void gotDetached()
{
//Sputnik no care
}
/// <summary>
/// Ship was just teleported through a blackhole.
/// </summary>
/// <param name="blackhole">Start blackhole.</param>
/// <param name="destination">Position of destination.</param>
public void Teleport(BlackHole blackhole, Vector2 destination) {
m_justTeleported = true;
}
private void CancelTractorBeam() {
// Release entity.
itemBeingTractored.TractorReleased();
itemBeingTractored = null;
// Stop Sound.
if (m_tractorSound != null) m_tractorSound.Stop(AudioStopOptions.AsAuthored);
m_tractorSound = null;
}
/// <summary>
/// Updates the State of a ship
/// </summary>
public void Update(Ship s, float elapsedTime) {
const float k_aimRadius = 250.0f;
s.ResetMaxRotVel();
controlled = s;
GamePadState gamepad = GamePad.GetState(PlayerIndex.One);
// Get keyboard state.
Vector2 movement = Vector2.Zero;
Vector2 aimPosition = Vector2.Zero;
float aimDirection;
Vector2 specialPosition = Vector2.Zero;
float specialDirection;
bool detachPressed = false;
bool useSpecialPressed = false;
bool useSpecialHeld = false;
bool shoot = false;
if (!gamepad.IsConnected) {
KeyboardState keyboard = Keyboard.GetState();
MouseState mouse = Mouse.GetState();
MouseState oldMouse = OldMouse.GetState();
if (keyboard.IsKeyDown(Keys.W)) movement.Y -= 1.0f;
if (keyboard.IsKeyDown(Keys.A)) movement.X -= 1.0f;
if (keyboard.IsKeyDown(Keys.S)) movement.Y += 1.0f;
if (keyboard.IsKeyDown(Keys.D)) movement.X += 1.0f;
// Aiming.
Vector2 mousePos = m_env.Camera.ScreenToWorld(new Vector2(mouse.X, mouse.Y)) - m_env.Camera.Position;
if (mousePos.Length() > k_aimRadius) {
mousePos.Normalize();
mousePos *= k_aimRadius;
}
specialPosition = Vector2.Normalize(mousePos) * k_aimRadius + s.Position;
aimDirection = Angle.Direction(Vector2.Zero, mousePos);
specialDirection = aimDirection;
m_env.HUD.Cursor.Rotation = specialDirection + MathHelper.PiOver2;
m_env.HUD.Cursor.Position = mousePos / 3.0f + m_env.Camera.WorldToScreen(s.Position);
m_env.HUD.Cursor.Visible = true;
{
// Reset mouse position to center.
Vector2 screenCenter = m_env.Camera.WorldToScreen(m_env.Camera.Position + mousePos);
if (s_captureMouse) Mouse.SetPosition((int) Math.Round(screenCenter.X), (int) Math.Round(screenCenter.Y));
m_env.Controller.IsMouseVisible = !s_captureMouse;
}
// Detach.
detachPressed = (keyboard.IsKeyDown(Keys.Space) && !OldKeyboard.GetState().IsKeyDown(Keys.Space));
// Use special.
useSpecialHeld = (mouse.RightButton == ButtonState.Pressed && oldMouse.RightButton == ButtonState.Pressed);
useSpecialPressed = (mouse.RightButton == ButtonState.Pressed && oldMouse.RightButton != ButtonState.Pressed);
// Shoot.
shoot = (mouse.LeftButton == ButtonState.Pressed);
// Debug. F3 toggles mouse capture.
if (keyboard.IsKeyDown(Keys.F3) && !OldKeyboard.GetState().IsKeyDown(Keys.F3)) {
s_captureMouse = !s_captureMouse;
}
} else {
GamePadState oldGamepad = OldGamePad.GetState();
Vector2 invertY = new Vector2(1.0f, -1.0f);
// Gamepad.
movement = gamepad.ThumbSticks.Left * invertY;
aimDirection = Angle.Direction(Vector2.Zero, gamepad.ThumbSticks.Right * k_aimRadius * invertY);
specialPosition = Angle.Vector(s.DesiredRotation) * k_aimRadius + s.Position;
specialDirection = s.DesiredRotation;
m_env.HUD.Cursor.Rotation = aimDirection + MathHelper.PiOver2;
m_env.HUD.Cursor.Position = Angle.Vector(aimDirection) * k_aimRadius / 3.0f + m_env.Camera.WorldToScreen(s.Position);
// Detach.
detachPressed = (gamepad.IsButtonDown(Buttons.B) && !oldGamepad.IsButtonDown(Buttons.B));
// Use special.
useSpecialHeld = (gamepad.IsButtonDown(Buttons.LeftShoulder) && oldGamepad.IsButtonDown(Buttons.LeftShoulder))
|| (gamepad.IsButtonDown(Buttons.RightShoulder) && oldGamepad.IsButtonDown(Buttons.RightShoulder));
useSpecialPressed = (gamepad.IsButtonDown(Buttons.LeftShoulder) && !oldGamepad.IsButtonDown(Buttons.LeftShoulder))
|| (gamepad.IsButtonDown(Buttons.RightShoulder) && !oldGamepad.IsButtonDown(Buttons.RightShoulder));
// Shoot.
shoot = (gamepad.ThumbSticks.Right.Length() > 0.1f);
m_env.HUD.Cursor.Visible = shoot;
}
// Act on input.
s.shooterRotation = aimDirection;
if(!(s is TriangulusShip)) {
if (itemBeingTractored != null)
{
CancelTractorBeam();
}
}
// Detach from ship.
if (detachPressed)
{
s.Detach();
// If i am tractoring something, and i detach from it, then they should go back to normal.
if(itemBeingTractored != null) {
CancelTractorBeam();
}
}
// The item I am tractoring died.
if (itemBeingTractored != null && !itemBeingTractored.IsTractored) {
CancelTractorBeam();
}
// Update tractored entity's position.
if (itemBeingTractored != null) {
if (m_justTeleported) ((Entity) itemBeingTractored).Position = specialPosition;
itemBeingTractored.UpdateTractor(specialPosition);
TractorBeamModifier.Position = s.Position;
m_env.TractorBeamEffect.Trigger(((Entity) itemBeingTractored).Position);
}
m_justTeleported = false;
s.DesiredVelocity = (movement != Vector2.Zero) ? Vector2.Normalize(movement) * s.maxSpeed : Vector2.Zero;
if (s.DesiredVelocity != Vector2.Zero) {
s.DesiredRotation = Angle.Direction(Vector2.Zero, s.DesiredVelocity);
}
// need to check if sputnik is in a ship or not before you can shoot.
if (shoot) s.Shoot(elapsedTime);
if(useSpecialHeld) {
if(s is CircloidShip) {
if (!creatingBlackHole) {
m_playerBlackHoles = BlackHole.CreatePair(m_env, specialPosition);
creatingBlackHole = true;
}
if(creatingBlackHole && !((BlackHole)m_playerBlackHoles.First.Entity).fullyFormed) {
m_playerBlackHoles.First.Entity.Position = specialPosition;
}
// This code will execute if you succeed in creating the black hole.
if(((BlackHole)m_playerBlackHoles.First.Entity).fullyFormed) {
if (previousBlackHoles != null) previousBlackHoles.Destroy();
previousBlackHoles = m_playerBlackHoles;
creatingBlackHole = false;
}
}
} else {
if(creatingBlackHole) {
m_playerBlackHoles.Destroy();
}
creatingBlackHole = false;
}
// Will spawn a blackhole when we first pressdown our right mouse button.
// if a blackhole has already been spawned this way, then the other one will be removed.
if(useSpecialPressed) {
if(s is TriangulusShip) {
// if we are tractoring something right now, then we arent allowed to tractor anything else
// we can shoot now.
if (itemBeingTractored == null) {
List<Entity> list = VisionHelper.FindAll(m_env, s.Position, specialDirection, MathHelper.ToRadians(120.0f), 500.0f);
IOrderedEnumerable<Entity> sortedList = list.OrderBy(ent => Vector2.DistanceSquared(s.Position, ent.Position));
SquaretopiaShip ship = null;
Entity collided = sortedList.FirstOrDefault(ent =>
{
if (ent is TakesDamage && ((TakesDamage) ent).IsFriendly()) return false;
if(ent is SquaretopiaShip) ship = (SquaretopiaShip)ent;
return (ent is Tractorable);
});
if(collided is Tractorable) {
((Tractorable)collided).Tractored(s); // Disable ship
itemBeingTractored = (Tractorable) collided;
m_tractorSound = Sound.PlayCue("tractor_beam");
}
if(ship != null) {
ship.TakeHit(0);
}
} else {
CancelTractorBeam();
}
} else if(s is SquaretopiaShip) {
ForceField ff = new ForceField(m_env, s.shooter.Position, specialDirection, controlled);
m_env.AddChild(ff);
}
}
}
}
}