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

Revision 3791, 10.2 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        Level.Background.FitToLevel();
99
100        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");
101        Animation taustaAni = new Animation(tausta);
102        taustaAni.FPS = 10;
103        taustaAni.Start();
104
105        Camera.ZoomFactor = 0.99;
106
107        qwe = new GameObject(Screen.Width, Screen.Height);
108        qwe.X = Level.Left;
109        qwe.Animation = taustaAni;
110        Add(qwe, -2);
111
112       
113    }
114
115    void CreateMap(string fileName)
116    {
117        ColorTileMap tile = ColorTileMap.FromLevelAsset(fileName);
118       
119        tile.SetTileMethod(Color.FromHexCode("00ff00"), CreatePlayer);
120       
121        tile.SetTileMethod(Color.Black, CreateHouse);
122        tile.SetTileMethod(Color.DarkGray, CreateRoof);
123        tile.SetTileMethod(Color.White, CreateWindow);
124        tile.SetTileMethod(Color.Yellow, CreateGoal);
125        tile.SetTileMethod(Color.FromHexCode("ff0000"), CreateBorder);
126        tile.SetTileMethod(Color.FromHexCode("a4a4a4"), CreateFloor);
127        tile.SetTileMethod(Color.FromHexCode("9f9f9f"), CreateHouseDrop);
128        tile.SetTileMethod(Color.FromHexCode("822b00"), CreateFloatingBridge);
129        tile.SetTileMethod(Color.FromHexCode("cccccc"), CreateBgWall);
130
131        tile.Execute(32, 32);
132    }
133
134
135    void CreatePlayer(Vector pos, double width, double height)
136    {
137        player = new PlatformCharacter(21, 45);
138        player.Position = pos;
139        player.Animation = new Animation(playerImg);
140        player.Animation.FPS = 5;
141        player.Animation.Start();
142        Camera.FollowX(player);
143
144        AddCollisionHandler<PlatformCharacter, PhysicsObject>(player, "break", onHitWindow);
145        AddCollisionHandler<PlatformCharacter, PhysicsObject>(player, "drop", onHitFloatingBridge);
146        AddCollisionHandler<PlatformCharacter, PhysicsObject>(player, "dropHouse", onHitDropHouse);
147        AddCollisionHandler<PlatformCharacter, PhysicsObject>(player, "die", onHitHouse);
148        AddCollisionHandler<PlatformCharacter, PhysicsObject>(player, "border", onHitHouse);
149        AddCollisionHandler<PlatformCharacter, PhysicsObject>(player, "goal", onHitGoal);
150
151        Keyboard.Listen(Key.Space, ButtonState.Pressed, Jump, "", player, 650.0);
152        Keyboard.Listen(Key.LeftControl, ButtonState.Pressed, Jump, "", player, 800.0);
153        Keyboard.Listen(Key.R, ButtonState.Pressed, NextMap, "");
154        Keyboard.Listen(Key.Escape, ButtonState.Pressed, MainMenu, "");
155
156
157        ControllerOne.Listen(Button.A, ButtonState.Pressed, Jump, "", player, 650.0);
158        ControllerOne.Listen(Button.B, ButtonState.Pressed, Jump, "", player, 800.0);
159        ControllerOne.Listen(Button.X, ButtonState.Pressed, Jump, "", player, 800.0);
160        ControllerOne.Listen(Button.RightShoulder, ButtonState.Pressed, NextMap, "");
161        ControllerOne.Listen(Button.Back, ButtonState.Pressed, MainMenu, "");
162        ControllerOne.Listen(Button.Start, ButtonState.Pressed, MainMenu, "");
163        Add(player, 2);
164    }
165
166    void onHitWindow(PlatformCharacter player, PhysicsObject window)
167    {
168        window.IgnoresGravity = false;
169        Timer.SingleShot(1.0, delegate { window.Destroy(); });
170
171    }
172
173    void onHitFloatingBridge(PlatformCharacter player, PhysicsObject floatingBridge)
174    {
175        floatingBridge.IgnoresGravity = false;
176        Timer.SingleShot(1.0, delegate { floatingBridge.Destroy(); });
177
178    }
179
180    void onHitDropHouse(PlatformCharacter player, PhysicsObject floatingBridge)
181    {
182        DropHouse.IgnoresGravity = false;
183        Timer.SingleShot(3.0, delegate { DropHouse.Destroy(); });
184
185    }
186
187    void onHitHouse(PlatformCharacter player, PhysicsObject house)
188    {
189        player.Destroy();
190        NextMap();
191
192    }
193
194    void onHitGoal(PlatformCharacter player, PhysicsObject house)
195    {
196        mapNum++;
197        NextMap();
198    }
199
200    void Jump(PlatformCharacter player, double jump)
201    {
202        ControllerOne.Vibrate(0.1, 0.1, 1, 1, 0.5);
203        Phone.Vibrate(100);
204        player.Jump(jump);
205    }
206
207    protected override void Update(Time time)
208    {
209        if (player != null)
210        {
211            qwe.X = player.X;
212            player.Walk(150);
213        }
214        base.Update(time);
215    }
216
217   
218
219    void CreateHouse(Vector pos, double width, double height)
220    {
221        house = PhysicsObject.CreateStaticObject(width, height);
222        Image houseImg = Game.LoadImage("imgs/housePart");
223        house.Position = pos;
224        house.Image = houseImg;
225        house.CollisionIgnoreGroup = 1;
226        house.Tag = "die";
227        Add(house);
228    }
229
230    void CreateHouseDrop(Vector pos, double width, double height)
231    {
232        DropHouse = PhysicsObject.CreateStaticObject(width, height);
233        Image DropHouseImg = Game.LoadImage("imgs/housePart");
234        DropHouse.Position = pos;
235        DropHouse.Image = DropHouseImg;
236        DropHouse.CollisionIgnoreGroup = 1;
237        DropHouse.IgnoresGravity = true;
238        DropHouse.Mass = 50;
239        DropHouse.CanRotate = false;
240        DropHouse.Tag = "dropHouse";
241        Add(DropHouse);
242    }
243
244    void CreateRoof(Vector pos, double width, double height)
245    {
246        PhysicsObject roof = PhysicsObject.CreateStaticObject(width, height);
247        Image roofImg = Game.LoadImage("imgs/roof");
248        roof.Position = pos;
249        roof.Image = roofImg;
250        roof.CollisionIgnoreGroup = 1;
251        Add(roof);
252    }
253
254    void CreateWindow(Vector pos, double width, double height)
255    {
256        window = new PhysicsObject(24, height);
257        Image windowImg = Game.LoadImage("imgs/window");
258        window.IgnoresGravity = true;
259        window.Position = pos;
260        window.Image = windowImg;
261        window.CollisionIgnoreGroup = 1;
262        window.Mass = 5;
263        window.Tag = "break";
264        Add(window, 1);
265    }
266
267    void CreateFloatingBridge(Vector pos, double width, double height)
268    {
269        floatingBridge = new PhysicsObject(width, height);
270        Image floatingBridgeImg = Game.LoadImage("imgs/floor");
271        floatingBridge.IgnoresGravity = true;
272        floatingBridge.Position = pos;
273        floatingBridge.Image = floatingBridgeImg;
274        floatingBridge.CollisionIgnoreGroup = 1;
275        floatingBridge.Mass = 5;
276        floatingBridge.Tag = "drop";
277        Add(floatingBridge, 1);
278    }
279
280
281
282    void CreateFloor(Vector pos, double width, double height)
283    {
284        PhysicsObject floor = PhysicsObject.CreateStaticObject(width, height);
285        Image floorImg = Game.LoadImage("imgs/floor");
286        floor.Position = pos;
287        floor.Image = floorImg;
288        floor.CollisionIgnoreGroup = 1;
289        Add(floor);
290    }
291
292    void CreateBorder(Vector pos, double width, double height)
293    {
294        border = PhysicsObject.CreateStaticObject(16, height);
295        Image borderImg = Game.LoadImage("imgs/border");
296        border.Position = pos;
297        border.CollisionIgnoreGroup = 0;
298        border.Image = borderImg;
299        border.Tag = "border";
300        Add(border);
301    }
302
303
304    void CreateGoal(Vector pos, double width, double height)
305    {
306        goal = PhysicsObject.CreateStaticObject(width, height);
307        Image goalImg = Game.LoadImage("imgs/goal");
308        goal.Position = pos;
309        goal.Image = goalImg;
310        goal.CollisionIgnoreGroup = 1;
311        goal.Tag = "goal";
312        Add(goal);
313    }
314
315    void CreateBgWall(Vector pos, double width, double height)
316    {
317        GameObject wall = new GameObject(width, height);
318        Image wallImg = Game.LoadImage("imgs/wall/wall");
319        wall.Position = pos;
320        wall.Image = wallImg;
321        Add(wall, -1);
322    }
323
324    void ExitGame()
325    {
326        Exit();
327    }
328
329}
Note: See TracBrowser for help on using the repository browser.