source: 2013/27/ArttuY/trunk/RampageRebellion/RampageRebellion/RampageRebellion/RRBegin.cs.r807 @ 4451

Revision 4451, 38.4 KB checked in by eearyla, 7 years ago (diff)

Backup

Line 
1using System;
2using System.Collections.Generic;
3using System.Linq;
4using System.Text;
5using System.Diagnostics;
6using Jypeli;
7using Jypeli.Assets;
8using Jypeli.Controls;
9using Jypeli.Effects;
10using Jypeli.Widgets;
11
12/// @author     Jaakko Lipas
13/// @version    Dev - 2.7.2013
14
15/// <summary>
16/// The main class of the game that uses all other classes in the project (except RRMain).
17/// </summary>
18public class RampageRebellion : PhysicsGame
19{
20
21    #region Tech Data
22
23    /**
24     * Tags:
25     *
26     *  B = Border
27     *  P = Player
28     *  SE = Consolidated Tag for Small-Class Enemies (small, medium)
29     *  LE = Consolidated Tag for Large-Class Enemies (large, boss)
30     *  EP = Enemy Projectile
31     *  PP = Player Projectile
32     *  LP = Player Laser Blaster Projectile
33     *  BOMB = Player Bomb
34     *  BX = Bomb Explosion
35     *  ------------
36     * Collision Ignore Groups
37     *  1 = Walls
38     *  2 = Players
39     *  3 = Projectiles
40     *  4 = Enemies
41     *  **/
42
43    #endregion
44
45    #region Attributes
46    /// <summary>
47    /// Type of controls. Valid for a keyboard and an Xbox 360 controller.
48    /// </summary>
49    public enum ControllerType { Keyboard, Xbox };
50
51    /// <summary>
52    /// Meter that gauges score.
53    /// </summary>
54    public readonly IntMeter SCOREMETER = new IntMeter(0);
55    /// <summary>
56    /// Meter that gauges the player's current power.
57    /// </summary>
58    public readonly IntMeter POWERMETER = new IntMeter(0);
59    /// <summary>
60    /// Meter that gauges the player's current health.
61    /// </summary>
62    public readonly IntMeter HEALTHMETER = new IntMeter(0);
63    /// <summary>
64    /// The player's speed (X-axis).
65    /// </summary>
66    public readonly Vector PLAYER_SPEED_X = new Vector(750.0 * 1.5, 0.0);
67    /// <summary>
68    /// The player's speed (Y-axis).
69    /// </summary>
70    public readonly Vector PLAYER_SPEED_Y = new Vector(0.0, 750.0 * 1.5);
71    /// <summary>
72    /// Position of the score meter.
73    /// </summary>
74    public readonly Vector SMETER_POSITION = new Vector(320.0, 290.0);
75    /// <summary>
76    /// Position of the power meter.
77    /// </summary>
78    public readonly Vector PMETER_POSITION = new Vector(320.0, 210.0);
79    /// <summary>
80    /// Position of the health meter.
81    /// </summary>
82    public readonly Vector HMETER_POSITION = new Vector(265.0, 150.0);
83
84    /// <summary>
85    /// Position used for the weapon selector position
86    /// </summary>
87    ///
88
89    public readonly Vector WMETER_POSITION = new Vector(320.0, -80.0);
90    /// <summary>
91    /// Position used for respawning the player.
92    /// </summary>
93
94    public readonly Vector RESPAWN_POSITION = new Vector(-200.0, -300.0);
95    /// <summary>
96    /// Gravity vector.
97    /// </summary>
98    public readonly Vector GRAVITY = new Vector(0.0, -150.0);
99    /// <summary>
100    /// GameObject array that visualizes the player's current health.
101    /// </summary>
102    public readonly GameObject[] METER_RECTANGLES = new GameObject[PLAYER_HP];
103    /// <summary>
104    /// Amount of enemy spawns in the game.
105    /// </summary>
106    public readonly int[] SPAWN_COUNT = new int[SPAWN_AMOUNT];
107
108    /// <summary>
109    /// Spawn position array for small enemy spawns.
110    /// </summary>
111    public readonly Vector[] ENEMY_SPAWNS1 = { new Vector(0.0, 200.0), new Vector(-100.0, 300.0), new Vector(-200.0, 100.0), new Vector(-250.0, 250.0), new Vector(-400.0, 150.0) };
112    /// <summary>
113    /// Spawn position array for medium enemy spawns.
114    /// </summary>
115    public readonly Vector[] ENEMY_SPAWNS2 = { new Vector(-200.0, 0.0), new Vector(-200.0, 100.0), new Vector(-200.0, 200.0), new Vector(-200.0, 300.0), new Vector(-200.0, 400.0) };
116    /// <summary>
117    /// Spawn position array for large enemy spawns.
118    /// </summary>
119    public readonly Vector[] ENEMY_SPAWNS3 = { new Vector(-450.0, 300.0), new Vector(-350.0, 300.0), new Vector(-250.0, 300.0), new Vector(-150.0, 300.0), new Vector(-50.0, 300.0) };
120    /// <summary>
121    /// Spawn position for the boss.
122    /// </summary>
123    public readonly Vector BOSS_SPAWN = new Vector(-200.0, 300.0);
124
125    /// <summary>
126    /// Shot power for the player's weapon.
127    /// </summary>
128    public const int PLAYER_WEAPONPOWER = 120;
129    /// <summary>
130    /// Small enemy health.
131    /// </summary>
132    public const double SMALLENEMY_HP = 50;
133    /// <summary>
134    /// Small enemy score value.
135    /// </summary>
136    public const double SMALLENEMY_SVALUE = 100;
137    /// <summary>
138    /// Small enemy power value.
139    /// </summary>
140    public const double SMALLENEMY_PVALUE = 1;
141    /// <summary>
142    /// Medium enemy health.
143    /// </summary>
144    public const double MEDENEMY_HP = 170;
145    /// <summary>
146    /// Medium enemy score value.
147    /// </summary>
148    public const double MEDENEMY_SVALUE = 400;
149    /// <summary>
150    /// Medium enemy power value.
151    /// </summary>
152    public const double MEDENEMY_PVALUE = 3;
153    /// <summary>
154    /// Big enemy health.
155    /// </summary>
156    public const double BIGENEMY_HP = 300;
157    /// <summary>
158    /// Big enemy score value.
159    /// </summary>
160    public const double BIGENEMY_SVALUE = 1000;
161    /// <summary>
162    /// Big enemy power value.
163    /// </summary>
164    public const double BIGENEMY_PVALUE = 6;
165    /// <summary>
166    /// Boss health.
167    /// </summary>
168    public const double BOSS_HP = 1444;
169    /// <summary>
170    /// Boss score value.
171    /// </summary>
172    public const double BOSS_SVALUE = 10000;
173    /// <summary>
174    /// Boss power value.
175    /// </summary>
176    public const double BOSS_PVALUE = 25;
177    /// <summary>
178    /// Player health. Also used as the value for determining the length of the player's health meter.
179    /// </summary>
180    public const int PLAYER_HP = 6;
181    /// <summary>
182    /// Player score value (the value to be deducted from the current score, if the player dies)
183    /// </summary>
184    public const double PLAYER_SVALUE = 4000;
185    /// <summary>
186    /// Radius of a small enemy.
187    /// </summary>
188    public const double SMALLENEMY_SIZE = 40.0;
189    /// <summary>
190    /// Radius of a medium enemy.
191    /// </summary>
192    public const double MEDENEMY_SIZE = 70.0;
193    /// <summary>
194    /// Radius of a big enemy.
195    /// </summary>
196    public const double BIGENEMY_SIZE = 110.0;
197    /// <summary>
198    /// Radius of a boss enemy.
199    /// </summary>
200    public const double BOSS_SIZE = 222.0;
201    /// <summary>
202    /// Enemy mass.
203    /// </summary>
204    public const double ENEMY_MASS = 600.0;
205    /// <summary>
206    /// Linear damping value for enemies.
207    /// </summary>
208    public const double ENEMY_DAMPING = 0.92;
209    /// <summary>
210    /// Fire rate of the player's weapon.
211    /// </summary>
212    public const double PLAYER_FIRERATE = 12;
213    /// <summary>
214    /// Length of a meter.
215    /// </summary>
216    public const double METER_LENGTH = 200.0;
217    /// <summary>
218    /// Length of a single bar in a health meter. Dependant on attribute METER_LENGTH.
219    /// </summary>
220    public const double HMETER_LENGTH = METER_LENGTH / 4;
221    /// <summary>
222    /// Height of a meter.
223    /// </summary>
224    public const double METER_HEIGHT = 25.0;
225    /// <summary>
226    /// Value to use when executing the tilemap.
227    /// </summary>
228    public const double FIELDEXEC_VALUE = 20.0;
229    /// <summary>
230    /// Interval between enemy spawns (in seconds).
231    /// </summary>
232    public const double SPAWNLOGIC_INTERVAL = 14.0;
233    /// <summary>
234    /// Sets the amount of enemy spawns to execute.
235    /// </summary>
236    public const int SPAWN_AMOUNT = 11;
237    /// <summary>
238    /// General initialization value for int variables.
239    /// </summary>
240    public const int GENERAL_INITVALUE = 0;
241
242    private static RampageRebellion gameObject;
243
244
245    /// <summary>
246    /// Internal variable, whenever the vibration features are enabled
247    /// </summary>
248
249    public bool vibrateEnabled = false;
250
251
252    /// <summary>
253    /// Primary weapon for the player. Fast but not too efficient
254    /// </summary>
255
256    public RRWeapon primaryWeapon;
257
258    /// <summary>
259    /// Secondary weapon. Not too fast to fire or to advance, but is effective
260    /// </summary>
261    ///
262    public RRWeapon secWeapon;
263
264    /// <summary>
265    /// Third weapon. Bomb, that is.
266    /// </summary>
267    ///
268    public RRWeapon triWeapon;
269
270    /// <summary>
271    /// Player weapon arsenal! >:D
272    /// </summary>
273
274    public RRWeaponArsenal playerWeaponArsenal;
275    #endregion
276
277    #region Initialization
278    /// <summary>
279    /// The title screen void that loads the title screen background, which contains control information.
280    /// </summary>
281    ///
282
283    public override void Begin()
284    {
285        gameObject = this;
286
287        SetWindowSize(1024, 820, false);
288        Image MenuBG = LoadImage("Title");
289        Level.Background.Image = MenuBG;
290        Level.BackgroundColor = Color.DarkGray;
291        Camera.ZoomToLevel();
292        Level.Background.ScaleToLevel();
293        ControllerOne.Listen(Button.Start, ButtonState.Pressed, delegate { ControlMethod(); ControllerOne.Disable(Button.Start); }, null);
294        ControllerOne.Listen(Button.Back, ButtonState.Pressed, ConfirmExit, null);
295        Keyboard.Listen(Key.Z, ButtonState.Pressed, delegate { ControlMethod(); Keyboard.Disable(Key.Z); }, null);
296        Keyboard.Listen(Key.Escape, ButtonState.Pressed, ConfirmExit, null);
297    }
298
299    /// <summary>
300    /// Reads which type of control the player wants to use - Controller or Keyboard - and passes the information to the other control settings.
301    /// </summary>
302    public void ControlMethod()
303    {
304        ClearAll();
305        Image CntrlBG = LoadImage("ControlScreen");
306        Level.Background.Image = CntrlBG;
307        Level.BackgroundColor = Color.DarkGray;
308        Level.Background.ScaleToLevel();
309        ControllerOne.Listen(Button.Start, ButtonState.Pressed, delegate { GameStart(ControllerType.Xbox); ControllerOne.Disable(Button.Start); }, null);
310        Keyboard.Listen(Key.Z, ButtonState.Pressed, delegate { GameStart(ControllerType.Keyboard); Keyboard.Disable(Key.Z); }, null);
311    }
312
313    /// <summary>
314    /// Get game object
315    /// </summary>
316    /// <returns></returns>
317
318    public static RampageRebellion getGame()
319    {
320        return gameObject;
321    }
322
323    /// <summary>
324    /// Loads a color tilemap to generate the field elements using subprograms. Zooms the camera accordingly. Adds menu controls.
325    /// </summary>
326    /// <param name="controllerType">The type of controls being used.</param>
327    public void GameStart(ControllerType controllerType)
328    {
329        Camera.ZoomToAllObjects();
330
331        vibrateEnabled = (controllerType == ControllerType.Xbox);
332
333        SCOREMETER.Reset();
334        POWERMETER.Reset();
335        HEALTHMETER.Reset();
336
337        ColorTileMap Field = ColorTileMap.FromLevelAsset("GameTilemap");
338
339        Field.SetTileMethod(new Color(0, 0, 0), CreateBorder);
340        Field.SetTileMethod(new Color(255, 0, 0), CreatePlayer, controllerType);
341        CreateScoreMeter();
342        CreatePowerMeter();
343
344        CreateHealthMeter(GENERAL_INITVALUE);
345        Field.Execute(FIELDEXEC_VALUE, FIELDEXEC_VALUE);
346
347        Image LevelUI = LoadImage("UI");
348        Level.Background.Image = LevelUI;
349
350        // Starts enemy spawning; ES.levels() for the pre-designed levels and ES.arcade() for arcade.
351        RREnemySpawner ES = new RREnemySpawner(); ES.levels();
352
353        Gravity = GRAVITY;
354    }
355    #endregion
356
357    #region In-Games
358    /// <summary>
359    /// A pause menu that pops up if the pause button is pressed.
360    /// </summary>
361    /// <param name="controllerType">The type of controls being used.</param>
362    /// <param name="player">The player.</param>
363    /// <param name="primaryWeapon">The player's weapon.</param>
364    public void PauseMenu(PhysicsObject player, ControllerType controllerType, RRWeaponArsenal primaryWeapon)
365    {
366        ClearControls();
367
368        MediaPlayer.Pause();
369        Pause();
370        switch (controllerType)
371        {
372            case ControllerType.Keyboard:
373                Keyboard.Listen(Key.Escape, ButtonState.Pressed, delegate { Pause(); ResetGame(); }, null);
374                Keyboard.Listen(Key.P, ButtonState.Pressed, delegate { Unpause(player, controllerType, primaryWeapon); }, null);
375                break;
376
377            case ControllerType.Xbox:
378                ControllerOne.Listen(Button.Back, ButtonState.Pressed, delegate { Pause(); ResetGame(); }, null);
379                ControllerOne.Listen(Button.Start, ButtonState.Pressed, delegate { Unpause(player, controllerType, primaryWeapon); }, null);
380                break;
381        }
382    }
383
384    /// <summary>
385    /// Unpauses the game and resumes music.
386    /// </summary>
387    /// <param name="controllerType">The type of controls being used.</param>
388    /// <param name="player">The player.</param>
389    /// <param name="primaryWeapon">The player's weapon.</param>
390    public void Unpause(PhysicsObject player, ControllerType controllerType, RRWeaponArsenal primaryWeapon)
391    {
392        ClearControls();
393        MediaPlayer.Resume();
394        Pause();
395        CreateControls(player, controllerType, primaryWeapon);
396    }
397
398    /// <summary>
399    /// Clears all game data and resets the game.
400    /// </summary>
401    public void ResetGame()
402    {
403        ClearAll();
404        Begin();
405    }
406
407    /// <summary>
408    /// Void that creates an invisible (fully transparent) border object. The playable field is visually bounded by the background image,
409    /// so there is no need to apply a visible texture or color to the borders.
410    /// </summary>
411    /// <param name="position">Vector location of the object, as specified in GameStart</param>
412    /// <param name="width">Width of the object, as specified in GameStart</param>
413    /// <param name="height">Height of the object, as specified in GameStart</param>
414    public void CreateBorder(Vector position, double width, double height)
415    {
416        PhysicsObject border = PhysicsObject.CreateStaticObject(width, height);
417        border.Position = position;
418        border.Color = new Color(0, 0, 0, 0);
419        border.CollisionIgnoreGroup = 1;
420        border.Tag = "B";
421        Add(border);
422    }
423
424    /// <summary>
425    /// Void that creates the player's collision detector box and sprite, adding controls for them.
426    /// </summary>
427    /// <param name="position">Vector location of the object, as specified in GameStart</param>
428    /// <param name="width">Width of the object, as specified in GameStart</param>
429    /// <param name="height">Height of the object, as specified in GameStart</param>
430    /// <param name="controllerType">The type of controls used</param>
431    ///
432
433    public void vibrateController()
434    {
435        if (((ControllerOne.GetType()) == typeof(GamePad)) && vibrateEnabled)
436        {
437            GamePad gp = ControllerOne as GamePad;
438            gp.Vibrate(0.5, 0.5, 0.0, 0.0, 0.1);
439        }
440    }
441
442    public void collisionHandler(RRShip collider, PhysicsObject collidee)
443    {
444        //System.Diagnostics.Debugger.Log(0, "Info", collider.Tag + "on" + collidee.Tag + "\n");
445        if ((String)collidee.Tag != "Z") vibrateController();
446    }
447
448    public void meterHandlerFunc(RRShip player)
449    {
450
451        for (int i = 0; i < METER_RECTANGLES.Length; i++)
452        {
453            METER_RECTANGLES[i].Color = ((i < player.Health) ? Color.Green : Color.Red);
454        }
455
456        if (player.Health == 0)
457        {
458            onPlayerDeath(player);
459        }
460    }
461
462    public void onPlayerDeath(RRShip player)
463    {
464        if (((ControllerOne.GetType()) == typeof(GamePad)) && vibrateEnabled)
465        {
466            GamePad gp = ControllerOne as GamePad;
467            gp.Vibrate(1, 1, 1, 1, 1);
468        }
469
470        Explosion expl = new Explosion(200);
471        expl.Position = player.Position;
472        Add(expl);
473
474        player.Destroy();
475
476        SCOREMETER.Value -= (int)player.ScoreValue;
477        POWERMETER.Reset();
478        ClearControls();
479
480        Keyboard.Listen(Key.Z, ButtonState.Pressed, delegate
481        {
482            for (int i = 0; i < METER_RECTANGLES.Length; i++)
483            {
484                METER_RECTANGLES[i].Color = Color.Green;
485            }
486            CreatePlayer(RESPAWN_POSITION, 20, 20, ControllerType.Keyboard);
487        }, null);
488        ControllerOne.Listen(Button.A, ButtonState.Pressed, delegate
489        {
490            for (int i = 0; i < METER_RECTANGLES.Length; i++)
491            {
492                METER_RECTANGLES[i].Color = Color.Green;
493            }
494            CreatePlayer(RESPAWN_POSITION, 20, 20, ControllerType.Xbox);
495        }, null);
496    }
497
498    public void CreatePlayer(Vector position, double width, double height, ControllerType controllerType)
499    {
500        // Replaced by RRShip
501        /*RRObject player = new RRObject(width, height);
502        player.Tag = "P";
503        player.Shape = Shape.Diamond;
504        player.Health = PLAYER_HP;
505        player.ScoreValue = PLAYER_SVALUE;
506        player.Position = position;
507        player.CanRotate = false;
508        player.Color = Color.Red;
509        player.CollisionIgnoreGroup = 2;
510        Add(player);
511        player.Add(new GameObject(LoadImage("PlayerSprite")));
512        player.IgnoresGravity = true;
513        AddCollisionHandler<RRObject, PhysicsObject>(player, PlayerCollision);*/
514
515        RRShip ship = new RRShip(this, width, height, PLAYER_HP, PLAYER_SVALUE, position, collisionHandler, meterHandlerFunc);
516        Add(ship);
517        HEALTHMETER.Value = PLAYER_HP;
518
519        //Let's generate this first..
520
521        GeneratePlayerWeapons(ship);
522
523        CreateControls(ship, controllerType, playerWeaponArsenal);
524    }
525
526
527    public void onBombDestruction(RRProjectile rrp)
528    {
529    }
530
531    /// <summary>
532    /// Generates weapons for the player's use
533    /// </summary>
534    /// <param name="ship"></param>
535
536    private void GeneratePlayerWeapons(RRShip ship)
537    {
538        primaryWeapon = new RRWeapon(5, 10, true, PLAYER_WEAPONPOWER, PLAYER_FIRERATE, new RRProjectileGenerator(20, 3, 0.1, new Color(255, 55, 128, 128), 10));
539        primaryWeapon.Position += new Vector(0, 30);
540        primaryWeapon.projectileGenerator.defaultCollisionIgnoreGroup = 3;
541        primaryWeapon.projectileGenerator.defaultTag = "PP";
542        primaryWeapon.projectileGenerator.canRotate = false;
543        primaryWeapon.Color = new Color(0, 0, 0, 0);
544
545        primaryWeapon.Angle += Angle.FromDegrees(90);
546
547        //primaryWeapon.ProjectileCollision = playerCollisionHandler;
548
549
550
551        //ShipWeapon secWeapon = new ShipWeapon(20, 30);
552        secWeapon = new RRWeapon(5, 10, true, PLAYER_WEAPONPOWER / 2.5, PLAYER_FIRERATE * 10, new RRProjectileGenerator(80, 3, 0.01, new Color(33, 211, 111, 128), 0.9));
553        secWeapon.Position += new Vector(0, 60);
554        secWeapon.projectileGenerator.defaultCollisionIgnoreGroup = 3;
555        secWeapon.projectileGenerator.ignoresCollisionResponse = true;
556        secWeapon.projectileGenerator.defaultTag = "LP";
557        secWeapon.projectileGenerator.canRotate = false;
558        secWeapon.Color = new Color(0, 0, 0, 0);
559
560        secWeapon.Angle += Angle.FromDegrees(90);
561
562
563        //And the BOMB!
564
565        triWeapon = new RRWeapon(30, 30, true, PLAYER_WEAPONPOWER * 2, PLAYER_FIRERATE / 10, new RRProjectileGenerator(30, 30, 0.2, new Color(111, 111, 111, 128), 200));
566        triWeapon.projectileGenerator.projectileShape = Shape.Circle;
567        triWeapon.projectileGenerator.defaultCollisionIgnoreGroup = 3;
568        triWeapon.projectileGenerator.defaultTag = "BOMB";
569        triWeapon.projectileGenerator.canRotate = false;
570        triWeapon.Color = new Color(0, 0, 0, 0);
571
572        triWeapon.Angle += Angle.FromDegrees(90);
573        triWeapon.countInWeaponArsenal = false;
574
575        playerWeaponArsenal = new RRWeaponArsenal(primaryWeapon);
576        //rP = primaryWeapon;
577        playerWeaponArsenal.ProjectileCollision = playerCollisionHandler;
578        playerWeaponArsenal.addNewWeapon(secWeapon);
579        playerWeaponArsenal.addNewWeapon(triWeapon);
580        ship.Add(playerWeaponArsenal);
581
582        CreateWeaponMeter();
583    }
584
585    public void triggerBomb()
586    {
587        playerWeaponArsenal.singleShoot(2);
588    }
589
590    public void shiftWeapons()
591    {
592        //Attempt to shift a weapon
593        playerWeaponArsenal.setNextWeapon();
594    }
595
596    /// <summary>
597    /// Creates controls for the player depending on the chosen control method.
598    /// </summary>
599    /// <param name="player">The player to be moved</param>
600    /// <param name="controllerType">The current control type</param>
601    /// <param name="primaryWeapon">The player's weapon</param>
602    private void CreateControls(PhysicsObject player, ControllerType controllerType, RRWeaponArsenal primaryWeapon)
603    {
604        ClearControls();
605        vibrateEnabled = (controllerType == ControllerType.Xbox);
606        switch (controllerType)
607        {
608            case ControllerType.Keyboard:
609                Keyboard.Listen(Key.Up, ButtonState.Down, MovePlayer, null, PLAYER_SPEED_Y, player);
610                Keyboard.Listen(Key.Up, ButtonState.Released, MovePlayer, null, Vector.Zero, player);
611                Keyboard.Listen(Key.Down, ButtonState.Down, MovePlayer, null, -PLAYER_SPEED_Y, player);
612                Keyboard.Listen(Key.Down, ButtonState.Released, MovePlayer, null, Vector.Zero, player);
613                Keyboard.Listen(Key.Left, ButtonState.Down, MovePlayer, null, -PLAYER_SPEED_X, player);
614                Keyboard.Listen(Key.Left, ButtonState.Released, MovePlayer, null, Vector.Zero, player);
615                Keyboard.Listen(Key.Right, ButtonState.Down, MovePlayer, null, PLAYER_SPEED_X, player);
616                Keyboard.Listen(Key.Right, ButtonState.Released, MovePlayer, null, Vector.Zero, player);
617                Keyboard.Listen(Key.P, ButtonState.Pressed, delegate { PauseMenu(player, controllerType, primaryWeapon); }, null);
618                Keyboard.Listen(Key.Z, ButtonState.Down, FireWeapon, null, primaryWeapon);
619                Keyboard.Listen(Key.X, ButtonState.Pressed, triggerBomb, null);
620                Keyboard.Listen(Key.LeftShift, ButtonState.Pressed, shiftWeapons, null);
621                break;
622            case ControllerType.Xbox:
623                ControllerOne.Listen(Button.DPadUp, ButtonState.Down, MovePlayer, null, PLAYER_SPEED_Y, player);
624                ControllerOne.Listen(Button.DPadUp, ButtonState.Released, MovePlayer, null, Vector.Zero, player);
625                ControllerOne.Listen(Button.DPadDown, ButtonState.Down, MovePlayer, null, -PLAYER_SPEED_Y, player);
626                ControllerOne.Listen(Button.DPadDown, ButtonState.Released, MovePlayer, null, Vector.Zero, player);
627                ControllerOne.Listen(Button.DPadLeft, ButtonState.Down, MovePlayer, null, -PLAYER_SPEED_X, player);
628                ControllerOne.Listen(Button.DPadLeft, ButtonState.Released, MovePlayer, null, Vector.Zero, player);
629                ControllerOne.Listen(Button.DPadRight, ButtonState.Down, MovePlayer, null, PLAYER_SPEED_X, player);
630                ControllerOne.Listen(Button.DPadRight, ButtonState.Released, MovePlayer, null, Vector.Zero, player);
631                ControllerOne.ListenAnalog(AnalogControl.LeftStick, 0.1, movePlayerAnalog, null, player);
632                ControllerOne.Listen(Button.Start, ButtonState.Pressed, delegate { PauseMenu(player, controllerType, primaryWeapon); }, null);
633                ControllerOne.Listen(Button.RightTrigger, ButtonState.Down, FireWeapon, null, primaryWeapon);
634                ControllerOne.Listen(Button.Y, ButtonState.Pressed, shiftWeapons, null);
635                break;
636            default:
637                break;
638        }
639    }
640
641    public void movePlayerAnalog(AnalogState al, PhysicsObject player)
642    {
643        double xPos = al.StateVector.X;
644        double yPos = al.StateVector.Y;
645        Vector pPushVector = new Vector((xPos) * PLAYER_SPEED_X.X, (yPos) * PLAYER_SPEED_Y.Y);
646        player.Push(pPushVector);
647
648        double maxVibration = 0.15;
649        double maxBias = 0.5;
650
651        double prcBias = (0.5 * Math.Abs((xPos + yPos / 2))) +
652                         (0.5 * Math.Max(0, Math.Min(1, (1 - Math.Abs((player.Velocity.Magnitude / 100))))));
653
654        //Bias the control
655        double rightControllerVibr = ((maxVibration / 2) - ((maxVibration / 2 * maxBias) * xPos)) * prcBias;
656        double leftControllerVibr = ((maxVibration / 2) + ((maxVibration / 2 * maxBias) * xPos)) * prcBias;
657
658        if (((ControllerOne.GetType()) == typeof(GamePad)) && vibrateEnabled)
659        {
660            GamePad gp = ControllerOne as GamePad;
661            gp.Vibrate(leftControllerVibr, rightControllerVibr, 0, 0, 0.1);
662        }
663
664        //player.Velocity = pPushVector;
665    }
666
667    /// <summary>
668    /// Fires the primary weapon of the player.
669    /// </summary>
670    /// <param name="weapon">The weapon to be fired</param>
671    public void FireWeapon(RRWeaponArsenal weapon)
672    {
673        PhysicsObject projectile = ((RRWeaponArsenal)weapon).Shoot();
674    }
675
676    /// <summary>
677    /// Applies the force vector specified in the controls to the player object.
678    /// </summary>
679    /// <param name="force">The force vector to be applied, specified in a previous Listen method</param>
680    /// <param name="player">The player that the force vector is to be applied to, specified in a previous Listen method</param>
681    public void MovePlayer(Vector force, PhysicsObject player)
682    {
683        player.Push(force);
684        //player.Velocity = force;
685    }
686
687
688    public void CreateWeaponMeter()
689    {
690        Label weaponMeter = new Label(METER_LENGTH, METER_HEIGHT);
691        weaponMeter.Position = WMETER_POSITION;
692        weaponMeter.Color = Color.Azure;
693        weaponMeter.BindTo(playerWeaponArsenal.currentWeaponIndexMeter);
694        Add(weaponMeter);
695    }
696
697    /// <summary>
698    /// Creates a score meter on the screen. Value represented by the attribute SCOREMETER.
699    /// </summary>
700    public void CreateScoreMeter()
701    {
702        Label scoreMeter = new Label(METER_LENGTH, METER_HEIGHT);
703        scoreMeter.Position = SMETER_POSITION;
704        scoreMeter.TextColor = Color.Green;
705        scoreMeter.BindTo(SCOREMETER);
706        Add(scoreMeter);
707    }
708
709    /// <summary>
710    /// Creates a power meter on the screen. Value represented by the attribute POWERMETER.
711    /// </summary>
712    public void CreatePowerMeter()
713    {
714        Label powerMeter = new Label(METER_LENGTH, METER_HEIGHT);
715        powerMeter.Position = PMETER_POSITION;
716        powerMeter.TextColor = Color.BloodRed;
717        powerMeter.BindTo(POWERMETER);
718        Add(powerMeter);
719    }
720
721    /// <summary>
722    /// Creates a health meter on the screen recursively. Length of the health meter is dependant on player maximum health.
723    /// </summary>
724    /// <param name="i"></param>
725    public void CreateHealthMeter(int i)
726    {
727        if (i >= PLAYER_HP) return;
728        METER_RECTANGLES[i] = new GameObject(HMETER_LENGTH, METER_HEIGHT);
729        METER_RECTANGLES[i].Position = new Vector(HMETER_POSITION.X + (HMETER_LENGTH * i), HMETER_POSITION.Y);
730        METER_RECTANGLES[i].Color = Color.Green;
731        Add(METER_RECTANGLES[i]);
732        CreateHealthMeter(i + 1);
733    }
734
735    public void UpgradeMenu()
736    {
737    }
738
739    #endregion
740
741    #region Colliders
742    /// <summary>
743    /// Handles collisions between player projectiles and other objects.
744    /// </summary>
745    /// <param name="projectile">A projectile fired by the player (collider)</param>
746    /// <param name="target">Target of collision</param>
747    public void playerCollisionHandler(PhysicsObject projectile, PhysicsObject target)
748    {
749        if (target.Tag.ToString() == "B" & projectile.Tag.ToString() != "BX") projectile.Destroy(); // If the bullet hits the top wall, destroy it
750        else if (target.Tag.ToString() == "EP" || target.Tag.ToString() == "PP")
751        {
752            if (projectile.Tag.ToString() != "BOMB" || projectile.Tag.ToString() != "BE") projectile.Destroy();
753            target.Destroy();
754        }
755
756        else if (target.Tag.ToString().EndsWith("E"))
757        {
758            RREnemy enemy = target as RREnemy;
759            RRProjectile proj = projectile as RRProjectile;
760            if (enemy.Health >= 0) enemy.Health -= proj.Damage;
761            if (enemy.Health <= 0)
762            {
763                //System.Diagnostics.Debugger.Log(0, "Info", Convert.ToString(enemy.ScoreValue) + "\n");
764                SCOREMETER.Value += (int)enemy.ScoreValue;
765                POWERMETER.Value += (int)enemy.PowerValue;
766                enemy.Destroy();
767            }
768            if ((projectile.Tag.ToString() == "LP" & target.Tag.ToString() != "LE") || projectile.Tag.ToString() == "BX") return;
769            else if (projectile.Tag.ToString() == "BOMB" & (target.Tag.ToString().EndsWith("E") || target.Tag.ToString() == "B")) BombExplosion(projectile);
770            projectile.Destroy();
771        }
772    }
773
774    public void BombExplosion(PhysicsObject bomb)
775    {
776        RRProjectile bombExplosion = new RRProjectile(5.0, 9001, "explosion");
777        bombExplosion.Position = bomb.Position;
778        bombExplosion.Damage = 150;
779        bombExplosion.Tag = "BX";
780        bombExplosion.IgnoresCollisionResponse = true;
781        bombExplosion.IgnoresGravity = true;
782        AddCollisionHandler<RRProjectile, RREnemy>(bombExplosion, playerCollisionHandler);
783        Add(bombExplosion);
784
785        Timer growExplosion = new Timer();
786        growExplosion.Interval = 0.02;
787        growExplosion.Start();
788        growExplosion.Timeout += delegate
789        {
790            if (bombExplosion.Width >= 1000 & bombExplosion.Height >= 1000) { growExplosion.Reset(); bombExplosion.Destroy(); }
791            else { bombExplosion.Width = bombExplosion.Width * 1.1; bombExplosion.Height = bombExplosion.Height * 1.1; }
792        };
793    }
794
795    #endregion
796
797    #region Enemy Logic
798
799
800    /// <summary>
801    /// After a successfully ran game, opens score screen and starts the program over after closing it.
802    /// </summary>
803    public void GameEnd()
804    {
805        Pause();
806        ScoreList scoreList = new ScoreList(10, false, 2500);
807        scoreList = DataStorage.TryLoad<ScoreList>(scoreList, "scorelist.xml");
808        HighScoreWindow scoreWindow = new HighScoreWindow("High Scores", "You have made the high scores! Please enter your name: ", scoreList, SCOREMETER.Value);
809        scoreWindow.Closed += delegate { DataStorage.Save<ScoreList>(scoreList, "scorelist.xml"); Pause(); ClearAll(); Begin(); };
810        Add(scoreWindow);
811    }
812    #endregion
813
814    #region Legacy
815    // Below: legacy code to document previous logic errors
816
817    //Old enemy generator code. Replaced by RREnemySpawner!
818
819    /** /// <summary>
820    /// Creates an enemy on the field with the given parameters. Returns the created enemy.
821    /// </summary>
822    /// <param name="position">Position to create enemy in</param>
823    /// <param name="radius">Radius of the enemy</param>
824    /// <param name="hp">Health of the enemy</param>
825    /// <param name="powerValue">Power given by the enemy on death</param>
826    /// <param name="scoreValue">Score given by the enemy on death</param>
827    /// <returns>The created enemy.</returns>
828    public RRObject CreateEnemy(Vector position, double radius, ushort hp, ushort powerValue, ushort scoreValue)
829    {
830        RRObject enemy = new RRObject(radius, radius);
831        enemy.Tag = "E";
832        enemy.Position = position;
833        enemy.Color = Color.HotPink;
834        enemy.CollisionIgnoreGroup = 3;
835        enemy.Mass = ENEMY_MASS;
836        enemy.LinearDamping = ENEMY_DAMPING;
837        enemy.CanRotate = false;
838        enemy.Health = hp;
839        enemy.ScoreValue = scoreValue;
840        enemy.PowerValue = powerValue;
841        Add(enemy);
842        return enemy;
843    } **/
844
845    /// <summary>
846    /// Main process for enemy spawns. If this isn't called in GameStart, no enemies will spawn.
847    /// REPLACED BY RREnemySpawner
848    /// </summary>
849    /*public void MainEnemyProcess(int spawnLogic)
850    {
851        for (int i = 0; i < SPAWN_COUNT.Length; i++)
852        {
853            SPAWN_COUNT[i] = i;
854        }
855        Timer spawns = new Timer();
856        spawns.Interval = SPAWNLOGIC_INTERVAL;
857        spawns.Start();
858        spawns.Timeout += delegate
859        {
860            if (spawnLogic == SPAWN_COUNT[0] || spawnLogic == SPAWN_COUNT[2] || spawnLogic == SPAWN_COUNT[4] || spawnLogic == SPAWN_COUNT[5])
861            {
862                for (int i = 0; i < ENEMY_SPAWNS1.Length; i++)
863                {
864                    CreateEnemy(ENEMY_SPAWNS1[i], SMALLENEMY_SIZE, SMALLENEMY_HP, SMALLENEMY_PVALUE, SMALLENEMY_SVALUE);
865                }
866            }
867            else if (spawnLogic == SPAWN_COUNT[1] || spawnLogic == SPAWN_COUNT[3] || spawnLogic == SPAWN_COUNT[6] || spawnLogic == SPAWN_COUNT[10])
868            {
869                for (int i = 0; i < ENEMY_SPAWNS2.Length; i++)
870                {
871                    CreateEnemy(ENEMY_SPAWNS2[i], MEDENEMY_SIZE, MEDENEMY_HP, MEDENEMY_PVALUE, MEDENEMY_SVALUE);
872                }
873            }
874            else if (spawnLogic >= SPAWN_COUNT[7] & spawnLogic < SPAWN_COUNT[10])
875            {
876                for (int i = 0; i < ENEMY_SPAWNS3.Length; i++)
877                {
878                    CreateEnemy(ENEMY_SPAWNS3[i], BIGENEMY_SIZE, BIGENEMY_HP, BIGENEMY_PVALUE, BIGENEMY_SVALUE);
879                }
880            }
881            else if (spawnLogic == 11)
882            {
883                RRObject boss = CreateEnemy(BOSS_SPAWN, BOSS_SIZE, BOSS_HP, BOSS_PVALUE, BOSS_SVALUE);
884                boss.Destroyed += delegate { GameEnd(); };
885            }
886            spawnLogic++;
887        };
888    }*/
889
890    /// <summary>
891    /// Handles collisions between the player and objects that can damage the player.
892    /// </summary>
893    /// <param name="player">The player (collider)</param>
894    /// <param name="target">Target of collision</param>
895    /* public void PlayerCollision(RRObject player, PhysicsObject target)
896     {
897         if (player.Velocity.X + player.Velocity.Y > 200.0)
898         {
899             if (target.Tag.ToString() == "B")
900             {
901                 if (player.Health == 1) METER_RECTANGLES[0].Color = Color.Red;
902                 else METER_RECTANGLES[player.Health - 1].Color = Color.Red;
903                 player.Health -= 1;
904             }
905             else if (target.Tag.ToString() == "E")
906             {
907                 target.Destroy();
908                 if (player.Health == 1)
909                 {
910                     METER_RECTANGLES[0].Color = Color.Red;
911                     player.Health -= 1;
912                 }
913                 else METER_RECTANGLES[player.Health - 1].Color = Color.Red;
914                 if (player.Health > 1)
915                 {
916                     METER_RECTANGLES[player.Health - 2].Color = Color.Red;
917                     player.Health -= 2;
918                 }
919             }
920             if (player.Health == 0)
921             {
922                 player.Destroy();
923                 SCOREMETER.Value -= player.ScoreValue;
924                 POWERMETER.Reset();
925                 ClearControls();
926                 Keyboard.Listen(Key.Z, ButtonState.Pressed, delegate
927                 {
928                     for (int i = 0; i < METER_RECTANGLES.Length; i++)
929                     {
930                         METER_RECTANGLES[i].Color = Color.Green;
931                     }
932                     CreatePlayer(RESPAWN_POSITION, 20, 20, ControllerType.Keyboard);
933                 }, null);
934                 ControllerOne.Listen(Button.A, ButtonState.Pressed, delegate
935                 {
936                     for (int i = 0; i < METER_RECTANGLES.Length; i++)
937                     {
938                         METER_RECTANGLES[i].Color = Color.Green;
939                     }
940                     CreatePlayer(RESPAWN_POSITION, 20, 20, ControllerType.Xbox);
941                 }, null);
942             }
943         }
944     } */
945
946    /*
947    /// <summary>
948    /// Void that adds an invisible PhysicsObject entity to an
949    /// existing PhysicsObject, to serve as an origin point for Bullet entities.
950    /// </summary>
951    /// <param name="shooter">The entity that the origin object is to be attached to</param>
952    /// <param name="controllerType">The type of controls used</param>
953    public void AddWeapons(PhysicsObject shooter)
954    {
955        PhysicsObject origin = new PhysicsObject(shooter.Width, shooter.Height, Shape.Rectangle);
956        origin.IgnoresCollisionResponse = true;
957        shooter.Add(origin);
958    }
959
960    /// <summary>
961    /// Uses a timer delegate to set the rate-of-fire for the player's weapon. Allows "rapid fire" controls.
962    /// </summary>
963    /// <param name="origin">The object to originate shots from</param>
964    /// <param name="weapon">The timer to set. The timer is created in a previous Listen method</param>
965    /// <param name="controlType">The type of controls used (Controller true/Keyboard false)</param>
966    public void WeaponRate(PhysicsObject origin, Timer weapon, bool controlType)
967    {
968        weapon.Interval = 0.225;
969        weapon.Timeout += delegate { FireWeapon(origin, Shape.Rectangle); };
970        weapon.Start();
971        if (controlType == true) {
972            ControllerOne.Listen(Button.RightTrigger, ButtonState.Released, StopRate, null, weapon);
973        }
974        else { Keyboard.Listen(Key.Z, ButtonState.Released, StopRate, null, weapon); }
975    }
976
977    /// <summary>
978    /// Stops the weapon timer from cycling the next shot.
979    /// </summary>
980    /// <param name="weapon">The timer to stop</param>
981    public void StopRate(Timer weapon)
982    {
983        weapon.Stop();
984    }
985
986    /// <summary>
987    /// Calls a subprogram which creates a Bullet at the specified location and sends it off flying.
988    /// </summary>
989    /// <param name="origin">The object to set origin point for</param>
990    /// <param name="shape">Shape of the bullet's collision detector</param>
991    public void FireWeapon(PhysicsObject origin, Shape shape)
992    {
993        CreateBullet(origin.X + 5, origin.Y + 10, 15.0, 4.0, shape);
994        CreateBullet(origin.X - 5, origin.Y + 10, 15.0, 4.0, shape);
995    }
996
997    public void CreateBullet(double x, double y, double width, double height, Shape shape)
998    {
999        Bullet Weapon1 = new Bullet(width, height);
1000        Weapon1.Damage = 5;
1001        Weapon1.X = x;
1002        Weapon1.Y = y;
1003        Weapon1.Shape = shape;
1004        Weapon1.Hit(new Vector(0.0, 600.0));
1005    }
1006    */
1007    #endregion
1008}
Note: See TracBrowser for help on using the repository browser.