1 | using System; |
---|
2 | using System.Collections.Generic; |
---|
3 | using Jypeli; |
---|
4 | using Jypeli.Assets; |
---|
5 | using Jypeli.Controls; |
---|
6 | using Jypeli.Effects; |
---|
7 | using Jypeli.Widgets; |
---|
8 | |
---|
9 | public class JalkapalloPeli2 : PhysicsGame |
---|
10 | { |
---|
11 | |
---|
12 | Pelaaja pelaaja1, pelaaja2; |
---|
13 | PhysicsObject pallo; |
---|
14 | PhysicsStructure maaliVasen, maaliOikea; |
---|
15 | PhysicsObject tolppaVasen, tolppaOikea; |
---|
16 | PhysicsObject kentta, katsomoVasen, katsomoOikea; |
---|
17 | |
---|
18 | ProgressBar pelaaja1Energia, pelaaja2Energia; |
---|
19 | IntMeter pelaaja1Maalit, pelaaja2Maalit; |
---|
20 | |
---|
21 | public override void Begin() |
---|
22 | { |
---|
23 | AloitaPeli(); |
---|
24 | } |
---|
25 | |
---|
26 | public void AloitaPeli() |
---|
27 | { |
---|
28 | ClearAll(); |
---|
29 | AlustaKentta(); |
---|
30 | AsetaNappaimet(); |
---|
31 | LuoHUD(); |
---|
32 | } |
---|
33 | |
---|
34 | public void AlustaKentta() |
---|
35 | { |
---|
36 | Gravity = new Vector(0, -300); |
---|
37 | |
---|
38 | kentta = Surface.CreateBottom(Level); |
---|
39 | katsomoVasen = Surface.CreateLeft(Level); |
---|
40 | katsomoVasen.Image = LoadImage("katsomo2"); |
---|
41 | katsomoOikea = Surface.CreateRight(Level); |
---|
42 | katsomoOikea.Image = LoadImage("katsomoRight"); |
---|
43 | |
---|
44 | |
---|
45 | |
---|
46 | Surface katto = Surface.CreateTop(Level); |
---|
47 | katto.IsVisible = false; |
---|
48 | Add(katto); |
---|
49 | Add(kentta); |
---|
50 | Add(katsomoVasen); |
---|
51 | Add(katsomoOikea); |
---|
52 | |
---|
53 | |
---|
54 | maaliVasen = LuoMaali(new Vector(Level.Left, Level.Bottom), true); |
---|
55 | maaliOikea = LuoMaali(new Vector(Level.Right, Level.Bottom), false); |
---|
56 | |
---|
57 | pelaaja1 = LuoPelaaja(new Vector(Level.Left + 100, Level.Bottom + 30)); |
---|
58 | pelaaja2 = LuoPelaaja(new Vector(Level.Right - 100, Level.Bottom + 30)); |
---|
59 | |
---|
60 | pallo = new PhysicsObject(20, 20); |
---|
61 | pallo.Shape = Shape.Circle; |
---|
62 | |
---|
63 | pallo.CollisionIgnoreGroup = 1; |
---|
64 | tolppaOikea.CollisionIgnoreGroup = 1; |
---|
65 | tolppaVasen.CollisionIgnoreGroup = 1; |
---|
66 | |
---|
67 | Add(pallo); |
---|
68 | |
---|
69 | Camera.ZoomTo(new Vector(kentta.Left, kentta.Bottom + kentta.Height / 2), new Vector(kentta.Right, katto.Bottom)); |
---|
70 | } |
---|
71 | |
---|
72 | public void AsetaNappaimet() |
---|
73 | { |
---|
74 | PhoneBackButton.Listen(ConfirmExit, "Lopeta peli"); |
---|
75 | Keyboard.Listen(Key.Escape, ButtonState.Pressed, ConfirmExit, "Lopeta peli"); |
---|
76 | |
---|
77 | // Pelaaja 1 |
---|
78 | ControllerOne.ListenAnalog(AnalogControl.LeftStick, 0.1, LiikutaPelaajaa, "", pelaaja1); |
---|
79 | ControllerOne.ListenAnalog(AnalogControl.RightTrigger, 0.1, Potkaise, "", pelaaja1, ControllerOne); |
---|
80 | ControllerOne.Listen(Button.LeftStick, ButtonState.Down, Sprinttaa, "", pelaaja1, true); |
---|
81 | ControllerOne.Listen(Button.LeftStick, ButtonState.Released, Sprinttaa, "", pelaaja1, false); |
---|
82 | ControllerOne.ListenAnalog(AnalogControl.LeftTrigger, 0.1, Hyppaa, "", pelaaja1); |
---|
83 | |
---|
84 | // Pelaaja 2 |
---|
85 | ControllerTwo.ListenAnalog(AnalogControl.LeftStick, 0.1, LiikutaPelaajaa, "", pelaaja2); |
---|
86 | ControllerTwo.ListenAnalog(AnalogControl.RightTrigger, 0.1, Potkaise, "", pelaaja2, ControllerTwo); |
---|
87 | ControllerTwo.Listen(Button.LeftStick, ButtonState.Down, Sprinttaa, "", pelaaja2, true); |
---|
88 | ControllerTwo.Listen(Button.LeftStick, ButtonState.Released, Sprinttaa, "", pelaaja2, false); |
---|
89 | ControllerTwo.ListenAnalog(AnalogControl.LeftTrigger, 0.1, Hyppaa, "", pelaaja2); |
---|
90 | } |
---|
91 | |
---|
92 | public void LiikutaPelaajaa(AnalogState state, Pelaaja pelaaja) |
---|
93 | { |
---|
94 | //pelaaja.Move(new Vector(state.StateVector.X * 100, 0)); |
---|
95 | pelaaja.Velocity = new Vector(state.StateVector.X * (pelaaja.Nopeus + pelaaja.Sprinttaa), pelaaja.Velocity.Y); |
---|
96 | } |
---|
97 | |
---|
98 | public void Sprinttaa(Pelaaja pelaaja, bool pohjassa) |
---|
99 | { |
---|
100 | if (!pohjassa || pelaaja.Energia <= 0) |
---|
101 | { |
---|
102 | pelaaja.Sprinttaa = 0; |
---|
103 | } |
---|
104 | else |
---|
105 | { |
---|
106 | pelaaja.Sprinttaa = 150; |
---|
107 | } |
---|
108 | } |
---|
109 | |
---|
110 | public void Potkaise(AnalogState state, Pelaaja pelaaja, GamePad controller) |
---|
111 | { |
---|
112 | pelaaja.Potkaise(pallo, controller.RightThumbDirection * 650); |
---|
113 | } |
---|
114 | |
---|
115 | public void Hyppaa(AnalogState state, Pelaaja pelaaja) |
---|
116 | { |
---|
117 | pelaaja.Jump(200); |
---|
118 | //pelaaja.Hyppaa(new Vector(0, 200)); |
---|
119 | } |
---|
120 | |
---|
121 | public PhysicsStructure LuoMaali(Vector pos, bool onkoVasen) |
---|
122 | { |
---|
123 | PhysicsObject tolppa = PhysicsObject.CreateStaticObject(10, 100); |
---|
124 | tolppa.Color = Color.White; |
---|
125 | tolppa.Mass = Int16.MaxValue; |
---|
126 | tolppa.CanRotate = false; |
---|
127 | if (onkoVasen) |
---|
128 | tolppaVasen = tolppa; |
---|
129 | else |
---|
130 | tolppaOikea = tolppa; |
---|
131 | //Add(tolppa); |
---|
132 | PhysicsObject pystyRima = PhysicsObject.CreateStaticObject(60, 10); |
---|
133 | pystyRima.Color = Color.White; |
---|
134 | pystyRima.CanRotate = false; |
---|
135 | //Add(tolppa); |
---|
136 | if (onkoVasen) |
---|
137 | pystyRima.Position = new Vector(tolppa.X - pystyRima.Width / 2, tolppa.Top); |
---|
138 | else |
---|
139 | pystyRima.Position = new Vector(tolppa.X + pystyRima.Width / 2, tolppa.Top); |
---|
140 | PhysicsStructure maali = new PhysicsStructure(); |
---|
141 | maali.IgnoresGravity = true; |
---|
142 | maali.Add(tolppa); |
---|
143 | maali.Add(pystyRima); |
---|
144 | |
---|
145 | AxleJoint liitos; |
---|
146 | if (onkoVasen) |
---|
147 | liitos = new AxleJoint(tolppa, pystyRima, new Vector(tolppa.Top, pystyRima.Right)); |
---|
148 | else |
---|
149 | liitos = new AxleJoint(tolppa, pystyRima, new Vector(tolppa.Top, pystyRima.Right)); |
---|
150 | liitos.Softness = 0; |
---|
151 | |
---|
152 | if (onkoVasen) |
---|
153 | maali.Position = new Vector(pos.X + maali.Width, pos.Y + maali.Height / 2); |
---|
154 | else |
---|
155 | maali.Position = new Vector(pos.X - maali.Width, pos.Y + maali.Height / 2); |
---|
156 | Add(maali); |
---|
157 | return maali; |
---|
158 | |
---|
159 | } |
---|
160 | |
---|
161 | public Pelaaja LuoPelaaja(Vector pos) |
---|
162 | { |
---|
163 | Pelaaja pelaaja = new Pelaaja(20, 40); |
---|
164 | pelaaja.Position = pos; |
---|
165 | pelaaja.CanRotate = false; |
---|
166 | pelaaja.Restitution = 0.0; |
---|
167 | //pelaaja.MaintainMomentum = true; |
---|
168 | pelaaja.Mass = 300; |
---|
169 | pelaaja.LahtoNopeus = 200; |
---|
170 | pelaaja.MaksimiNopeus = 350; |
---|
171 | |
---|
172 | AddCollisionHandler(pelaaja, tormays); |
---|
173 | Add(pelaaja); |
---|
174 | return pelaaja; |
---|
175 | } |
---|
176 | |
---|
177 | public void tormays(PhysicsObject tormaaja, PhysicsObject kohde) |
---|
178 | { |
---|
179 | if (kohde == kentta) |
---|
180 | (tormaaja as Pelaaja).Laskeudu(); |
---|
181 | } |
---|
182 | |
---|
183 | public void Maali(int pelaaja) |
---|
184 | { |
---|
185 | pallo.Stop(); |
---|
186 | pallo.Position = Level.Center; |
---|
187 | if (pelaaja == 1) |
---|
188 | pelaaja1Maalit.Value++; |
---|
189 | if (pelaaja == 2) |
---|
190 | pelaaja2Maalit.Value++; |
---|
191 | } |
---|
192 | |
---|
193 | protected override void Update(Time time) |
---|
194 | { |
---|
195 | if (pallo.Right < tolppaVasen.Left && pallo.Top < tolppaVasen.Top) |
---|
196 | Maali(1); |
---|
197 | if (pallo.Left > tolppaOikea.Right && pallo.Top < tolppaOikea.Top) |
---|
198 | Maali(2); |
---|
199 | base.Update(time); |
---|
200 | } |
---|
201 | |
---|
202 | public void LuoHUD() |
---|
203 | { |
---|
204 | pelaaja1Energia = new ProgressBar(100, 20); |
---|
205 | pelaaja1Energia.BindTo(pelaaja1.Energia); |
---|
206 | pelaaja1Energia.Position = new Vector(Screen.LeftSafe + 250, Screen.TopSafe - 100); |
---|
207 | pelaaja1Energia.BorderColor = Color.Black; |
---|
208 | pelaaja1Energia.Color = Color.Blue; |
---|
209 | Add(pelaaja1Energia); |
---|
210 | |
---|
211 | pelaaja2Energia = new ProgressBar(100, 20); |
---|
212 | pelaaja2Energia.BindTo(pelaaja2.Energia); |
---|
213 | pelaaja2Energia.Position = new Vector(Screen.RightSafe - 250, Screen.TopSafe - 100); |
---|
214 | pelaaja2Energia.BorderColor = Color.Black; |
---|
215 | pelaaja2Energia.Color = Color.Blue; |
---|
216 | Add(pelaaja2Energia); |
---|
217 | |
---|
218 | pelaaja1Maalit = new IntMeter(0); |
---|
219 | pelaaja1Maalit.MaxValue = 10; |
---|
220 | Label pelaaja1Naytto = new Label(); |
---|
221 | pelaaja1Naytto.Position = new Vector(Screen.LeftSafe + 250, Screen.TopSafe - 70); |
---|
222 | pelaaja1Naytto.BindTo(pelaaja1Maalit); |
---|
223 | Add(pelaaja1Naytto); |
---|
224 | |
---|
225 | pelaaja2Maalit = new IntMeter(0); |
---|
226 | pelaaja2Maalit.MaxValue = 10; |
---|
227 | Label pelaaja2Naytto = new Label(); |
---|
228 | pelaaja2Naytto.Position = new Vector(Screen.RightSafe - 250, Screen.TopSafe - 70); |
---|
229 | pelaaja2Naytto.BindTo(pelaaja2Maalit); |
---|
230 | Add(pelaaja2Naytto); |
---|
231 | } |
---|
232 | } |
---|