1 | using System; |
---|
2 | using Jypeli; |
---|
3 | using Jypeli.Widgets; |
---|
4 | using Jypeli.ScreenObjects; |
---|
5 | using Jypeli.Assets; |
---|
6 | using Jypeli.Effects; |
---|
7 | |
---|
8 | public class Peli : PhysicsGame |
---|
9 | { |
---|
10 | PhysicsObject kone1; |
---|
11 | PhysicsObject kone2; |
---|
12 | ExplosionSystem systeemi; |
---|
13 | |
---|
14 | |
---|
15 | int Pelaaja1NosteenSuunta = 1; |
---|
16 | int Pelaaja2NosteenSuunta = 1; |
---|
17 | |
---|
18 | Animation kone1_kaantyy_animation; |
---|
19 | Animation kone2_kaantyy_animation; |
---|
20 | |
---|
21 | protected override void Begin() |
---|
22 | { |
---|
23 | LuoKentta(); |
---|
24 | } |
---|
25 | |
---|
26 | void LuoKentta() |
---|
27 | { |
---|
28 | Level.BackgroundColor = Color.LightBlue; |
---|
29 | Gravity = new Vector(0.0, -150.0); |
---|
30 | Level.Height = Screen.Height; |
---|
31 | Level.Width = 20000; |
---|
32 | Level.CreateBorders(); |
---|
33 | //Camera.ZoomToLevel(); |
---|
34 | //Level.CreateBottomBorder(); |
---|
35 | PhysicsObject maa = new PhysicsObject(Level.Width, 50); |
---|
36 | //maa.CollisionIgnorer. |
---|
37 | maa.Color = Color.Green; |
---|
38 | maa.Tag = "maailma"; |
---|
39 | maa.Y = Level.Bottom; |
---|
40 | |
---|
41 | Add(maa); |
---|
42 | kone1 = TeeLentokone(true); |
---|
43 | kone1.Image = LoadImage("kone1"); |
---|
44 | kone1.Tag = "kone1"; |
---|
45 | kone1.X = -100; |
---|
46 | Add(kone1); |
---|
47 | //Camera.FollowedObject = kone1.; |
---|
48 | |
---|
49 | |
---|
50 | kone2 = TeeLentokone(true); |
---|
51 | kone2.Image = LoadImage("kone2"); |
---|
52 | kone2.Tag = "kone2"; |
---|
53 | |
---|
54 | kone2.X = 100; |
---|
55 | Add(kone2); |
---|
56 | |
---|
57 | Image[] kone1_kaantyy_kuvat = LoadImages("kone1_nurin", "kone1_nurin_2", "kone1_nurin_3", "kone1_nurin_4", "kone1"); |
---|
58 | kone1_kaantyy_animation = new Animation(kone1_kaantyy_kuvat); |
---|
59 | |
---|
60 | Image[] kone2_kaantyy_kuvat = LoadImages("kone2_nurin", "kone2_nurin_2", "kone2_nurin_3", "kone2_nurin_4", "kone2"); |
---|
61 | kone2_kaantyy_animation = new Animation(kone2_kaantyy_kuvat); |
---|
62 | |
---|
63 | |
---|
64 | TeeOhjaimet(); |
---|
65 | |
---|
66 | systeemi = new ExplosionSystem(LoadImage("rajahdys"), 100, false); |
---|
67 | systeemi.MaxScale = 0.3; |
---|
68 | systeemi.MinScale = 0.1; |
---|
69 | systeemi.MaxLifetime = 0.1; |
---|
70 | systeemi.MinLifetime = 0.08; |
---|
71 | Add(systeemi); |
---|
72 | |
---|
73 | } |
---|
74 | PhysicsObject TeeLentokone(bool ihminen) |
---|
75 | { |
---|
76 | PhysicsObject kone = new PhysicsObject(30, 30); |
---|
77 | kone.Color = Color.Black; |
---|
78 | kone.IgnoresGravity = false; |
---|
79 | kone.LinearDamping = 0.985; |
---|
80 | return kone; |
---|
81 | } |
---|
82 | |
---|
83 | void TeeOhjaimet() |
---|
84 | { |
---|
85 | Keyboard.Listen(Key.Up, ButtonState.Down, LiikutaPelaajaa, null, kone1); |
---|
86 | Keyboard.Listen(Key.Right, ButtonState.Down, KaannaPelaajaa, null, kone1, -1); |
---|
87 | Keyboard.Listen(Key.Left, ButtonState.Down, KaannaPelaajaa, null, kone1, 1); |
---|
88 | Keyboard.Listen(Key.Right, ButtonState.Up, KaannaPelaajaa, null, kone1, 0); |
---|
89 | Keyboard.Listen(Key.Left, ButtonState.Up, KaannaPelaajaa, null, kone1, 0); |
---|
90 | Keyboard.Listen(Key.Down, ButtonState.Pressed, PyoritaPelaajaa, null, kone1); |
---|
91 | Keyboard.Listen(Key.RightControl, ButtonState.Pressed, Ammu, null, kone1, 1); |
---|
92 | |
---|
93 | Keyboard.Listen(Key.W, ButtonState.Down, LiikutaPelaajaa, null, kone2); |
---|
94 | Keyboard.Listen(Key.D, ButtonState.Down, KaannaPelaajaa, null, kone2, -1); |
---|
95 | Keyboard.Listen(Key.A, ButtonState.Down, KaannaPelaajaa, null, kone2, 1); |
---|
96 | Keyboard.Listen(Key.D, ButtonState.Up, KaannaPelaajaa, null, kone2, 0); |
---|
97 | Keyboard.Listen(Key.A, ButtonState.Up, KaannaPelaajaa, null, kone2, 0); |
---|
98 | Keyboard.Listen(Key.S, ButtonState.Pressed, PyoritaPelaajaa, null, kone2); |
---|
99 | Keyboard.Listen(Key.LeftControl, ButtonState.Pressed, Ammu, null, kone2, 1); |
---|
100 | } |
---|
101 | |
---|
102 | void LiikutaPelaajaa(PhysicsObject pelaaja) |
---|
103 | { |
---|
104 | pelaaja.Push(Vector.FromLengthAndAngle(300, pelaaja.Angle)); |
---|
105 | } |
---|
106 | void KaannaPelaajaa(PhysicsObject pelaaja, int nopeus) //pitäisikö riippua nopeudesta? |
---|
107 | { |
---|
108 | pelaaja.AngularVelocity = nopeus; |
---|
109 | } |
---|
110 | |
---|
111 | void PaivitaKoneenNoste(PhysicsObject kone) |
---|
112 | { |
---|
113 | Vector KoneenNokanSuunta = Vector.FromLengthAndAngle(1, kone.Angle); |
---|
114 | Vector KoneenNopeusEteen = kone.Velocity.Project(KoneenNokanSuunta); |
---|
115 | if (KoneenNopeusEteen.Magnitude != 0) |
---|
116 | { |
---|
117 | if ((kone.Velocity.Magnitude / KoneenNopeusEteen.Magnitude / 100) < 0.05) |
---|
118 | { |
---|
119 | kone.LinearDamping = 1 - (kone.Velocity.Magnitude / KoneenNopeusEteen.Magnitude / 200); |
---|
120 | } |
---|
121 | } |
---|
122 | if (kone.Tag == "kone1") |
---|
123 | { |
---|
124 | kone.Push(Vector.FromLengthAndAngle((KoneenNopeusEteen.Magnitude / 2), (kone.Angle + Angle.RightAngle) * Pelaaja1NosteenSuunta)); |
---|
125 | } |
---|
126 | else |
---|
127 | { |
---|
128 | kone.Push(Vector.FromLengthAndAngle((KoneenNopeusEteen.Magnitude / 2), (kone.Angle + Angle.RightAngle) * Pelaaja2NosteenSuunta)); |
---|
129 | } |
---|
130 | kone.Push(Vector.FromLengthAndAngle(10, kone.Angle)); //tyhjäkäynti |
---|
131 | } |
---|
132 | |
---|
133 | |
---|
134 | void PyoritaPelaajaa(PhysicsObject pelaaja) |
---|
135 | { |
---|
136 | if (pelaaja.Tag == "kone1") |
---|
137 | { |
---|
138 | Pelaaja1NosteenSuunta = Pelaaja1NosteenSuunta * -1; |
---|
139 | if (Pelaaja1NosteenSuunta == -1) |
---|
140 | { |
---|
141 | Animation kone1_kaantyy_reverse_animation = Animation.Flip(kone1_kaantyy_animation); |
---|
142 | kone1.Animation = kone1_kaantyy_reverse_animation; |
---|
143 | kone1.Animation.FPS = 6; |
---|
144 | kone1.Animation.StopOnLastFrame = true; |
---|
145 | kone1.Animation.Start(1); |
---|
146 | } |
---|
147 | else |
---|
148 | { |
---|
149 | kone1.Animation = kone1_kaantyy_animation; |
---|
150 | kone1.Animation.FPS = 6; |
---|
151 | kone1.Animation.StopOnLastFrame = true; |
---|
152 | kone1.Animation.Start(1); |
---|
153 | } |
---|
154 | } |
---|
155 | else |
---|
156 | { |
---|
157 | Pelaaja2NosteenSuunta = Pelaaja2NosteenSuunta * -1; |
---|
158 | if (Pelaaja2NosteenSuunta == -1) |
---|
159 | { |
---|
160 | Animation kone2_kaantyy_reverse_animation = Animation.Flip(kone2_kaantyy_animation); |
---|
161 | kone2.Animation = kone2_kaantyy_reverse_animation; |
---|
162 | kone2.Animation.FPS = 6; |
---|
163 | kone2.Animation.StopOnLastFrame = true; |
---|
164 | kone2.Animation.Start(1); |
---|
165 | } |
---|
166 | else |
---|
167 | { |
---|
168 | kone2.Animation = kone2_kaantyy_animation; |
---|
169 | kone2.Animation.FPS = 6; |
---|
170 | kone2.Animation.StopOnLastFrame = true; |
---|
171 | kone2.Animation.Start(1); |
---|
172 | } |
---|
173 | } |
---|
174 | |
---|
175 | } |
---|
176 | void Ammu(PhysicsObject kone, int nopeus) |
---|
177 | { |
---|
178 | PhysicsObject ammus = new PhysicsObject(4, 4); |
---|
179 | ammus.Shape = Shapes.Circle; |
---|
180 | ammus.Color = Color.Yellow; |
---|
181 | ammus.X = kone.X; |
---|
182 | ammus.Y = kone.Y; |
---|
183 | ammus.Mass = 5; |
---|
184 | ammus.IgnoresCollisionResponse = false; |
---|
185 | ammus.IgnoresExplosions = true; |
---|
186 | TimeSpan elinaika = new TimeSpan(15000000); |
---|
187 | ammus.MaximumLifetime = elinaika; |
---|
188 | ammus.Tag = kone.Tag + "ammus"; |
---|
189 | AddCollisionHandler(ammus, AmmusOsuu); |
---|
190 | Add(ammus); |
---|
191 | Angle koneenKulma = kone.Angle; |
---|
192 | Vector KoneenNokanSuunta = Vector.FromLengthAndAngle(1, kone.Angle); |
---|
193 | Vector KoneenNopeusEteen = kone.Velocity.Project(KoneenNokanSuunta); |
---|
194 | Vector impulssi = Vector.FromLengthAndAngle(KoneenNopeusEteen.Magnitude * 5 + 2500, koneenKulma); |
---|
195 | ammus.Hit(impulssi); |
---|
196 | } |
---|
197 | void AmmusOsuu(PhysicsObject ammus, PhysicsObject kohde) |
---|
198 | { |
---|
199 | if (kohde.Tag != "") |
---|
200 | { |
---|
201 | string ampuja = ammus.Tag.ToString(); |
---|
202 | string uhri = kohde.Tag.ToString(); |
---|
203 | |
---|
204 | |
---|
205 | if (ampuja[4] != uhri[4] && kohde.Tag != "ajopiste") |
---|
206 | { |
---|
207 | systeemi.AddEffect(ammus.X, ammus.Y, 5); |
---|
208 | ammus.Destroy(); |
---|
209 | } |
---|
210 | } |
---|
211 | } |
---|
212 | |
---|
213 | protected override void Update(Time time) |
---|
214 | { |
---|
215 | PaivitaKoneenNoste(kone1); |
---|
216 | PaivitaKoneenNoste(kone2); |
---|
217 | Camera.X = kone1.X; |
---|
218 | base.Update(time); |
---|
219 | } |
---|
220 | } |
---|