1 | using System; |
---|
2 | using Jypeli; |
---|
3 | using Jypeli.Widgets; |
---|
4 | using Jypeli.Assets; |
---|
5 | |
---|
6 | public class Peli : PhysicsGame |
---|
7 | { |
---|
8 | |
---|
9 | const double nopeus = 400; |
---|
10 | const double hyppyVoima = 1500; |
---|
11 | |
---|
12 | |
---|
13 | const int ruudunLeveys = 50; |
---|
14 | const int ruudunKorkeus = 50; |
---|
15 | |
---|
16 | PlatformCharacter nemesis; |
---|
17 | GameObject nemesisMiekka; |
---|
18 | PlatformCharacter nemesis2; |
---|
19 | GameObject nemesis2Miekka; |
---|
20 | |
---|
21 | IntMeter pelaajan1Pisteet; |
---|
22 | IntMeter pelaajan2Pisteet; |
---|
23 | |
---|
24 | |
---|
25 | protected override void Begin() |
---|
26 | { |
---|
27 | LuoKentta(); |
---|
28 | LisaaLaskurit(); |
---|
29 | |
---|
30 | } |
---|
31 | |
---|
32 | void LuoKentta() |
---|
33 | { |
---|
34 | |
---|
35 | Gravity = new Vector(0, -1500); |
---|
36 | TileMap ruudut = TileMap.FromFile("kentta.txt"); |
---|
37 | ruudut['='] = LuoPalikka; |
---|
38 | ruudut.Insert(ruudunLeveys, ruudunKorkeus); |
---|
39 | |
---|
40 | LisaaPelaajat(); |
---|
41 | AsetaOhjaimet(); |
---|
42 | Camera.ZoomToLevel(); |
---|
43 | |
---|
44 | } |
---|
45 | |
---|
46 | void LisaaPelaajat() |
---|
47 | { |
---|
48 | nemesis = new PlatformCharacter(100, 100); |
---|
49 | nemesis.Shape = Shapes.Circle; |
---|
50 | nemesis.Image = LoadImage("Nemesis1"); |
---|
51 | nemesis.X = -100.0; |
---|
52 | nemesis.Y = -200.0; |
---|
53 | nemesis.RightWalkingAnimation = new Animation(LoadImage("Nemesis1 kävely1")); |
---|
54 | nemesis.LeftWalkingAnimation = new Animation(Image.Mirror(LoadImage("Nemesis1 kävely1"))); |
---|
55 | AddCollisionHandler(nemesis, osumaLattiaan); |
---|
56 | |
---|
57 | |
---|
58 | Add(nemesis); |
---|
59 | |
---|
60 | nemesis2 = new PlatformCharacter(100, 100); |
---|
61 | nemesis2.Shape = Shapes.Circle; |
---|
62 | nemesis2.Image = LoadImage("Nemesis2"); |
---|
63 | nemesis2.X = 100.0; |
---|
64 | nemesis2.Y = -200.0; |
---|
65 | nemesis2.RightWalkingAnimation = new Animation(Image.Mirror(LoadImage("Nemesis2"))); |
---|
66 | nemesis2.LeftWalkingAnimation = new Animation(LoadImage("Nemesis2")); |
---|
67 | |
---|
68 | AddCollisionHandler(nemesis2, osumaLattiaan); |
---|
69 | Add(nemesis2); |
---|
70 | |
---|
71 | } |
---|
72 | |
---|
73 | |
---|
74 | |
---|
75 | PhysicsObject LuoPalikka() |
---|
76 | { |
---|
77 | PhysicsObject palikka = PhysicsObject.CreateStaticObject(50.0, 50.0); |
---|
78 | palikka.Shape = Shapes.Rectangle; |
---|
79 | palikka.Color = Color.Black; |
---|
80 | return palikka; |
---|
81 | } |
---|
82 | |
---|
83 | void AsetaOhjaimet() |
---|
84 | { |
---|
85 | Keyboard.Listen(Key.A, ButtonState.Down, Liikuta, null, nemesis, -nopeus); |
---|
86 | Keyboard.Listen(Key.D, ButtonState.Down, Liikuta, null, nemesis, nopeus); |
---|
87 | Keyboard.Listen(Key.A, ButtonState.Released, nemesis.StopHorizontal, null); |
---|
88 | Keyboard.Listen(Key.D, ButtonState.Released, nemesis.StopHorizontal, null); |
---|
89 | Keyboard.Listen(Key.W, ButtonState.Pressed, Hyppaa, "Hyppää", nemesis, hyppyVoima); |
---|
90 | Keyboard.Listen(Key.LeftControl, ButtonState.Pressed, Lyo, "Lyö", nemesis); |
---|
91 | |
---|
92 | Keyboard.Listen(Key.Left, ButtonState.Down, Liikuta, null, nemesis2, -nopeus); |
---|
93 | Keyboard.Listen(Key.Right, ButtonState.Down, Liikuta, null, nemesis2, nopeus); |
---|
94 | Keyboard.Listen(Key.Left, ButtonState.Released, nemesis2.StopHorizontal, null); |
---|
95 | Keyboard.Listen(Key.Right, ButtonState.Released, nemesis2.StopHorizontal, null); |
---|
96 | Keyboard.Listen(Key.Up, ButtonState.Pressed, Hyppaa, "Hyppää", nemesis2, hyppyVoima); |
---|
97 | Keyboard.Listen(Key.RightControl, ButtonState.Pressed, Lyo, "Lyö", nemesis2); |
---|
98 | |
---|
99 | Keyboard.Listen(Key.F1, ButtonState.Pressed, ShowControlHelp, "Näytä ohjeet"); |
---|
100 | Keyboard.Listen(Key.Escape, ButtonState.Pressed, Exit, "Poistu"); |
---|
101 | } |
---|
102 | |
---|
103 | void Lyo(PlatformCharacter hahmo) |
---|
104 | { |
---|
105 | Image i; |
---|
106 | if (hahmo == nemesis) |
---|
107 | { |
---|
108 | if (nemesis.X < nemesis2.X) |
---|
109 | { |
---|
110 | i = LoadImage("Nemesis1 attack"); |
---|
111 | } |
---|
112 | else |
---|
113 | { |
---|
114 | i = Image.Mirror(LoadImage("Nemesis1 attack")); |
---|
115 | } |
---|
116 | } |
---|
117 | else |
---|
118 | { |
---|
119 | if (nemesis.X > nemesis2.X) |
---|
120 | { |
---|
121 | i = Image.Mirror(LoadImage("Nemesis2 attack")); |
---|
122 | } |
---|
123 | else |
---|
124 | { |
---|
125 | i = LoadImage("Nemesis2 attack"); |
---|
126 | } |
---|
127 | } |
---|
128 | Image[] tekstuurit = new Image[] { i }; |
---|
129 | Animation a = new Animation(tekstuurit); |
---|
130 | hahmo.Animation = a; |
---|
131 | } |
---|
132 | |
---|
133 | void Liikuta(PlatformCharacter hahmo, double nopeus) |
---|
134 | { |
---|
135 | hahmo.Walk(nopeus); |
---|
136 | } |
---|
137 | |
---|
138 | void Hyppaa(PlatformCharacter hahmo, double voima) |
---|
139 | { |
---|
140 | Image[] tekstuurit = LoadImages( |
---|
141 | "nemesis1 hyppy", |
---|
142 | "nemesis2 hyppy"); |
---|
143 | |
---|
144 | Animation hyppyTekstuuri = new Animation(tekstuurit); |
---|
145 | hyppyTekstuuri.FPS = 5; |
---|
146 | |
---|
147 | if (hahmo == nemesis) |
---|
148 | { |
---|
149 | nemesis.Image = tekstuurit[0]; |
---|
150 | nemesis.Tag = "ilmassa"; |
---|
151 | } |
---|
152 | if (hahmo == nemesis2) |
---|
153 | { |
---|
154 | nemesis2.Image = tekstuurit[1]; |
---|
155 | nemesis2.Tag = "ilmassa"; |
---|
156 | } |
---|
157 | |
---|
158 | hahmo.Jump(voima); |
---|
159 | } |
---|
160 | void LisaaLaskurit() |
---|
161 | { |
---|
162 | pelaajan1Pisteet = LuoPisteLaskuri(Screen.Left + 100.0, Screen.Top - 100.0); |
---|
163 | pelaajan2Pisteet = LuoPisteLaskuri(Screen.Right - 100.0, Screen.Top - 100.0); |
---|
164 | } |
---|
165 | IntMeter LuoPisteLaskuri( double x, double y ) |
---|
166 | { |
---|
167 | IntMeter laskuri = new IntMeter( 0 ); |
---|
168 | laskuri.MaxValue = 50; |
---|
169 | Label naytto = new Label(); |
---|
170 | naytto.BindTo( laskuri ); |
---|
171 | naytto.X = x; |
---|
172 | naytto.Y = y; |
---|
173 | naytto.TextColor = Color.Green; |
---|
174 | Add( naytto ); |
---|
175 | return laskuri; |
---|
176 | } |
---|
177 | |
---|
178 | void osumaLattiaan(PhysicsObject osuja, PhysicsObject kohde) |
---|
179 | { |
---|
180 | if (osuja == nemesis && osuja.Y > kohde.Y && osuja.Tag.ToString() == "ilmassa") |
---|
181 | { |
---|
182 | osuja.Image = LoadImage("Nemesis1"); |
---|
183 | osuja.Tag = "maassa"; |
---|
184 | } |
---|
185 | if (osuja == nemesis2 && osuja.Y > kohde.Y && osuja.Tag.ToString() == "ilmassa") |
---|
186 | { |
---|
187 | osuja.Image = LoadImage("Nemesis2"); |
---|
188 | osuja.Tag = "maassa"; |
---|
189 | } |
---|
190 | } |
---|
191 | |
---|
192 | } |
---|