1 | using System; |
---|
2 | using Jypeli; |
---|
3 | using Jypeli.Widgets; |
---|
4 | using Jypeli.Assets; |
---|
5 | |
---|
6 | public class Peli : PhysicsGame |
---|
7 | { |
---|
8 | Timer kello; |
---|
9 | PhysicsObject pelaaja; |
---|
10 | PhysicsObject minion; |
---|
11 | PlasmaCannon plasmatykki; |
---|
12 | |
---|
13 | protected override void Begin() |
---|
14 | { |
---|
15 | MessageDisplay.TextColor = Color.White; |
---|
16 | kello = LuoLahettaja(); |
---|
17 | Tuho(); |
---|
18 | Level.BackgroundColor = Color.Black; |
---|
19 | pelaaja = Pelaaja(); |
---|
20 | |
---|
21 | } |
---|
22 | |
---|
23 | PhysicsObject Pelaaja() |
---|
24 | { |
---|
25 | PhysicsObject pelaaja = new PhysicsObject(50.0, 50.0); |
---|
26 | pelaaja.Color = Color.Blue; |
---|
27 | pelaaja.Shape = Shapes.Triangle; |
---|
28 | pelaaja.IgnoresCollisionResponse = true; |
---|
29 | plasmatykki = new PlasmaCannon(20, 20); |
---|
30 | plasmatykki.Angle = Angle.Degrees(90); |
---|
31 | plasmatykki.PlasmaParticleCollision = PlasmaAmmusOsuma; |
---|
32 | pelaaja.Add(plasmatykki); |
---|
33 | Keyboard.Listen(Key.Space, ButtonState.Down, plasmatykki.Use, "Tulta"); |
---|
34 | Keyboard.Listen(Key.Left, ButtonState.Down, Vauhdikkuus, "Siirry vasemmalle", new Vector(-200.0, 0.0)); |
---|
35 | Keyboard.Listen(Key.Left, ButtonState.Released, Vauhdikkuus, null, Vector.Zero); |
---|
36 | Keyboard.Listen(Key.Right, ButtonState.Down, Vauhdikkuus, "Sirry oikealle", new Vector (200.0, 0.0)); |
---|
37 | Keyboard.Listen(Key.Right, ButtonState.Released, Vauhdikkuus, null, Vector.Zero); |
---|
38 | Keyboard.Listen(Key.Up, ButtonState.Down, Vauhdikkuus, "Eespäin", new Vector (0.0, 200.0)); |
---|
39 | Keyboard.Listen(Key.Up, ButtonState.Released, Vauhdikkuus, null, Vector.Zero); |
---|
40 | Keyboard.Listen(Key.Down, ButtonState.Down, Vauhdikkuus, "Peräänny", new Vector (0.0, -200.0)); |
---|
41 | Keyboard.Listen(Key.Down, ButtonState.Released, Vauhdikkuus, null, Vector.Zero); |
---|
42 | Add(pelaaja); |
---|
43 | return pelaaja; |
---|
44 | } |
---|
45 | |
---|
46 | PhysicsObject Minion() |
---|
47 | { |
---|
48 | minion = new PhysicsObject(50.0, 50.0); |
---|
49 | minion.Color = Color.DarkGray; |
---|
50 | minion.Shape = Shapes.Triangle; |
---|
51 | minion.Angle = Angle.Degrees(180); |
---|
52 | double vaakasijainti = RandomGen.NextDouble(Level.Left, Level.Right); |
---|
53 | minion.Y = Level.Top + 200; |
---|
54 | minion.X = vaakasijainti; |
---|
55 | Add(minion); |
---|
56 | PlasmaCannon minionPlasma = new PlasmaCannon(20, 5); |
---|
57 | minionPlasma.Angle = Angle.Degrees(90); |
---|
58 | minionPlasma.PlasmaParticleCollision = VastustajienTuli; |
---|
59 | minion.Add(minionPlasma); |
---|
60 | Vector[] reitti = new Vector[2]; |
---|
61 | reitti[0] = new Vector(0, Level.Top); |
---|
62 | reitti[1] = new Vector(vaakasijainti, Level.Bottom - 200); |
---|
63 | PathFollowerBrain aivot = new PathFollowerBrain(reitti); |
---|
64 | minion.Brain = aivot; |
---|
65 | aivot.Speed = 200; |
---|
66 | aivot.Active = true; |
---|
67 | Timer ampumajastin = new Timer(); |
---|
68 | ampumajastin.Interval = 0.3; |
---|
69 | ampumajastin.Tag = minionPlasma; |
---|
70 | ampumajastin.Trigger += VihollinenAmpuu; |
---|
71 | ampumajastin.Start(); |
---|
72 | |
---|
73 | return minion; |
---|
74 | } |
---|
75 | |
---|
76 | void VihollinenAmpuu(Timer ajastin) |
---|
77 | { |
---|
78 | ((PlasmaCannon)ajastin.Tag).Shoot(); |
---|
79 | } |
---|
80 | |
---|
81 | Timer LuoLahettaja() |
---|
82 | { |
---|
83 | Timer kello = new Timer(); |
---|
84 | kello.Interval = 0.6; |
---|
85 | kello.Trigger += LahetaMinion; |
---|
86 | kello.Start(); |
---|
87 | return kello; |
---|
88 | } |
---|
89 | |
---|
90 | PhysicsObject Tuho() |
---|
91 | { |
---|
92 | PhysicsObject tuho = PhysicsObject.CreateStaticObject(Screen.Width, 60.0); |
---|
93 | tuho.Y = Level.Bottom - 200; |
---|
94 | Add(tuho); |
---|
95 | AddCollisionHandler(tuho, Tyrmays); |
---|
96 | return tuho; |
---|
97 | } |
---|
98 | |
---|
99 | void Tyrmays(PhysicsObject tyrmaaja, PhysicsObject kohde) |
---|
100 | { |
---|
101 | kohde.Destroy(); |
---|
102 | } |
---|
103 | |
---|
104 | void PlasmaAmmusOsuma(PhysicsObject ammus, PhysicsObject kohde) |
---|
105 | { |
---|
106 | |
---|
107 | if (kohde != pelaaja) |
---|
108 | { |
---|
109 | kohde.Destroy(); |
---|
110 | MessageDisplay.Add("Vihollisalus tuhottu"); |
---|
111 | } |
---|
112 | |
---|
113 | } |
---|
114 | |
---|
115 | void VastustajienTuli(PhysicsObject ammus, PhysicsObject kohde) |
---|
116 | { |
---|
117 | if (kohde == pelaaja) |
---|
118 | { |
---|
119 | kohde.Destroy(); |
---|
120 | MessageDisplay.Add("Game Over"); |
---|
121 | } |
---|
122 | } |
---|
123 | |
---|
124 | void LahetaMinion(Timer sender) |
---|
125 | { |
---|
126 | Minion(); |
---|
127 | } |
---|
128 | |
---|
129 | void Vauhdikkuus(Vector suunta) |
---|
130 | { |
---|
131 | pelaaja.Velocity = suunta; |
---|
132 | } |
---|
133 | |
---|
134 | } |
---|