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

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