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

Revision 3782, 6.5 KB checked in by anlakane, 8 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    GameObject qwe;
17
18    int mapNum = 1;
19
20    //Animaatiot
21    Image[] playerImg = Game.LoadImages("imgs/player/player", "imgs/player/player2", "imgs/player/player3");
22    Image[] jumpAni = Game.LoadImages("imgs/player/playerJump", "imgs/player/playerJump2", "imgs/player/playerJump3");
23    Image[] breakWindow = Game.LoadImages("imgs/animations/window/break", "imgs/animations/window/break2", "imgs/animations/window/break3", "imgs/animations/window/break4");
24
25    public override void Begin()
26    {
27        // TODO: Kirjoita ohjelmakoodisi tähän
28        NextMap();
29        IsMouseVisible = true;
30        IsFullScreen = true;
31    }
32
33    void NextMap()
34    {
35        ClearAll();
36
37        Gravity = new Vector(0.0, -800.0);
38        if (mapNum == 1) CreateMap("maps/map1");
39        else if (mapNum == 2) CreateMap("map2");
40
41        Level.Background.FitToLevel();
42
43        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");
44        Animation taustaAni = new Animation(tausta);
45        taustaAni.FPS = 10;
46        taustaAni.Start();
47
48        qwe = new GameObject(Screen.Width, Screen.Height);
49        qwe.X = Level.Left;
50        qwe.Animation = taustaAni;
51        Add(qwe, -1);
52
53       
54    }
55
56    void CreateMap(string fileName)
57    {
58        ColorTileMap tile = ColorTileMap.FromLevelAsset(fileName);
59       
60        tile.SetTileMethod(Color.FromHexCode("00ff00"), CreatePlayer);
61       
62        tile.SetTileMethod(Color.Black, CreateHouse);
63        tile.SetTileMethod(Color.DarkGray, CreateRoof);
64        tile.SetTileMethod(Color.White, CreateWindow);
65        tile.SetTileMethod(Color.Yellow, CreateGoal);
66        tile.SetTileMethod(Color.FromHexCode("ff0000"), CreateBorder);
67
68        tile.SetTileMethod(Color.FromHexCode("cccccc"), CreateBgWall);
69
70        tile.Execute(32, 32);
71    }
72
73
74    void CreatePlayer(Vector pos, double width, double height)
75    {
76        player = new PlatformCharacter(21, 45);
77        player.Position = pos;
78        player.Animation = new Animation(playerImg);
79        player.Animation.FPS = 5;
80        player.Animation.Start();
81        Camera.FollowX(player);
82
83        AddCollisionHandler<PlatformCharacter, PhysicsObject>(player, "break", onHitWindow);
84        AddCollisionHandler<PlatformCharacter, PhysicsObject>(player, "die", onHitHouse);
85        AddCollisionHandler<PlatformCharacter, PhysicsObject>(player, "border", onHitHouse);
86        AddCollisionHandler<PlatformCharacter, PhysicsObject>(player, "goal", onHitGoal);
87
88        Keyboard.Listen(Key.Space, ButtonState.Pressed, Jump, "", player, 650.0);
89        Keyboard.Listen(Key.LeftControl, ButtonState.Pressed, Jump, "", player, 800.0);
90        Keyboard.Listen(Key.R, ButtonState.Pressed, NextMap, "");
91        Keyboard.Listen(Key.Escape, ButtonState.Pressed, ExitGame, "");
92
93
94        ControllerOne.Listen(Button.A, ButtonState.Pressed, Jump, "", player, 650.0);
95        ControllerOne.Listen(Button.B, ButtonState.Pressed, Jump, "", player, 800.0);
96        ControllerOne.Listen(Button.X, ButtonState.Pressed, Jump, "", player, 800.0);
97        ControllerOne.Listen(Button.RightShoulder, ButtonState.Pressed, NextMap, "");
98        ControllerOne.Listen(Button.Back, ButtonState.Pressed, ExitGame, "");
99        Add(player);
100    }
101
102    void onHitWindow(PlatformCharacter player, PhysicsObject window)
103    {
104        window.Destroy();       
105    }
106
107    void onHitHouse(PlatformCharacter player, PhysicsObject house)
108    {
109        player.Destroy();
110        NextMap();
111
112    }
113
114    void onHitGoal(PlatformCharacter player, PhysicsObject house)
115    {
116        mapNum++;
117        NextMap();
118    }
119
120    void Jump(PlatformCharacter player, double jump)
121    {
122        ControllerOne.Vibrate(0.1, 0.1, 1, 1, 0.5);
123        Phone.Vibrate(100);
124        player.Jump(jump);
125    }
126
127    protected override void Update(Time time)
128    {
129        if (player != null)
130        {
131            qwe.X = player.X;
132            player.Walk(150);
133        }
134        base.Update(time);
135    }
136
137   
138
139    void CreateHouse(Vector pos, double width, double height)
140    {
141        house = PhysicsObject.CreateStaticObject(width, height);
142        Image houseImg = Game.LoadImage("imgs/housePart");
143        house.Position = pos;
144        house.Image = houseImg;
145        house.CollisionIgnoreGroup = 1;
146        house.Tag = "die";
147        Add(house);
148    }
149
150    void CreateRoof(Vector pos, double width, double height)
151    {
152        PhysicsObject roof = PhysicsObject.CreateStaticObject(width, height);
153        Image roofImg = Game.LoadImage("imgs/roof");
154        roof.Position = pos;
155        roof.Image = roofImg;
156        roof.CollisionIgnoreGroup = 1;
157        Add(roof);
158    }
159
160    void CreateWindow(Vector pos, double width, double height)
161    {
162        window = PhysicsObject.CreateStaticObject(16, height);
163        Image windowImg = Game.LoadImage("imgs/window");
164        window.Position = pos;
165        window.Image = windowImg;
166        window.CollisionIgnoreGroup = 1;
167        window.Tag = "break";
168        Add(window);
169    }
170
171    void CreateBorder(Vector pos, double width, double height)
172    {
173        border = PhysicsObject.CreateStaticObject(16, height);
174        Image borderImg = Game.LoadImage("imgs/border");
175        border.Position = pos;
176        border.Image = borderImg;
177        border.Tag = "border";
178        Add(border);
179    }
180
181
182    void CreateGoal(Vector pos, double width, double height)
183    {
184        goal = PhysicsObject.CreateStaticObject(width, height);
185        Image goalImg = Game.LoadImage("imgs/goal");
186        goal.Position = pos;
187        goal.Image = goalImg;
188        goal.CollisionIgnoreGroup = 1;
189        goal.Tag = "goal";
190        Add(goal);
191    }
192
193    void CreateBgWall(Vector pos, double width, double height)
194    {
195        GameObject wall = new GameObject(width, height);
196        Image wallImg = Game.LoadImage("imgs/wall/wall");
197        wall.Position = pos;
198        wall.Image = wallImg;
199        Add(wall);
200    }
201
202    void ExitGame()
203    {
204        Exit();
205    }
206
207}
Note: See TracBrowser for help on using the repository browser.