aboutsummaryrefslogtreecommitdiff
path: root/SuperPolarity/Actors/MainShip.cs
blob: 7eef0b7cb10fd2d0bd722bb9e9629eef8f9a4d02 (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
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
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Audio;

namespace SuperPolarity
{
    class MainShip : Ship
    {

        public static Color BlueColor;
        public static Color RedColor;

        protected bool Shooting;
        protected int ShotCooldown;

        protected float CurrentImmortalTime;
        protected float MaxImmortalTime;
        protected bool Flashing;

        protected SoundEffect PolarityChange;
        protected SoundEffect ShootSound;
        protected SoundEffect Hit;

		// Aura stuff
		protected Texture2D AuraPoint;
		protected double AuraRadius;
		protected double AuraAmplitude;
		protected double AuraFrequency;
		protected double AuraSubAmplitude;
		protected double AuraWaveSize;
		protected double AuraSubPhase;
		protected double AuraSubFrequency;
		public double[] EnemyModifications;
		public double MaxEnemyModification;
		public double EnemyModificationSpread;

        public MainShip(SuperPolarity newGame) : base(newGame) {}

        ~MainShip()
        {
        }

        public override void Initialize(Texture2D texture, Vector2 position)
        {
            base.Initialize(texture, position);

			MainShip.BlueColor = new Color(71, 182, 226);
            MainShip.RedColor = new Color(235, 160, 185);

			AuraPoint = CreateCircle (2);

			AuraRadius = 75;
			AuraAmplitude = 7;
			AuraFrequency = 3;
			AuraSubAmplitude = 7;
			AuraWaveSize = 3;
			AuraSubPhase = 0;
			AuraSubFrequency = 9 * Math.PI / 180;
			MaxEnemyModification = 20;
			EnemyModificationSpread = 10;
			EnemyModifications = new double[360];
			for ( int i = 0; i < EnemyModifications.Length; i++ ) {
				EnemyModifications[i] = 0;
			}

            PolarityChange = game.Content.Load<SoundEffect>("Sound\\polaritychange");
            ShootSound = game.Content.Load<SoundEffect>("Sound\\bullet");
            Hit = game.Content.Load<SoundEffect>("Sound\\hit");

            SetPolarity(Polarity.Positive);

            ActVelocity = 2.5f;

            ShotCooldown = 50;
            MaxImmortalTime = 1500;

            BoxDimensions.X = 2;
            BoxDimensions.Y = 2;
            BoxDimensions.W = 2;
            BoxDimensions.Z = 2;
            InitBox();

            BindInput();
        }

        void BindInput()
        {
            InputController.Bind("moveX", HandleHorizontalMovement);
            InputController.Bind("moveY", HandleVerticalMovement);
            InputController.Bind("changePolarity", HandleChangePolarity);
            InputController.Bind("shoot", HandleShot);
        }

        protected void HandleShot(float value)
        {

            Shooting = true;
            Timer t = new Timer(new TimerCallback(UnlockShot));
            t.Change(ShotCooldown, Timeout.Infinite);

            if (Children.Count > 10)
            {
                return;
            }

            var bullet = ActorFactory.CreateBullet(Position, Angle);

            Children.Add(bullet);
            bullet.Parent = this;

            ShootSound.Play();
        }

        protected void UnlockShot(object state)
        {
            InputController.Unlock("shoot");
            Shooting = false;
        }

        protected void HandleChangePolarity(float value)
        {
            SwitchPolarity();
        }

        public void HandleHorizontalMovement(float value)
        {
            if (value >= -0.5 && value <= 0.5)
            {
                value = 0;
            }

            Velocity.X = value * MaxVelocity;
        }

        public void HandleVerticalMovement(float value)
        {
            if (value >= -0.5 && value <= 0.5)
            {
                value = 0;
            }

            Velocity.Y = value * MaxVelocity;
        }

        public override void SwitchPolarity()
        {
            base.SwitchPolarity();
            PolarityChange.Play();
            game.Player.ResetMultiplier();
        }

        public override void SetPolarity(Polarity newPolarity)
        {
            base.SetPolarity(newPolarity);;
        }
			
        public override void Update(GameTime gameTime)
        {
            base.Update(gameTime);
            ConstrainToEdges();
            UpdateImmortality(gameTime);
        }

		public void ResetEnemyModification() {
			for ( int i = 0; i < EnemyModifications.Length; i++ ) {
				EnemyModifications[i] = 0;
			}
		}

        public void UpdateImmortality(GameTime gameTime)
        {
            if (Immortal)
            {
                CurrentImmortalTime += gameTime.ElapsedGameTime.Milliseconds;

                if (Flashing)
                {
                    Color = new Color(255, 255, 255, 128);
                }
                else
                {
                    Color = Color.White;
                }

                Flashing = !Flashing;

                if (CurrentImmortalTime > MaxImmortalTime)
                {
                    Immortal = false;
                    Color = Color.White;
                }
            }
        }

        public override void Move(GameTime gameTime)
        {
            var VelocityLimit = MaxVelocity;
            var SavedVelocity = new Vector2(Velocity.X, Velocity.Y);

            if (Shooting)
            {
                VelocityLimit = ActVelocity;
            }

            if (SavedVelocity.X > VelocityLimit)
            {
                SavedVelocity.X = VelocityLimit;
            }

            if (SavedVelocity.X < -VelocityLimit)
            {
                SavedVelocity.X = -VelocityLimit;
            }

            if (SavedVelocity.Y > VelocityLimit)
            {
                SavedVelocity.Y = VelocityLimit;
            }

            if (SavedVelocity.Y < -VelocityLimit)
            {
                SavedVelocity.Y = -VelocityLimit;
            }

            Position.X = Position.X + SavedVelocity.X;
            Position.Y = Position.Y + SavedVelocity.Y;
        }

        public override void Magnetize(Ship ship, float distance, float angle)
        {
        }

        protected void ConstrainToEdges()
        {
            if (Position.X < 0)
            { 
                Position.X = 0;

                if (Velocity.X < 0)
                {
                    Velocity.X = 0;
                }
            }
            if (Position.X > game.GraphicsDevice.Viewport.Width)
            { 
                Position.X = game.GraphicsDevice.Viewport.Width;

                if (Velocity.X > 0)
                {
                    Velocity.X = 0;
                }
            }
            if (Position.Y < 0) 
            {
                Position.Y = 0;

                if (Velocity.Y < 0)
                {
                    Velocity.Y = 0;
                }
            }
            if (Position.Y > game.GraphicsDevice.Viewport.Height)
            {
                Position.Y = game.GraphicsDevice.Viewport.Height;

                if (Velocity.Y < 0)
                {
                    Velocity.Y = 0;
                }
            }
        }

		public void UpdateEnemyModification(Ship enemy) {

			var dx = enemy.Position.X - Position.X;
			var dy = enemy.Position.Y - Position.Y;
			var angleInDegrees = ((360 + Math.Round (Math.Atan2 (dy, dx) * 180 / Math.PI)) % 360);

			for (var i = -EnemyModificationSpread; i < EnemyModificationSpread; i++) {
				var strength = MaxEnemyModification - Math.Abs (i * MaxEnemyModification / EnemyModificationSpread);

				var index = (int)((360 + angleInDegrees + i) % 360);

				if (enemy.CurrentPolarity == CurrentPolarity) {
					EnemyModifications[index] -= strength;
				} else {
					EnemyModifications[index] += strength;
				}

				if (EnemyModifications[index] > MaxEnemyModification) {
					EnemyModifications[index] = MaxEnemyModification;
				}

				if (EnemyModifications[index] < -MaxEnemyModification) {
					EnemyModifications[index] = -MaxEnemyModification;
				}
			}
		}

		public override void Draw(SpriteBatch spriteBatch)
        {
			DrawCircle (spriteBatch);
            base.Draw(spriteBatch);
			//spriteBatch.Draw(BoxTexture, Box, new Color(255, 0, 255, 25));
        }

		protected void DrawCircle(SpriteBatch spriteBatch) {

			var j = AuraWaveSize;
			var subWaveRadius = 0.0;
			var randomPosition = 0.0;

			for (var i = 0; i < 360; i++) {
				var angle = i * Math.PI / 180;

				if (j == AuraWaveSize) {
					subWaveRadius = AuraSubAmplitude * Math.Sin (randomPosition + AuraSubPhase);
					randomPosition -= (2 * Math.PI / 8);
					j = 0;
				}
				j++;

				var radius = EnemyModifications[i] + subWaveRadius + AuraRadius + AuraAmplitude * Math.Sin (i / AuraFrequency);
				var x = Position.X + radius * Math.Cos (angle);
				var y = Position.Y + radius * Math.Sin (angle);

				x = Math.Round (x);
				y = Math.Round (y);
				var pointPosition = new Vector2 ((float)x, (float)y);

				if (CurrentPolarity == Polarity.Positive) {
					spriteBatch.Draw (AuraPoint, pointPosition, RedColor);
				} else {
					spriteBatch.Draw (AuraPoint, pointPosition, BlueColor);
				}
			}

			AuraSubPhase += AuraSubFrequency;

			/*         var r = 50;
    var wave = 5;
    var frequency = 1.4;
    var randomFactor = 5;
    var randomAreaSize = 4;
    
    var j = AuraWaveSize;
    var rand = 0;
    var randomPosition = 0;

			AuraSubPhase = 0;
    
    for (var i = 0; i < 360; i++) {
        var rad = Math.radians(i);
        if (j == AuraWaveSize) {
            //rand = Math.random() * 2 * AuraSubAmplitude - AuraSubAmplitude;
            rand = AuraSubAmplitude * Math.sin(AuraSubPhase + offset);
            AuraSubPhase -= (2 * Math.PI / 8);
            j = 0;
        }
        j++;
        var radius = polarityModifications[i] + rand + AuraRadius + AuraAmplitude * Math.sin(i / AuraFrequency);
        var x = Position.x + radius * Math.cos(rad);
        var y = Position.y + radius * Math.sin(rad);
        x = Math.round(x);
        y = Math.round(y);
    
    draw
        //console.log(i, rad, x, y, dataStart);
    });
    
    ctx.putImageData(imgData, 0, 0);*/
		}

		protected Texture2D CreateCircle(int radius)
		{
			int outerRadius = radius * 2 + 2;
			Texture2D texture = new Texture2D (game.GraphicsDevice, outerRadius, outerRadius);

			Color[] data = new Color[outerRadius * outerRadius];

			for (int i = 0; i < data.Length; i++) {
				data [i] = Color.Transparent;
			}

			double angleStep = 1f / radius;

			for (double angle = 0; angle < Math.PI * 2; angle += angleStep)
			{
				int x = (int)Math.Round (radius + radius * Math.Cos (angle));
				int y = (int)Math.Round (radius + radius + Math.Sin (angle));

				data [y * outerRadius + x + 1] = Color.White;
			}

			texture.SetData (data);
			return texture;
		}

        public override void Collide(Actor other, Rectangle collision)
        {
          if (other.GetType().IsAssignableFrom(typeof(StandardShip)) &&
                            !Immortal)
            {
                Die();
            }
        }

        protected override void Die()
        {
            game.Player.Lives = game.Player.Lives - 1;
            game.Player.ResetMultiplier();
            if (game.Player.Lives < 0)
            {
                Dying = true;
                game.GameOver();
            }
            else {
                Hit.Play();
                Immortal = true;
                CurrentImmortalTime = 0;
            }
        }

        public override void CleanUp()
        {
            base.CleanUp();
            InputController.Unbind("moveX", HandleHorizontalMovement);
            InputController.Unbind("moveY", HandleVerticalMovement);
            InputController.Unbind("changePolarity", HandleChangePolarity);
            InputController.Unbind("shoot", HandleShot);
        }
    }
}