blob: ff04ea06502344eef48c0e051863e7b86040f186 (
plain)
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
|
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Content;
using System.Security.Cryptography;
namespace SuperPolarity
{
class StandardShip : Ship
{
protected bool Flashing;
protected int ChangeRate;
protected int CurrentTime;
public int AngleChangeProbability;
protected int BouncePadding;
protected float RotationFactor;
protected Random Random;
protected bool AddingAngle;
public StandardShip(SuperPolarity newGame) : base(newGame) {}
public override void Initialize(Texture2D texture, Vector2 position)
{
base.Initialize(texture, position);
var cryptoResult = new byte[4];
new RNGCryptoServiceProvider().GetBytes(cryptoResult);
ChangeRate = 50;
AngleChangeProbability = 50;
BouncePadding = 0;
MaxVelocity = 3;
CurrentTime = 0;
RotationFactor = (float) (3 * Math.PI / 180);
Random = new Random(BitConverter.ToInt32(cryptoResult, 0));
AddingAngle = true;
}
public override void Magnetize(Ship ship, float distance, float angle)
{
if (ship.GetType() == typeof(MainShip)) {
base.Magnetize(ship, distance, angle);
}
}
public override void Update(GameTime gameTime)
{
CurrentTime += gameTime.ElapsedGameTime.Milliseconds;
if (!Magnetizing)
{
AutoMove();
BounceBack();
}
ChangeAngle();
Position += Velocity;
UpdateBox();
Magnetizing = false;
if (Flashing)
{
Color = new Color(255, 255, 255, 128);
Flashing = false;
}
else
{
Color = Color.White;
}
}
protected void AutoMove()
{
float newAngle = Angle;
if (CurrentTime < ChangeRate)
{
return;
}
if (Random.Next(AngleChangeProbability) == 2)
{
AddingAngle = !AddingAngle;
}
CurrentTime = 0;
if (AddingAngle)
{
newAngle += (float) ( Random.NextDouble() * RotationFactor);
}
else
{
newAngle -= (float) (Random.NextDouble() * RotationFactor);
}
Velocity.X = (float) (MaxVelocity * Math.Cos(newAngle));
Velocity.Y = (float) (MaxVelocity * Math.Sin(newAngle));
}
protected void BounceBack()
{
if (Position.X + Width < -BouncePadding && Velocity.X < 0)
{
Velocity.X = -Velocity.X;
}
if (Position.Y + Height < -BouncePadding && Velocity.Y < 0)
{
Velocity.Y = -Velocity.Y;
}
if (Position.X > game.GraphicsDevice.Viewport.Width + BouncePadding && Velocity.X > 0)
{
Velocity.X = -Velocity.X;
}
if (Position.Y > game.GraphicsDevice.Viewport.Height + BouncePadding && Velocity.Y > 0)
{
Velocity.Y = -Velocity.Y;
}
}
public override void Collide(Actor other, Rectangle collision)
{
if (Dying)
{
return;
}
if (other.GetType() == typeof(MainShip))
{
Die();
return;
}
if (other.GetType() == typeof(Bullet))
{
var theBullet = (Bullet)other;
TakeDamage(theBullet.Power);
Flashing = true;
}
}
protected override void Die()
{
ActorManager.CheckOut(this);
Renderer.CheckOut(this);
game.Player.AddScore(Value);
game.Player.AddMultiplier(1);
}
}
}
|