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

Revision 2360, 3.7 KB checked in by jaollipa, 8 years ago (diff)
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/reactorBG");
16
17
18    public override void Begin()
19    {
20        menuCreate();
21    }
22
23    void menuCreate()
24    {
25        ClearAll();
26        titleMenu = new List<Label>();
27
28        Label startGameButton = new Label("Start Game");
29        startGameButton.Position = new Vector(0, 40);
30        titleMenu.Add(startGameButton);
31
32        Label exitGameButton = new Label("Exit Game");
33        exitGameButton.Position = new Vector(0, -40);
34        titleMenu.Add(exitGameButton);
35
36        //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?)
37        MediaPlayer.Play("Sounds/TitleScreenMusic");
38        MediaPlayer.Volume = 1.0;
39        MediaPlayer.IsRepeating = true;
40
41        foreach (Label titleMenuScreen in titleMenu)
42        {
43            Add(titleMenuScreen);
44        }
45
46        Mouse.ListenOn(startGameButton, MouseButton.Left, ButtonState.Pressed, startGame, null);
47        Keyboard.Listen(Key.Enter, ButtonState.Pressed, startGame, null);
48
49        Mouse.ListenOn(exitGameButton, MouseButton.Left, ButtonState.Pressed, Exit, null);
50        Keyboard.Listen(Key.Escape, ButtonState.Pressed, Exit, null);
51
52        Mouse.IsCursorVisible = true;
53        Mouse.ListenMovement(1.0, MenuMovement, null);
54    }
55
56    void MenuMovement(AnalogState mouseState)
57    {
58        foreach (Label button in titleMenu)
59        {
60            if (Mouse.IsCursorOn(button))
61            {
62                button.TextColor = Color.DarkRed;
63            }
64            else
65            {
66                button.TextColor = Color.DarkBlue;
67            }
68        }
69    }
70
71    void startGame()
72    {
73        ClearAll();
74        createLevel();
75        Gravity = new Vector(0, -1000);
76
77        MediaPlayer.Stop();
78        // Starts playing the background music, which is set to repeat
79        MediaPlayer.Play("Sounds/Stage1Music");
80        MediaPlayer.Volume = 0.75;
81        MediaPlayer.IsRepeating = true;
82
83        Camera.Follow(player);
84        Camera.ZoomFactor = 1.0;
85        Camera.StayInLevel = true;
86    }
87
88    void createLevel()
89    {
90        TileMap levelSpec = TileMap.FromFile("ReactorEntrance.txt");
91        levelSpec['='] = createVerticalBlock;
92        levelSpec['|'] = createHorizontalBlock;
93        levelSpec['P'] = addPlayer;
94        levelSpec['a'] = addMeleeAlien;
95        levelSpec.Insert(SCREEN_WIDTH, SCREEN_HEIGHT);
96        Level.CreateBorders();
97        Level.Background.Image = background1;
98    }
99
100    Player addPlayer()
101    {
102        player = new Player(33, 74, Shape.Rectangle);
103        return player;
104    }
105
106    MeleeAlien addMeleeAlien()
107    {
108        MeleeAlien meleeAlien = new MeleeAlien(45, 80, Shape.Rectangle, 50);
109        return meleeAlien;
110    }
111
112    PhysicsObject createHorizontalBlock()
113    {
114        PhysicsObject hBlock = PhysicsObject.CreateStaticObject(SCREEN_WIDTH, SCREEN_HEIGHT,
115        Shape.Rectangle, CollisionShapeQuality.Best);
116        hBlock.Color = Color.Gray;
117        return hBlock;
118    }
119
120    PhysicsObject createVerticalBlock()
121    {
122        PhysicsObject vBlock = PhysicsObject.CreateStaticObject(SCREEN_WIDTH, SCREEN_HEIGHT,
123        Shape.Rectangle, CollisionShapeQuality.Best);
124        vBlock.Color = Color.DarkGray;
125        return vBlock;
126    }
127}
Note: See TracBrowser for help on using the repository browser.