]> git.r.bdr.sh - rbdr/super-polarity/blob - Super Polarity/Actors/StandardShip.cs
556be0f413b5a96f5a178f37bbf2e1cbb7d9693b
[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(SuperPolarity 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 HP = 5;
42 }
43
44 public override void Magnetize(Ship ship, float distance, float angle)
45 {
46 if (ship.GetType() == typeof(MainShip)) {
47 base.Magnetize(ship, distance, angle);
48 }
49 }
50
51 public override void Update(GameTime gameTime)
52 {
53 CurrentTime += gameTime.ElapsedGameTime.Milliseconds;
54 if (!Magnetizing)
55 {
56 AutoMove();
57 BounceBack();
58 }
59 ChangeAngle();
60 Position += Velocity;
61 UpdateBox();
62 Magnetizing = false;
63 }
64
65 protected void AutoMove()
66 {
67 float newAngle = Angle;
68
69 if (CurrentTime < ChangeRate)
70 {
71 return;
72 }
73
74 if (Random.Next(AngleChangeProbability) == 2)
75 {
76 AddingAngle = !AddingAngle;
77 }
78
79 CurrentTime = 0;
80
81 if (AddingAngle)
82 {
83 newAngle += (float) ( Random.NextDouble() * RotationFactor);
84 }
85 else
86 {
87 newAngle -= (float) (Random.NextDouble() * RotationFactor);
88 }
89
90 Velocity.X = (float) (MaxVelocity * Math.Cos(newAngle));
91 Velocity.Y = (float) (MaxVelocity * Math.Sin(newAngle));
92 }
93
94 protected void BounceBack()
95 {
96 if (Position.X + Width < -BouncePadding && Velocity.X < 0)
97 {
98 Velocity.X = -Velocity.X;
99 }
100
101 if (Position.Y + Height < -BouncePadding && Velocity.Y < 0)
102 {
103 Velocity.Y = -Velocity.Y;
104 }
105
106 if (Position.X > game.GraphicsDevice.Viewport.Width + BouncePadding && Velocity.X > 0)
107 {
108 Velocity.X = -Velocity.X;
109 }
110
111 if (Position.Y > game.GraphicsDevice.Viewport.Height + BouncePadding && Velocity.Y > 0)
112 {
113 Velocity.Y = -Velocity.Y;
114 }
115 }
116
117 public override void Collide(Actor other, Rectangle collision)
118 {
119 if (Dying)
120 {
121 return;
122 }
123
124 if (other.GetType() == typeof(MainShip))
125 {
126 Die();
127 return;
128 }
129
130 if (other.GetType() == typeof(Bullet))
131 {
132 var theBullet = (Bullet)other;
133 TakeDamage(theBullet.Power);
134 }
135 }
136
137 protected override void Die()
138 {
139 ActorManager.CheckOut(this);
140 Renderer.CheckOut(this);
141 game.Player.AddScore(Value);
142 game.Player.AddMultiplier(1);
143 }
144 }
145 }