source: 2017/27/PyryS/GravityAssist/GravityAssist/GravityAssist/GravityAssist.cs @ 8896

Revision 8896, 5.8 KB checked in by npo17_32, 4 years ago (diff)

Added Random Generation to GravityAssist?, some more graphics, and changed controls and added a restart button.

Line 
1using System;
2using System.Collections.Generic;
3using Jypeli;
4using Jypeli.Assets;
5using Jypeli.Controls;
6using Jypeli.Effects;
7using Jypeli.Widgets;
8
9public class GravityAssist : PhysicsGame
10{
11    const double nopeus = 30;
12    const double hyppyNopeus = 750;
13    const int RUUDUN_KOKO = 40;
14
15    bool GravityDirection;
16
17    PhysicsObject player;
18
19    // Load all images
20    Image[] playerImages = LoadImages("Player0", "Player1");
21    Image wall0 = LoadImage("SpaceStationWall0");
22    Image wall1 = LoadImage("SpaceStationWall1");
23
24    Image damagedWall0 = LoadImage("SpaceStationWallDamaged0");
25
26    Image obstacle0 = LoadImage("Obstacle0");
27    Image background0 = LoadImage("background0");
28
29
30    // Beginning Function
31    public override void Begin()
32    {
33        Gravity = new Vector(0, 0);
34        SmoothTextures = false;
35
36        LuoKentta();
37        LisaaNappaimet();
38
39        Camera.Follow(player);
40        Camera.ZoomFactor = 1.0;
41        Camera.StayInLevel = true;
42
43        //Level.Background.Image = background0;
44
45        GameObject backgroundObject0 = new GameObject(Level.Width, 128);
46        backgroundObject0.Image = background0;
47        backgroundObject0.TextureWrapSize = new Vector(16, 1);
48        Add(backgroundObject0, -3);
49        Layers[-3].RelativeTransition = new Vector(0.5, 0);
50    }
51    // Level loading function
52    void LuoKentta()
53    {
54        TileMap kentta = TileMap.FromLevelAsset("kentta1");
55        kentta.SetTileMethod('A', LisaaTaso, false);
56        kentta.SetTileMethod('V', LisaaTaso, true);
57        kentta.SetTileMethod('.', AddObstacle, true);
58        kentta.SetTileMethod(',', AddObstacle, false);
59        kentta.SetTileMethod('N', LisaaPelaaja);
60        kentta.Execute(RUUDUN_KOKO, RUUDUN_KOKO);
61        Level.CreateBorders();
62        Level.Background.Color = Color.Black;
63    }
64    // Level generation function
65    void LisaaTaso(Vector paikka, double leveys, double korkeus, bool flipped)
66    {
67        PhysicsObject taso = PhysicsObject.CreateStaticObject(leveys, korkeus);
68        taso.Position = paikka;
69        taso.Image = RandomGen.SelectOne(wall0, wall0, wall0, wall0,wall0, wall0, wall0, wall0, wall1, damagedWall0);
70        if (flipped) taso.FlipImage();
71        taso.Restitution = 0.0;
72        taso.KineticFriction = 0.0;
73        taso.StaticFriction = 0.0;
74        taso.Tag = "wall";
75        Add(taso);
76    }
77    // Obstacle creation function
78    void AddObstacle(Vector paikka, double leveys, double korkeus, bool flipped)
79    {
80        PhysicsObject obstacle = PhysicsObject.CreateStaticObject(korkeus, leveys, Shape.FromImage(obstacle0));
81        obstacle.IgnoresCollisionResponse = true;
82        obstacle.Image = obstacle0;
83        if (flipped)
84        {
85            obstacle.FlipImage();
86            obstacle.Position = paikka + new Vector(Convert.ToInt64(RandomGen.NextDouble(Level.Left, Level.Right)), -10);
87        }
88        else
89        {
90            obstacle.Position = paikka + new Vector(Convert.ToInt64(RandomGen.NextDouble(Level.Left, Level.Right)), 10);
91        }
92        obstacle.Tag = "obstacle";
93        Add(obstacle);
94    }
95    // Player creation and movement function
96    void LisaaPelaaja(Vector paikka, double leveys, double korkeus)
97    {
98        player = new PhysicsObject(32, 13);
99        player.Position = paikka;
100        player.Mass = 100.0;
101        player.CanRotate = false;
102        player.KineticFriction = 0.0;
103        player.StaticFriction = 0.0;
104        player.Restitution = 0.0;
105        player.Image = playerImages[0];
106        player.Animation = new Animation(playerImages);
107        player.Animation.FPS = 15;
108        player.Animation.Start();
109        AddCollisionHandler(player, collision);
110        Add(player);
111        player.IgnoresCollisionResponse = true;
112        Timer liikutusAjastin = new Timer();
113        liikutusAjastin.Interval = 0.02;
114        liikutusAjastin.Timeout += delegate { player.Velocity = new Vector(150, player.Velocity.Y); };
115        liikutusAjastin.Start();
116
117    }
118    // Controls function
119    void LisaaNappaimet()
120    {
121        Keyboard.Listen(Key.Escape, ButtonState.Pressed, ConfirmExit, "Exit");
122
123        Keyboard.Listen(Key.R, ButtonState.Pressed, restart, "Restart");
124
125        Keyboard.Listen(Key.Space, ButtonState.Pressed, GravitySwap, "Gravity Switched", player, 1.0);
126
127        ControllerOne.Listen(Button.Back, ButtonState.Pressed, Exit, "Exit");
128
129        ControllerOne.Listen(Button.A, ButtonState.Pressed, GravitySwap, "Gravity Switched", player, 1.0);
130    }
131
132    // Gravity swap function
133    void GravitySwap(PhysicsObject Player, double Force)
134    {
135        if (GravityDirection == true)
136        {
137            player.Velocity = new Vector(player.Velocity.X, -150);
138            GravityDirection = false;
139            player.TextureWrapSize = new Vector(1, 1);
140        }
141        else if (GravityDirection == false)
142        {
143            player.Velocity = new Vector(player.Velocity.X, 150);
144            GravityDirection = true;
145            player.TextureWrapSize = new Vector(1, -1);
146        }
147    }
148    // Collision management function
149    void collision(PhysicsObject hahmo, PhysicsObject obstacle)
150    {
151
152        if (obstacle.Tag == "obstacle")
153        {
154
155            MessageDisplay.Add("You died.");
156            player.Destroy();
157            restart();
158        }
159
160        if (obstacle.Tag == "wall")
161        {
162            player.Velocity = new Vector(player.Velocity.X, 0);
163            if (player.Y < obstacle.Top)
164            {
165                player.Y = obstacle.Bottom - player.Height / 2.0 - 0.2;
166            }
167            else
168            {
169                player.Y = obstacle.Top + player.Height / 2.0 + 0.2;
170            }
171        } 
172    }
173    void restart()
174    {
175        ClearAll();
176        Begin();
177    }
178}
Note: See TracBrowser for help on using the repository browser.