source: 2012/30/JyriP/X/X/X/X.cs @ 3793

Revision 3793, 10.3 KB checked in by anlakane, 11 years ago (diff)

Talletus.

Line 
1using System;
2using System.Collections.Generic;
3using Jypeli;
4using Jypeli.Assets;
5using Jypeli.Controls;
6using Jypeli.Effects;
7using Jypeli.Widgets;
8
9public class X : PhysicsGame
10{
11    PlatformCharacter player;
12    PhysicsObject window;
13    PhysicsObject house;
14    PhysicsObject goal;
15    PhysicsObject border;
16    PhysicsObject floatingBridge;
17    PhysicsObject DropHouse;
18    GameObject qwe;
19
20    int mapNum = 1;
21
22    //Animaatiot
23    Image[] playerImg = Game.LoadImages("imgs/player/player", "imgs/player/player2", "imgs/player/player3");
24    Image[] jumpAni = Game.LoadImages("imgs/player/playerJump", "imgs/player/playerJump2", "imgs/player/playerJump3");
25    Image[] breakWindow = Game.LoadImages("imgs/animations/window/break", "imgs/animations/window/break2", "imgs/animations/window/break3", "imgs/animations/window/break4");
26
27    public override void Begin()
28    {
29        // TODO: Kirjoita ohjelmakoodisi tähän
30        //Kopioi tämä NextMap();
31        MainMenu();
32        IsMouseVisible = true;
33        IsFullScreen = true;
34    }
35
36    void MainMenu()
37    {
38        mapNum = 1;
39        ClearAll();
40        MediaPlayer.Play("musics/theme");
41        MultiSelectWindow mainMenu = new MultiSelectWindow("X", "Play", "Exit");
42        mainMenu.AddItemHandler(0, MapMenu);
43        mainMenu.AddItemHandler(1, ExitGame);
44        Add(mainMenu);
45    }
46
47    void MapMenu()
48    {
49        MultiSelectWindow MapList = new MultiSelectWindow("X Maps", "Hello World", "Run!", "QWE", ".:???:.");
50        MapList.AddItemHandler(0, NextMap);
51        MapList.AddItemHandler(1, map2);
52        MapList.AddItemHandler(2, map3);
53        MapList.AddItemHandler(3, map4);
54        Add(MapList);
55    }
56
57    void map2()
58    {
59        MediaPlayer.Play("musics/music2");
60        mapNum = 2;
61        NextMap();
62    }
63
64    void map3()
65    {
66        MediaPlayer.Play("musics/theme");
67        mapNum = 3;
68        NextMap();
69    }
70
71    void map4()
72    {
73        MediaPlayer.Play("musics/music2");
74        mapNum = 4;
75        NextMap();
76    }
77
78
79    void NextMap()
80    {
81        ClearAll();
82
83        Gravity = new Vector(0.0, -800.0);
84        if (mapNum == 1) CreateMap("maps/map1");
85        else if (mapNum == 2)
86        {
87            CreateMap("maps/map2");
88        }
89        else if (mapNum == 3)
90        {
91            CreateMap("maps/map3");
92        }
93        else if (mapNum == 4)
94        {
95            CreateMap("maps/map4");
96        }
97
98        else if (mapNum == 5) MainMenu();
99
100        Level.Background.FitToLevel();
101
102        Image[] tausta = Game.LoadImages("imgs/animations/bg/pu", "imgs/animations/bg/ke", "imgs/animations/bg/mupu", "imgs/animations/bg/va", "imgs/animations/bg/mu", "imgs/animations/bg/muva", "imgs/animations/bg/vi");
103        Animation taustaAni = new Animation(tausta);
104        taustaAni.FPS = 10;
105        taustaAni.Start();
106
107        Camera.ZoomFactor = 0.99;
108
109        qwe = new GameObject(Screen.Width, Screen.Height);
110        qwe.X = Level.Left;
111        qwe.Animation = taustaAni;
112        Add(qwe, -2);
113
114       
115    }
116
117    void CreateMap(string fileName)
118    {
119        ColorTileMap tile = ColorTileMap.FromLevelAsset(fileName);
120       
121        tile.SetTileMethod(Color.FromHexCode("00ff00"), CreatePlayer);
122       
123        tile.SetTileMethod(Color.Black, CreateHouse);
124        tile.SetTileMethod(Color.DarkGray, CreateRoof);
125        tile.SetTileMethod(Color.White, CreateWindow);
126        tile.SetTileMethod(Color.Yellow, CreateGoal);
127        tile.SetTileMethod(Color.FromHexCode("ff0000"), CreateBorder);
128        tile.SetTileMethod(Color.FromHexCode("a4a4a4"), CreateFloor);
129        tile.SetTileMethod(Color.FromHexCode("9f9f9f"), CreateHouseDrop);
130        tile.SetTileMethod(Color.FromHexCode("822b00"), CreateFloatingBridge);
131        tile.SetTileMethod(Color.FromHexCode("cccccc"), CreateBgWall);
132
133        tile.Execute(32, 32);
134    }
135
136
137    void CreatePlayer(Vector pos, double width, double height)
138    {
139        player = new PlatformCharacter(21, 45);
140        player.Position = pos;
141        player.Animation = new Animation(playerImg);
142        player.Animation.FPS = 5;
143        player.Animation.Start();
144        Camera.FollowX(player);
145
146        AddCollisionHandler<PlatformCharacter, PhysicsObject>(player, "break", onHitWindow);
147        AddCollisionHandler<PlatformCharacter, PhysicsObject>(player, "drop", onHitFloatingBridge);
148        AddCollisionHandler<PlatformCharacter, PhysicsObject>(player, "dropHouse", onHitDropHouse);
149        AddCollisionHandler<PlatformCharacter, PhysicsObject>(player, "die", onHitHouse);
150        AddCollisionHandler<PlatformCharacter, PhysicsObject>(player, "border", onHitHouse);
151        AddCollisionHandler<PlatformCharacter, PhysicsObject>(player, "goal", onHitGoal);
152
153        Keyboard.Listen(Key.Space, ButtonState.Pressed, Jump, "", player, 650.0);
154        Keyboard.Listen(Key.LeftControl, ButtonState.Pressed, Jump, "", player, 800.0);
155        Keyboard.Listen(Key.R, ButtonState.Pressed, NextMap, "");
156        Keyboard.Listen(Key.Escape, ButtonState.Pressed, MainMenu, "");
157
158
159        ControllerOne.Listen(Button.A, ButtonState.Pressed, Jump, "", player, 650.0);
160        ControllerOne.Listen(Button.B, ButtonState.Pressed, Jump, "", player, 800.0);
161        ControllerOne.Listen(Button.X, ButtonState.Pressed, Jump, "", player, 800.0);
162        ControllerOne.Listen(Button.RightShoulder, ButtonState.Pressed, NextMap, "");
163        ControllerOne.Listen(Button.Back, ButtonState.Pressed, MainMenu, "");
164        ControllerOne.Listen(Button.Start, ButtonState.Pressed, MainMenu, "");
165        Add(player, 2);
166    }
167
168    void onHitWindow(PlatformCharacter player, PhysicsObject window)
169    {
170        window.IgnoresGravity = false;
171        Timer.SingleShot(1.0, delegate { window.Destroy(); });
172
173    }
174
175    void onHitFloatingBridge(PlatformCharacter player, PhysicsObject floatingBridge)
176    {
177        floatingBridge.IgnoresGravity = false;
178        Timer.SingleShot(1.0, delegate { floatingBridge.Destroy(); });
179
180    }
181
182    void onHitDropHouse(PlatformCharacter player, PhysicsObject floatingBridge)
183    {
184        DropHouse.IgnoresGravity = false;
185        Timer.SingleShot(3.0, delegate { DropHouse.Destroy(); });
186
187    }
188
189    void onHitHouse(PlatformCharacter player, PhysicsObject house)
190    {
191        player.Destroy();
192        NextMap();
193
194    }
195
196    void onHitGoal(PlatformCharacter player, PhysicsObject house)
197    {
198        mapNum++;
199        NextMap();
200    }
201
202    void Jump(PlatformCharacter player, double jump)
203    {
204        ControllerOne.Vibrate(0.1, 0.1, 1, 1, 0.5);
205        Phone.Vibrate(100);
206        player.Jump(jump);
207    }
208
209    protected override void Update(Time time)
210    {
211        if (player != null)
212        {
213            qwe.X = player.X;
214            player.Walk(150);
215        }
216        base.Update(time);
217    }
218
219   
220
221    void CreateHouse(Vector pos, double width, double height)
222    {
223        house = PhysicsObject.CreateStaticObject(width, height);
224        Image houseImg = Game.LoadImage("imgs/housePart");
225        house.Position = pos;
226        house.Image = houseImg;
227        house.CollisionIgnoreGroup = 1;
228        house.Tag = "die";
229        Add(house);
230    }
231
232    void CreateHouseDrop(Vector pos, double width, double height)
233    {
234        DropHouse = PhysicsObject.CreateStaticObject(width, height);
235        Image DropHouseImg = Game.LoadImage("imgs/housePart");
236        DropHouse.Position = pos;
237        DropHouse.Image = DropHouseImg;
238        DropHouse.CollisionIgnoreGroup = 1;
239        DropHouse.IgnoresGravity = true;
240        DropHouse.Mass = 50;
241        DropHouse.CanRotate = false;
242        DropHouse.Tag = "dropHouse";
243        Add(DropHouse);
244    }
245
246    void CreateRoof(Vector pos, double width, double height)
247    {
248        PhysicsObject roof = PhysicsObject.CreateStaticObject(width, height);
249        Image roofImg = Game.LoadImage("imgs/roof");
250        roof.Position = pos;
251        roof.Image = roofImg;
252        roof.CollisionIgnoreGroup = 1;
253        Add(roof);
254    }
255
256    void CreateWindow(Vector pos, double width, double height)
257    {
258        window = new PhysicsObject(24, height);
259        Image windowImg = Game.LoadImage("imgs/window");
260        window.IgnoresGravity = true;
261        window.Position = pos;
262        window.Image = windowImg;
263        window.CollisionIgnoreGroup = 1;
264        window.Mass = 5;
265        window.Tag = "break";
266        Add(window, 1);
267    }
268
269    void CreateFloatingBridge(Vector pos, double width, double height)
270    {
271        floatingBridge = new PhysicsObject(width, height);
272        Image floatingBridgeImg = Game.LoadImage("imgs/floor");
273        floatingBridge.IgnoresGravity = true;
274        floatingBridge.Position = pos;
275        floatingBridge.Image = floatingBridgeImg;
276        floatingBridge.CollisionIgnoreGroup = 1;
277        floatingBridge.Mass = 5;
278        floatingBridge.Tag = "drop";
279        Add(floatingBridge, 1);
280    }
281
282
283
284    void CreateFloor(Vector pos, double width, double height)
285    {
286        PhysicsObject floor = PhysicsObject.CreateStaticObject(width, height);
287        Image floorImg = Game.LoadImage("imgs/floor");
288        floor.Position = pos;
289        floor.Image = floorImg;
290        floor.CollisionIgnoreGroup = 1;
291        Add(floor);
292    }
293
294    void CreateBorder(Vector pos, double width, double height)
295    {
296        border = PhysicsObject.CreateStaticObject(16, height);
297        Image borderImg = Game.LoadImage("imgs/border");
298        border.Position = pos;
299        border.CollisionIgnoreGroup = 0;
300        border.Image = borderImg;
301        border.Tag = "border";
302        Add(border);
303    }
304
305
306    void CreateGoal(Vector pos, double width, double height)
307    {
308        goal = PhysicsObject.CreateStaticObject(width, height);
309        Image goalImg = Game.LoadImage("imgs/goal");
310        goal.Position = pos;
311        goal.Image = goalImg;
312        goal.CollisionIgnoreGroup = 1;
313        goal.Tag = "goal";
314        Add(goal);
315    }
316
317    void CreateBgWall(Vector pos, double width, double height)
318    {
319        GameObject wall = new GameObject(width, height);
320        Image wallImg = Game.LoadImage("imgs/wall/wall");
321        wall.Position = pos;
322        wall.Image = wallImg;
323        Add(wall, -1);
324    }
325
326    void ExitGame()
327    {
328        Exit();
329    }
330
331}
Note: See TracBrowser for help on using the repository browser.