]> git.r.bdr.sh - rbdr/super-polarity/blob - SuperPolarity/Actors/StandardShip.cs
Works in win, compiles in mac
[rbdr/super-polarity] / SuperPolarity / 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 bool Flashing;
16 protected int ChangeRate;
17 protected int CurrentTime;
18 public int AngleChangeProbability;
19 protected int BouncePadding;
20 protected float RotationFactor;
21 protected Random Random;
22 protected bool AddingAngle;
23
24 public StandardShip(SuperPolarity newGame) : base(newGame) {}
25
26 public override void Initialize(Texture2D texture, Vector2 position)
27 {
28 base.Initialize(texture, position);
29
30 var cryptoResult = new byte[4];
31 new RNGCryptoServiceProvider().GetBytes(cryptoResult);
32
33 ChangeRate = 50;
34 AngleChangeProbability = 50;
35 BouncePadding = 0;
36 MaxVelocity = 2;
37 CurrentTime = 0;
38 RotationFactor = (float) (3 * Math.PI / 180);
39 Random = new Random(BitConverter.ToInt32(cryptoResult, 0));
40 AddingAngle = true;
41 }
42
43 public override void Magnetize(Ship ship, float distance, float angle)
44 {
45 if (ship.GetType() == typeof(MainShip)) {
46 base.Magnetize(ship, distance, angle);
47 }
48 }
49
50 public override void Update(GameTime gameTime)
51 {
52 CurrentTime += gameTime.ElapsedGameTime.Milliseconds;
53 if (!Magnetizing)
54 {
55 AutoMove();
56 BounceBack();
57 }
58 ChangeAngle();
59 Position += Velocity;
60 UpdateBox();
61 Magnetizing = false;
62
63 if (Flashing)
64 {
65 Color = new Color(255, 255, 255, 128);
66 Flashing = false;
67 }
68 else
69 {
70 Color = Color.White;
71 }
72 }
73
74 protected void AutoMove()
75 {
76 float newAngle = Angle;
77
78 if (CurrentTime < ChangeRate)
79 {
80 return;
81 }
82
83 if (Random.Next(AngleChangeProbability) == 2)
84 {
85 AddingAngle = !AddingAngle;
86 }
87
88 CurrentTime = 0;
89
90 if (AddingAngle)
91 {
92 newAngle += (float) ( Random.NextDouble() * RotationFactor);
93 }
94 else
95 {
96 newAngle -= (float) (Random.NextDouble() * RotationFactor);
97 }
98
99 Velocity.X = (float) (MaxVelocity * Math.Cos(newAngle));
100 Velocity.Y = (float) (MaxVelocity * Math.Sin(newAngle));
101 }
102
103 protected void BounceBack()
104 {
105 if (Position.X + Width < -BouncePadding && Velocity.X < 0)
106 {
107 Velocity.X = -Velocity.X;
108 }
109
110 if (Position.Y + Height < -BouncePadding && Velocity.Y < 0)
111 {
112 Velocity.Y = -Velocity.Y;
113 }
114
115 if (Position.X > game.GraphicsDevice.Viewport.Width + BouncePadding && Velocity.X > 0)
116 {
117 Velocity.X = -Velocity.X;
118 }
119
120 if (Position.Y > game.GraphicsDevice.Viewport.Height + BouncePadding && Velocity.Y > 0)
121 {
122 Velocity.Y = -Velocity.Y;
123 }
124 }
125
126 public override void Collide(Actor other, Rectangle collision)
127 {
128 if (Dying)
129 {
130 return;
131 }
132
133 if (other.GetType() == typeof(MainShip))
134 {
135 Die();
136 return;
137 }
138
139 if (other.GetType() == typeof(Bullet))
140 {
141 var theBullet = (Bullet)other;
142 TakeDamage(theBullet.Power);
143 Flashing = true;
144 }
145 }
146
147 protected override void Die()
148 {
149 ActorManager.CheckOut(this);
150 Renderer.CheckOut(this);
151 game.Player.AddScore(Value);
152 game.Player.AddMultiplier(1);
153 }
154 }
155 }