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 D2x_2d : PhysicsGame |
---|
10 | { |
---|
11 | public class Shieldable : PhysicsObject |
---|
12 | { |
---|
13 | protected int shield; |
---|
14 | |
---|
15 | public Shieldable(int w, int h) : base(w, h) |
---|
16 | { |
---|
17 | |
---|
18 | } |
---|
19 | |
---|
20 | public int getShield() |
---|
21 | { |
---|
22 | return shield; |
---|
23 | } |
---|
24 | |
---|
25 | public void setShield(int t) |
---|
26 | { |
---|
27 | if(t > 0) shield = t; |
---|
28 | else Destroy(); |
---|
29 | } |
---|
30 | } |
---|
31 | |
---|
32 | public class Pelaaja : Shieldable |
---|
33 | { |
---|
34 | private int energy = 100; |
---|
35 | |
---|
36 | public Pelaaja(int w, int h) : base(w, h) |
---|
37 | { |
---|
38 | shield = 100; |
---|
39 | Tag = "pelaaja"; |
---|
40 | } |
---|
41 | |
---|
42 | private void Ammu() |
---|
43 | { |
---|
44 | if(energy > 0) |
---|
45 | { |
---|
46 | //Ammus a = new Ammus(1, Angle.); |
---|
47 | energy--; |
---|
48 | //Add(a); |
---|
49 | } |
---|
50 | } |
---|
51 | } |
---|
52 | |
---|
53 | public class Vihollinen : Shieldable |
---|
54 | { |
---|
55 | private int deal; |
---|
56 | |
---|
57 | public Vihollinen(int d, int w, int h) : base(w, h) |
---|
58 | { |
---|
59 | deal = d; |
---|
60 | Tag = "vihollinen"; |
---|
61 | } |
---|
62 | |
---|
63 | public void Ammu(PhysicsObject kohde) |
---|
64 | { |
---|
65 | Vector s = (kohde.Position - Position).Normalize(); |
---|
66 | Ammus a = new Ammus(deal, s); |
---|
67 | Add(a); |
---|
68 | } |
---|
69 | } |
---|
70 | |
---|
71 | public class Ammus : PhysicsObject |
---|
72 | { |
---|
73 | private int deal; |
---|
74 | |
---|
75 | public Ammus(int d, Vector suunta) : base(10, 3) |
---|
76 | { |
---|
77 | deal = d; |
---|
78 | Tag = "ammus"; |
---|
79 | Hit(suunta * 100); |
---|
80 | } |
---|
81 | |
---|
82 | public int getDeal() |
---|
83 | { |
---|
84 | return deal; |
---|
85 | } |
---|
86 | } |
---|
87 | |
---|
88 | protected Pelaaja pelaaja; |
---|
89 | |
---|
90 | private bool playeradded = false; |
---|
91 | |
---|
92 | public override void Begin() |
---|
93 | { |
---|
94 | LuoKuuntelijat(); |
---|
95 | NaytaValikko(); |
---|
96 | |
---|
97 | Camera.ZoomToLevel(); |
---|
98 | } |
---|
99 | |
---|
100 | private void LuoKuuntelijat() |
---|
101 | { |
---|
102 | //Keyboard.Listen(Key.A, ButtonState.Pressed, delegate { pelaaja.Move(new Vector(20, 20)); }, null); |
---|
103 | |
---|
104 | Keyboard.Listen(Key.Escape, ButtonState.Pressed, ConfirmExit, "Exit the game"); |
---|
105 | } |
---|
106 | |
---|
107 | private void NaytaValikko() |
---|
108 | { |
---|
109 | MultiSelectWindow alkuValikko = new MultiSelectWindow("", "Start game", "Exit"); |
---|
110 | alkuValikko.AddItemHandler(0, AloitaPeli); |
---|
111 | alkuValikko.AddItemHandler(1, ConfirmExit); |
---|
112 | alkuValikko.DefaultCancel = 1; |
---|
113 | Add(alkuValikko); |
---|
114 | } |
---|
115 | |
---|
116 | private void AloitaPeli() |
---|
117 | { |
---|
118 | InputWindow kysymysIkkuna = new InputWindow("Enter the level number"); |
---|
119 | kysymysIkkuna.TextEntered += ProcessInput; |
---|
120 | Add(kysymysIkkuna); |
---|
121 | } |
---|
122 | |
---|
123 | private void ProcessInput(InputWindow ikkuna) |
---|
124 | { |
---|
125 | try |
---|
126 | { |
---|
127 | TileMap ruudut = TileMap.FromLevelAsset(ikkuna.InputBox.Text); |
---|
128 | ruudut.SetTileMethod('P', LuoPelaaja); |
---|
129 | ruudut.SetTileMethod('V', LuoVihollinen); |
---|
130 | ruudut.SetTileMethod('|', LuoPystySeina); |
---|
131 | ruudut.SetTileMethod('-', LuoVaakaSeina); |
---|
132 | ruudut.Execute(20, 20); |
---|
133 | } |
---|
134 | catch (Exception) |
---|
135 | { |
---|
136 | MessageDisplay.Add("Level number out of range"); |
---|
137 | } |
---|
138 | } |
---|
139 | |
---|
140 | private void LuoVihollinen(Vector paikka, double leveys, double korkeus) |
---|
141 | { |
---|
142 | Vihollinen vih = new Vihollinen(20, 10, 10); |
---|
143 | vih.Position = paikka; |
---|
144 | vih.Tag = "vihollinen"; |
---|
145 | vih.setShield(100); |
---|
146 | Timer ajastin = new Timer(); |
---|
147 | ajastin.Interval = 1.5; |
---|
148 | ajastin.Timeout += delegate { if(!pelaaja.IsDestroyed) vih.Ammu(pelaaja); }; |
---|
149 | ajastin.Start(); |
---|
150 | Add(vih); |
---|
151 | } |
---|
152 | |
---|
153 | private void LuoPystySeina(Vector paikka, double leveys, double korkeus) |
---|
154 | { |
---|
155 | PhysicsObject seina = PhysicsObject.CreateStaticObject(leveys, korkeus); |
---|
156 | seina.Position = paikka; |
---|
157 | Add(seina); |
---|
158 | } |
---|
159 | |
---|
160 | private void LuoVaakaSeina(Vector paikka, double leveys, double korkeus) |
---|
161 | { |
---|
162 | PhysicsObject seina = PhysicsObject.CreateStaticObject(leveys, korkeus); |
---|
163 | seina.Position = paikka; |
---|
164 | Add(seina); |
---|
165 | } |
---|
166 | |
---|
167 | private void LuoPelaaja(Vector paikka, double leveys, double korkeus) |
---|
168 | { |
---|
169 | if (playeradded) throw new Exception("Too many players"); |
---|
170 | pelaaja = new Pelaaja(10, 10); |
---|
171 | pelaaja.Position = paikka; |
---|
172 | pelaaja.setShield(100); |
---|
173 | AddCollisionHandler<Pelaaja, Ammus>(pelaaja, "ammus", Osuma); |
---|
174 | Add(pelaaja); |
---|
175 | playeradded = true; |
---|
176 | |
---|
177 | } |
---|
178 | |
---|
179 | private void Osuma(Pelaaja pelaaja, Ammus kohde) |
---|
180 | { |
---|
181 | pelaaja.setShield(pelaaja.getShield() - kohde.getDeal()); |
---|
182 | kohde.Destroy(); |
---|
183 | } |
---|
184 | } |
---|