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

Revision 2402, 4.4 KB checked in by teeevasa, 12 years ago (diff)

updated Jypeli

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