]> git.r.bdr.sh - rbdr/super-polarity/blob - Super Polarity/Actors/StandardShip.cs
50a671ad6196f9254aea6d6b9a632138a5def5ac
[rbdr/super-polarity] / Super Polarity / Actors / StandardShip.cs
1 using System;
2 using System.Collections.Generic;
3 using System.Linq;
4 using System.Text;
5 using Microsoft.Xna.Framework;
6 using Microsoft.Xna.Framework.Graphics;
7 using Microsoft.Xna.Framework.Content;
8 using System.Security.Cryptography;
9
10 namespace SuperPolarity
11 {
12 class StandardShip : Ship
13 {
14
15 protected int ChangeRate;
16 protected int CurrentTime;
17 protected int AngleChangeProbability;
18 protected int BouncePadding;
19 protected float RotationFactor;
20 protected Random Random;
21 protected bool AddingAngle;
22
23 public StandardShip(Game newGame) : base(newGame) {}
24
25 public override void Initialize(Texture2D texture, Vector2 position)
26 {
27 base.Initialize(texture, position);
28
29 var cryptoResult = new byte[4];
30 new RNGCryptoServiceProvider().GetBytes(cryptoResult);
31
32 ChangeRate = 50;
33 AngleChangeProbability = 50;
34 BouncePadding = 0;
35 MaxVelocity = 1;
36 CurrentTime = 0;
37 RotationFactor = (float) (3 * Math.PI / 180);
38 Random = new Random(BitConverter.ToInt32(cryptoResult, 0));
39 AddingAngle = true;
40 }
41
42 public override void Magnetize(Ship ship, float distance, float angle)
43 {
44 if (ship.GetType() == typeof(MainShip)) {
45 base.Magnetize(ship, distance, angle);
46 }
47 }
48
49 public override void Update(GameTime gameTime)
50 {
51 CurrentTime += gameTime.ElapsedGameTime.Milliseconds;
52 if (!Magnetizing)
53 {
54 AutoMove();
55 BounceBack();
56 }
57 ChangeAngle();
58 Position += Velocity;
59 Magnetizing = false;
60 }
61
62 protected void AutoMove()
63 {
64 float newAngle = Angle;
65
66 if (CurrentTime < ChangeRate)
67 {
68 return;
69 }
70
71 if (Random.Next(AngleChangeProbability) == 2)
72 {
73 AddingAngle = !AddingAngle;
74 }
75
76 CurrentTime = 0;
77
78 if (AddingAngle)
79 {
80 newAngle += (float) ( Random.NextDouble() * RotationFactor);
81 }
82 else
83 {
84 newAngle -= (float) (Random.NextDouble() * RotationFactor);
85 }
86
87 Velocity.X = (float) (MaxVelocity * Math.Cos(newAngle));
88 Velocity.Y = (float) (MaxVelocity * Math.Sin(newAngle));
89 }
90
91 protected void BounceBack()
92 {
93 if (Position.X + Width < -BouncePadding && Velocity.X < 0)
94 {
95 Velocity.X = -Velocity.X;
96 }
97
98 if (Position.Y + Height < -BouncePadding && Velocity.Y < 0)
99 {
100 Velocity.Y = -Velocity.Y;
101 }
102
103 if (Position.X > game.GraphicsDevice.Viewport.Width + BouncePadding && Velocity.X > 0)
104 {
105 Velocity.X = -Velocity.X;
106 }
107
108 if (Position.Y > game.GraphicsDevice.Viewport.Height + BouncePadding && Velocity.Y > 0)
109 {
110 Velocity.Y = -Velocity.Y;
111 }
112 }
113 }
114 }