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

Revision 2413, 4.7 KB checked in by teeevasa, 12 years ago (diff)

more cleaning

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    // The sound effect intended for walking sounds close enough to ferocious aliens ripping your guts out
18    SoundEffect alienAttack = LoadSoundEffect("Sounds/walkingSound");
19
20    public override void Begin()
21    {
22        menuCreate();
23    }
24
25    void menuCreate()
26    {
27        ClearAll();
28        titleMenu = new List<Label>();
29
30        Label startGameButton = new Label("Start Game");
31        startGameButton.Position = new Vector(0, 40);
32        titleMenu.Add(startGameButton);
33
34        Label exitGameButton = new Label("Exit Game");
35        exitGameButton.Position = new Vector(0, -40);
36        titleMenu.Add(exitGameButton);
37
38        //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?)
39        MediaPlayer.Play("Sounds/TitleScreenMusic");
40        MediaPlayer.Volume = 1.0;
41        MediaPlayer.IsRepeating = true;
42
43        foreach (Label titleMenuScreen in titleMenu)
44        {
45            Add(titleMenuScreen);
46        }
47
48        Mouse.ListenOn(startGameButton, MouseButton.Left, ButtonState.Pressed, startGame, null);
49        Keyboard.Listen(Key.Enter, ButtonState.Pressed, startGame, null);
50
51        Mouse.ListenOn(exitGameButton, MouseButton.Left, ButtonState.Pressed, Exit, null);
52        Keyboard.Listen(Key.Escape, ButtonState.Pressed, Exit, null);
53
54        Mouse.IsCursorVisible = true;
55        Mouse.ListenMovement(1.0, MenuMovement, null);
56    }
57
58    void MenuMovement(AnalogState mouseState)
59    {
60        foreach (Label button in titleMenu)
61        {
62            if (Mouse.IsCursorOn(button))
63            {
64                button.TextColor = Color.DarkRed;
65            }
66            else
67            {
68                button.TextColor = Color.DarkBlue;
69            }
70        }
71    }
72
73    void startGame()
74    {
75        ClearAll();
76        createLevel();
77        Gravity = new Vector(0, -1000);
78
79        MediaPlayer.Stop();
80        // Starts playing the background music, which is set to repeat
81        MediaPlayer.Play("Sounds/Stage2Music");
82        MediaPlayer.Volume = 0.75;
83        MediaPlayer.IsRepeating = true;
84
85        Camera.Follow(player);
86        Camera.ZoomFactor = 1.0;
87        Camera.StayInLevel = true;
88    }
89
90    void winGame(PhysicsObject victoryPoint, PhysicsObject target)
91    {
92        if (target.Tag == "Player")
93        {
94            ClearAll();
95            Label youWon = new Label("You won!");
96            Add(youWon);
97        }
98    }
99
100    void handleAlienCollision(PhysicsObject alien, PhysicsObject target)
101    {
102        if (target.Tag == "Player")
103        {
104            alienAttack.Play();
105            player.reduceHitPointsBy(10);
106        }
107    }
108
109    void createLevel()
110    {
111        TileMap levelSpec = TileMap.FromFile("InsideReactor.txt");
112        levelSpec['='] = createVerticalBlock;
113        levelSpec['|'] = createHorizontalBlock;
114        levelSpec['P'] = addPlayer;
115        levelSpec['a'] = addMeleeAlien;
116        levelSpec['x'] = addVictoryPoint;
117        levelSpec.Insert(SCREEN_WIDTH, SCREEN_HEIGHT);
118        Level.CreateBorders();
119        Level.Background.Image = background1;
120    }
121
122    Player addPlayer()
123    {
124        player = new Player(33, 74, Shape.Rectangle);
125        return player;
126    }
127
128    MeleeAlien addMeleeAlien()
129    {
130        MeleeAlien meleeAlien = new MeleeAlien(45, 80, Shape.Rectangle, 50, player);
131        AddCollisionHandler(meleeAlien, handleAlienCollision);
132        return meleeAlien;
133    }
134
135    PhysicsObject createHorizontalBlock()
136    {
137        PhysicsObject hBlock = PhysicsObject.CreateStaticObject(SCREEN_WIDTH, SCREEN_HEIGHT,
138        Shape.Rectangle, CollisionShapeQuality.Best);
139        hBlock.Color = Color.Gray;
140        return hBlock;
141    }
142
143    PhysicsObject createVerticalBlock()
144    {
145        PhysicsObject vBlock = PhysicsObject.CreateStaticObject(SCREEN_WIDTH, SCREEN_HEIGHT,
146        Shape.Rectangle, CollisionShapeQuality.Best);
147        vBlock.Color = Color.DarkGray;
148        return vBlock;
149    }
150
151    PhysicsObject addVictoryPoint()
152    {
153        PhysicsObject victoryPoint = PhysicsObject.CreateStaticObject(SCREEN_WIDTH, SCREEN_HEIGHT, Shape.Rectangle);
154        AddCollisionHandler(victoryPoint, winGame);
155        return victoryPoint;
156    }
157}
Note: See TracBrowser for help on using the repository browser.