source: 2011/26/JaakkoL/Rogue Agent 2372/Rogue Agent 2372/Rogue Agent 2372/Peli.cs @ 2397

Revision 2397, 3.9 KB checked in by teeevasa, 10 years ago (diff)

revised the first level so that it works with the background, as well as some misc updates

Line 
1using System;
2using System.Collections.Generic;
3using Jypeli;
4using Jypeli.Assets;
5using Jypeli.Controls;
6using Jypeli.Effects;
7using Jypeli.Widgets;
8
9public class Peli : PhysicsGame
10{
11    List<Label> titleMenu;
12    const int SCREEN_WIDTH = 50;
13    const int SCREEN_HEIGHT = 50;
14    Player player;
15    Image background1 = LoadImage("Images/reactorInsideBG");
16
17    public override void Begin()
18    {
19        menuCreate();
20    }
21
22    void menuCreate()
23    {
24        ClearAll();
25        titleMenu = new List<Label>();
26
27        Label startGameButton = new Label("Start Game");
28        startGameButton.Position = new Vector(0, 40);
29        titleMenu.Add(startGameButton);
30
31        Label exitGameButton = new Label("Exit Game");
32        exitGameButton.Position = new Vector(0, -40);
33        titleMenu.Add(exitGameButton);
34
35        //Starts playing the title screen music, which is set to repeat (which is unnecessary though - who would want to stare at an inanimate screen for 4 minutes?)
36        MediaPlayer.Play("Sounds/TitleScreenMusic");
37        MediaPlayer.Volume = 1.0;
38        MediaPlayer.IsRepeating = true;
39
40        foreach (Label titleMenuScreen in titleMenu)
41        {
42            Add(titleMenuScreen);
43        }
44
45        Mouse.ListenOn(startGameButton, MouseButton.Left, ButtonState.Pressed, startGame, null);
46        Keyboard.Listen(Key.Enter, ButtonState.Pressed, startGame, null);
47
48        Mouse.ListenOn(exitGameButton, MouseButton.Left, ButtonState.Pressed, Exit, null);
49        Keyboard.Listen(Key.Escape, ButtonState.Pressed, Exit, null);
50
51        Mouse.IsCursorVisible = true;
52        Mouse.ListenMovement(1.0, MenuMovement, null);
53    }
54
55    void MenuMovement(AnalogState mouseState)
56    {
57        foreach (Label button in titleMenu)
58        {
59            if (Mouse.IsCursorOn(button))
60            {
61                button.TextColor = Color.DarkRed;
62            }
63            else
64            {
65                button.TextColor = Color.DarkBlue;
66            }
67        }
68    }
69
70    void startGame()
71    {
72        ClearAll();
73        createLevel();
74        Gravity = new Vector(0, -1000);
75
76        MediaPlayer.Stop();
77        // Starts playing the background music, which is set to repeat
78        MediaPlayer.Play("Sounds/Stage1Music");
79        MediaPlayer.Volume = 0.75;
80        MediaPlayer.IsRepeating = true;
81
82        Camera.Follow(player);
83        Camera.ZoomFactor = 1.0;
84        Camera.StayInLevel = true;
85
86        //AddCollisionHandler(MeleeAlien alien, handleAlienCollision);
87    }
88
89    void handleAlienCollision(MeleeAlien alien, Player player)
90    {
91        player.reduceHitPointsBy(10);
92    }
93
94    void createLevel()
95    {
96        TileMap levelSpec = TileMap.FromFile("InsideReactor.txt");
97        levelSpec['='] = createVerticalBlock;
98        levelSpec['|'] = createHorizontalBlock;
99        levelSpec['P'] = addPlayer;
100        levelSpec['a'] = addMeleeAlien;
101        levelSpec.Insert(SCREEN_WIDTH, SCREEN_HEIGHT);
102        Level.CreateBorders();
103        Level.Background.Image = background1;
104    }
105
106    Player addPlayer()
107    {
108        player = new Player(33, 74, Shape.Rectangle);
109        return player;
110    }
111
112    MeleeAlien addMeleeAlien()
113    {
114        MeleeAlien meleeAlien = new MeleeAlien(45, 80, Shape.Rectangle, 50, player);
115        return meleeAlien;
116    }
117
118    PhysicsObject createHorizontalBlock()
119    {
120        PhysicsObject hBlock = PhysicsObject.CreateStaticObject(SCREEN_WIDTH, SCREEN_HEIGHT,
121        Shape.Rectangle, CollisionShapeQuality.Best);
122        hBlock.Color = Color.Gray;
123        return hBlock;
124    }
125
126    PhysicsObject createVerticalBlock()
127    {
128        PhysicsObject vBlock = PhysicsObject.CreateStaticObject(SCREEN_WIDTH, SCREEN_HEIGHT,
129        Shape.Rectangle, CollisionShapeQuality.Best);
130        vBlock.Color = Color.DarkGray;
131        return vBlock;
132    }
133}
Note: See TracBrowser for help on using the repository browser.