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 Airbattle : PhysicsGame |
---|
10 | { |
---|
11 | PhysicsObject alus; |
---|
12 | Image aluskuva = LoadImage("alus"); |
---|
13 | IntMeter pisteLaskuri; |
---|
14 | ExplosionSystem rajahdys = new ExplosionSystem(LoadImage("rajahdys"), 100); |
---|
15 | EasyHighScore topLista = new EasyHighScore(); |
---|
16 | public override void Begin() |
---|
17 | { |
---|
18 | Aloitapeli(); |
---|
19 | |
---|
20 | } |
---|
21 | |
---|
22 | void Aloitapeli() |
---|
23 | { |
---|
24 | ClearAll(); |
---|
25 | Add(rajahdys); |
---|
26 | Level.Size = new Vector(800, 600); |
---|
27 | SetWindowSize(800, 600, false); |
---|
28 | |
---|
29 | LuoKentta(); |
---|
30 | LuoPistelaskuri(); |
---|
31 | MediaPlayer.Play("taustamusa"); |
---|
32 | MediaPlayer.IsRepeating = true; |
---|
33 | |
---|
34 | |
---|
35 | Camera.ZoomToLevel(); |
---|
36 | Keyboard.Listen(Key.Space, ButtonState.Pressed, Ammu, "Ammu", alus); |
---|
37 | |
---|
38 | Keyboard.Listen(Key.Left, ButtonState.Down, LiikutaPelaajaa, null, alus, new Vector(-1000, 0)); |
---|
39 | Keyboard.Listen(Key.Left, ButtonState.Released, LiikutaPelaajaa, null, alus, Vector.Zero); |
---|
40 | |
---|
41 | Keyboard.Listen(Key.Right, ButtonState.Down, LiikutaPelaajaa, null, alus, new Vector(1000, 0)); |
---|
42 | Keyboard.Listen(Key.Right, ButtonState.Released, LiikutaPelaajaa, null, alus, Vector.Zero); |
---|
43 | |
---|
44 | Keyboard.Listen(Key.Up, ButtonState.Down, LiikutaPelaajaa, null, alus, new Vector(0, 1000)); |
---|
45 | Keyboard.Listen(Key.Up, ButtonState.Released, LiikutaPelaajaa, null, alus, Vector.Zero); |
---|
46 | |
---|
47 | Keyboard.Listen(Key.Down, ButtonState.Down, LiikutaPelaajaa, null, alus, new Vector(0, -1000)); |
---|
48 | Keyboard.Listen(Key.Down, ButtonState.Released, LiikutaPelaajaa, null, alus, Vector.Zero); |
---|
49 | |
---|
50 | PhoneBackButton.Listen(ConfirmExit, "Lopeta peli"); |
---|
51 | Keyboard.Listen(Key.Escape, ButtonState.Pressed, ConfirmExit, "Lopeta peli"); |
---|
52 | } |
---|
53 | |
---|
54 | |
---|
55 | void Ammu(PhysicsObject ampuja) |
---|
56 | { |
---|
57 | PhysicsObject ammus = new PhysicsObject(10, 10); |
---|
58 | //ammus.Image = luodinkuva; |
---|
59 | |
---|
60 | ammus.Position = ampuja.Position; |
---|
61 | ammus.IgnoresGravity = true; |
---|
62 | ammus.Angle = ampuja.Angle; |
---|
63 | ammus.Color = Color.Yellow; |
---|
64 | ammus.CanRotate = false; |
---|
65 | ammus.LifetimeLeft = TimeSpan.FromSeconds(2); |
---|
66 | ammus.Tag = "ammus"; |
---|
67 | ammus.CollisionIgnoreGroup = ampuja.CollisionIgnoreGroup; |
---|
68 | ammus.Hit(Vector.FromLengthAndAngle(1000, ampuja.Angle + Angle.FromDegrees(90))); |
---|
69 | Add(ammus); |
---|
70 | AddCollisionHandler(ammus, "hämis", CollisionHandler.AddMeterValue(pisteLaskuri, 200)); |
---|
71 | AddCollisionHandler(ammus, AmmusOsuu); |
---|
72 | |
---|
73 | } |
---|
74 | |
---|
75 | void AmmusOsuu(PhysicsObject ammus, PhysicsObject kohde) |
---|
76 | { |
---|
77 | ammus.Destroy(); |
---|
78 | if(!kohde.Tag.Equals("seinä"))kohde.Destroy(); |
---|
79 | } |
---|
80 | |
---|
81 | |
---|
82 | |
---|
83 | |
---|
84 | void PelaajaOsuu(PhysicsObject alus, PhysicsObject hamis) |
---|
85 | { |
---|
86 | alus.Destroy(); |
---|
87 | rajahdys.AddEffect(alus.X, alus.Y, 100); |
---|
88 | topLista.EnterAndShow(pisteLaskuri.Value); |
---|
89 | topLista.HighScoreWindow.Closed += IkkunaSuljettu; |
---|
90 | |
---|
91 | } |
---|
92 | void IkkunaSuljettu(Window sender) |
---|
93 | { |
---|
94 | Aloitapeli(); |
---|
95 | } |
---|
96 | |
---|
97 | void LiikutaPelaajaa(PhysicsObject alus, Vector nopeus) |
---|
98 | { |
---|
99 | // alareuna |
---|
100 | if ((nopeus.Y < 0) && (alus.Bottom <= Level.Bottom)) |
---|
101 | { |
---|
102 | alus.Bottom = Level.Bottom; |
---|
103 | alus.Velocity = Vector.Zero; |
---|
104 | return; |
---|
105 | } |
---|
106 | // yläreuna |
---|
107 | if ((nopeus.Y > 0) && (alus.Top >= Level.Top)) |
---|
108 | { |
---|
109 | alus.Top = Level.Top; |
---|
110 | alus.Velocity = Vector.Zero; |
---|
111 | return; |
---|
112 | } |
---|
113 | // vasen |
---|
114 | if ((nopeus.X < 0) && (alus.Left <= Level.Left)) |
---|
115 | { |
---|
116 | alus.Left = Level.Left; |
---|
117 | alus.Velocity = Vector.Zero; |
---|
118 | return; |
---|
119 | } |
---|
120 | // oikea |
---|
121 | if ((nopeus.X > 0) && (alus.Right >= Level.Right)) |
---|
122 | { |
---|
123 | alus.Right = Level.Right; |
---|
124 | alus.Velocity = Vector.Zero; |
---|
125 | return; |
---|
126 | } |
---|
127 | |
---|
128 | alus.Velocity = nopeus; |
---|
129 | |
---|
130 | } |
---|
131 | void LuoKentta() |
---|
132 | { |
---|
133 | TileMap ruudut = TileMap.FromLevelAsset("kentta1"); |
---|
134 | ruudut.SetTileMethod('a', Luoalus); |
---|
135 | ruudut.SetTileMethod('h', Luohamis); |
---|
136 | ruudut.Execute(50, 50); |
---|
137 | } |
---|
138 | void Luoalus(Vector paikka, double leveys, double korkeus) |
---|
139 | { |
---|
140 | alus = new PhysicsObject(leveys, korkeus, Shape.Circle); |
---|
141 | Add(alus); |
---|
142 | alus.Position = paikka; |
---|
143 | alus.LinearDamping = 0.9; |
---|
144 | alus.CollisionIgnoreGroup = 2; |
---|
145 | alus.CanRotate = false; |
---|
146 | alus.Tag = "alus"; |
---|
147 | alus.Image = aluskuva; |
---|
148 | AddCollisionHandler(alus, "hämis", PelaajaOsuu); |
---|
149 | } |
---|
150 | void Luohamis(Vector paikka, double leveys, double korkeus) |
---|
151 | { |
---|
152 | PhysicsObject hamis = new PhysicsObject(leveys, korkeus); |
---|
153 | hamis.Position = paikka; |
---|
154 | Add(hamis); |
---|
155 | hamis.CanRotate = false; |
---|
156 | //hamis.AngularDamping = 0.9; |
---|
157 | Image hamiskuva = LoadImage("hamis"); |
---|
158 | hamis.Image = hamiskuva; |
---|
159 | FollowerBrain seuraajanAivot = new FollowerBrain("alus"); |
---|
160 | seuraajanAivot.Speed = 200; |
---|
161 | seuraajanAivot.Active = true; |
---|
162 | hamis.Brain = seuraajanAivot; |
---|
163 | hamis.Tag = "hämis"; |
---|
164 | |
---|
165 | } |
---|
166 | |
---|
167 | void LuoPistelaskuri() |
---|
168 | { |
---|
169 | pisteLaskuri = new IntMeter(0); |
---|
170 | |
---|
171 | Label pisteNaytto = new Label(); |
---|
172 | pisteNaytto.X = Screen.Left + 100; |
---|
173 | pisteNaytto.Y = Screen.Top - 100; |
---|
174 | pisteNaytto.TextColor = Color.Black; |
---|
175 | pisteNaytto.Color = Color.White; |
---|
176 | |
---|
177 | pisteNaytto.BindTo(pisteLaskuri); |
---|
178 | Add(pisteNaytto); |
---|
179 | } |
---|
180 | |
---|
181 | |
---|
182 | |
---|
183 | |
---|
184 | |
---|
185 | |
---|
186 | |
---|
187 | } |
---|