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

Revision 2412, 4.7 KB checked in by teeevasa, 9 years ago (diff)

some final changes, such as removing unnecessary files etc.

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        //AddCollisionHandler(alien, handleAlienCollision);
90    }
91
92    void winGame(PhysicsObject victoryPoint, PhysicsObject target)
93    {
94        if (target.Tag == "Player")
95        {
96            ClearAll();
97            Label youWon = new Label("You won!");
98            Add(youWon);
99        }
100    }
101
102    void handleAlienCollision(PhysicsObject alien, PhysicsObject target)
103    {
104        if (target.Tag == "Player")
105        {
106            alienAttack.Play();
107            player.reduceHitPointsBy(10);
108        }
109    }
110
111    void createLevel()
112    {
113        TileMap levelSpec = TileMap.FromFile("InsideReactor.txt");
114        levelSpec['='] = createVerticalBlock;
115        levelSpec['|'] = createHorizontalBlock;
116        levelSpec['P'] = addPlayer;
117        levelSpec['a'] = addMeleeAlien;
118        levelSpec['x'] = addVictoryPoint;
119        levelSpec.Insert(SCREEN_WIDTH, SCREEN_HEIGHT);
120        Level.CreateBorders();
121        Level.Background.Image = background1;
122    }
123
124    Player addPlayer()
125    {
126        player = new Player(33, 74, Shape.Rectangle);
127        return player;
128    }
129
130    MeleeAlien addMeleeAlien()
131    {
132        MeleeAlien meleeAlien = new MeleeAlien(45, 80, Shape.Rectangle, 50, player);
133        AddCollisionHandler(meleeAlien, handleAlienCollision);
134        return meleeAlien;
135    }
136
137    PhysicsObject createHorizontalBlock()
138    {
139        PhysicsObject hBlock = PhysicsObject.CreateStaticObject(SCREEN_WIDTH, SCREEN_HEIGHT,
140        Shape.Rectangle, CollisionShapeQuality.Best);
141        hBlock.Color = Color.Gray;
142        return hBlock;
143    }
144
145    PhysicsObject createVerticalBlock()
146    {
147        PhysicsObject vBlock = PhysicsObject.CreateStaticObject(SCREEN_WIDTH, SCREEN_HEIGHT,
148        Shape.Rectangle, CollisionShapeQuality.Best);
149        vBlock.Color = Color.DarkGray;
150        return vBlock;
151    }
152
153    PhysicsObject addVictoryPoint()
154    {
155        PhysicsObject victoryPoint = PhysicsObject.CreateStaticObject(SCREEN_WIDTH, SCREEN_HEIGHT, Shape.Rectangle);
156        AddCollisionHandler(victoryPoint, winGame);
157        return victoryPoint;
158    }
159}
Note: See TracBrowser for help on using the repository browser.