1 | using System; |
---|
2 | using System.Collections.Generic; |
---|
3 | using System.Linq; |
---|
4 | using Jypeli; |
---|
5 | using Jypeli.Assets; |
---|
6 | using Jypeli.Controls; |
---|
7 | using Jypeli.Effects; |
---|
8 | using Jypeli.Widgets; |
---|
9 | |
---|
10 | public class Punasininen : PhysicsGame |
---|
11 | { |
---|
12 | private const double SPEED = 450; |
---|
13 | private const double JUMPSPEED = 1250; |
---|
14 | private const int TILE_SIZE = 40; |
---|
15 | |
---|
16 | private Player blue; |
---|
17 | private Player red; |
---|
18 | private Image[] orangeTileImages = LoadImages("bluetile\\bluetile", 12); |
---|
19 | private Image[] blueTileImages; |
---|
20 | private Image orangeGlow = LoadImage("orangeglow"); |
---|
21 | private Image blueGlow = LoadImage("blueglow"); |
---|
22 | |
---|
23 | private Image bluepic; |
---|
24 | private Image redpic; |
---|
25 | |
---|
26 | private Image pistolpic; |
---|
27 | |
---|
28 | DoubleMeter percentageTracker; |
---|
29 | |
---|
30 | private Shader shader; |
---|
31 | |
---|
32 | public override void Begin() |
---|
33 | { |
---|
34 | |
---|
35 | IsMouseVisible = true; // <- voi ottaa pois |
---|
36 | shader = new Shader(GraphicsDevice, Content, Camera); |
---|
37 | |
---|
38 | //Gravity = new Vector(0, -1000); |
---|
39 | CreateLevel(); |
---|
40 | AddControls(); |
---|
41 | } |
---|
42 | |
---|
43 | private static Image[] LoadImages(string prefix, int numberOfImages) |
---|
44 | { |
---|
45 | var images = new Image[numberOfImages]; |
---|
46 | for (int i = 1; i <= numberOfImages; i++) |
---|
47 | { |
---|
48 | //images[i-1] = LoadImage(string.Format("{0}{1:0000}", prefix, numberOfImages)); |
---|
49 | images[i-1] = LoadImage($"{prefix}{numberOfImages.ToString().PadLeft(4, '0')}"); |
---|
50 | } |
---|
51 | return images; |
---|
52 | } |
---|
53 | |
---|
54 | void CreateLevel() |
---|
55 | { |
---|
56 | Gravity = new Vector(0, -1000); |
---|
57 | |
---|
58 | ColorTileMap map = ColorTileMap.FromLevelAsset("dungeon1"); |
---|
59 | map.SetTileMethod(Color.Black, AddPlatform); |
---|
60 | map.SetTileMethod(Color.FromHexCode("#FF0026FF"), delegate(Vector paikka, double leveys, double korkeus) { blue = CreatePlayer(paikka, leveys, korkeus, bluepic, Color.Blue);}); |
---|
61 | map.SetTileMethod(Color.FromHexCode("FFD800"), CreateWeaponCrate); |
---|
62 | map.SetTileMethod(Color.Red, delegate(Vector paikka, double leveys, double korkeus) { red = CreatePlayer(paikka, leveys, korkeus, redpic, Color.Red); }); |
---|
63 | map.Execute(TILE_SIZE, TILE_SIZE); |
---|
64 | |
---|
65 | Camera.ZoomToAllObjects(); |
---|
66 | |
---|
67 | Level.Background.Color = Color.Black; |
---|
68 | |
---|
69 | percentageTracker = new DoubleMeter(0, 0, 100); |
---|
70 | |
---|
71 | ProgressBar percentageBar = new ProgressBar(Level.Width / 2, 20) { Y = Screen.Top - 20, BarColor = red.Color, Color = blue.Color }; |
---|
72 | percentageBar.Y = Screen.Top - 20; |
---|
73 | percentageBar.BindTo(percentageTracker); |
---|
74 | Add(percentageBar); |
---|
75 | } |
---|
76 | |
---|
77 | void CreateWeaponCrate(Vector place, double width, double height) |
---|
78 | { |
---|
79 | WeaponCrate crate = new WeaponCrate(width, height); |
---|
80 | crate.Position = place; |
---|
81 | crate.Color = Color.DarkGray; |
---|
82 | Add(crate); |
---|
83 | } |
---|
84 | |
---|
85 | void AddPlatform(Vector paikka, double leveys, double korkeus) |
---|
86 | { |
---|
87 | PhysicsObject platform = PhysicsObject.CreateStaticObject(leveys, korkeus); |
---|
88 | platform.Position = paikka; |
---|
89 | platform.Color = Color.Black; |
---|
90 | platform.Tag = "platform"; |
---|
91 | Add(platform); |
---|
92 | } |
---|
93 | |
---|
94 | Player CreatePlayer(Vector paikka, double leveys, double korkeus, Image playerspic, Color playersColor) |
---|
95 | { |
---|
96 | Player player = new Player(leveys, korkeus, playerspic, playersColor); |
---|
97 | player.Position = paikka; |
---|
98 | Add(player); |
---|
99 | |
---|
100 | player.Weapon = new AssaultRifle(leveys / 2, korkeus / 2) { FireRate = 1.5, AttackSound = null, MaxAmmoLifetime = TimeSpan.FromSeconds(8), ProjectileCollision = BulletHitsSomething}; |
---|
101 | |
---|
102 | AddCollisionHandler(player, "platform", delegate(PhysicsObject a, PhysicsObject b) |
---|
103 | { |
---|
104 | ColorTile(a, b); |
---|
105 | }); |
---|
106 | AddCollisionHandler(player, "crate", delegate(PhysicsObject a, PhysicsObject b) |
---|
107 | { |
---|
108 | //((Player)a).Weapon = ((WeaponCrate)b).GiveWeapon(); |
---|
109 | player.Weapon = GunLottery(); |
---|
110 | b.Destroy(); |
---|
111 | }); |
---|
112 | |
---|
113 | return player; |
---|
114 | } |
---|
115 | |
---|
116 | void BulletHitsSomething(PhysicsObject bullet, PhysicsObject target) |
---|
117 | { |
---|
118 | if (target.Tag == "platform") |
---|
119 | { |
---|
120 | // TODO bullet must know its owner |
---|
121 | if (bullet.Color == Color.Blue) |
---|
122 | { |
---|
123 | ColorTile(blue, target); |
---|
124 | } |
---|
125 | else if (bullet.Color == Color.Red) |
---|
126 | { |
---|
127 | ColorTile(red, target); |
---|
128 | } |
---|
129 | } |
---|
130 | bullet.Destroy(); |
---|
131 | } |
---|
132 | |
---|
133 | Weapon GunLottery() |
---|
134 | { |
---|
135 | return new AssaultRifle(TILE_SIZE / 2, TILE_SIZE / 2) { FireRate = 1.5, AttackSound = null, MaxAmmoLifetime = TimeSpan.FromSeconds(8), ProjectileCollision = BulletHitsSomething }; |
---|
136 | } |
---|
137 | |
---|
138 | void Win(Player player) |
---|
139 | { |
---|
140 | Pause(); |
---|
141 | //Haluaisin tehdä tähän jonkinlaisen hauskan ponnahdusefektin jossain vaiheessa. Sellaisen napakan zoomin ja sitten boing. Hurdur. |
---|
142 | Camera.ZoomTo(player.Left, player.Bottom, player.Right, player.Top + 100); |
---|
143 | |
---|
144 | Label announcement = new Label("Kiva homma hei") |
---|
145 | { |
---|
146 | TextColor = player.Color, |
---|
147 | Position = Camera.WorldToScreen(player.Position + new Vector(0, 40)), |
---|
148 | TextScale = new Vector(2, 2) |
---|
149 | }; |
---|
150 | Add(announcement); |
---|
151 | |
---|
152 | } |
---|
153 | |
---|
154 | void ColorTile(PhysicsObject player, PhysicsObject platform) |
---|
155 | { |
---|
156 | GameObject glow = platform.Objects.FirstOrDefault(); |
---|
157 | if (glow == null) |
---|
158 | { |
---|
159 | glow = new GameObject(TILE_SIZE * 7, TILE_SIZE * 7); |
---|
160 | platform.Add(glow); |
---|
161 | } |
---|
162 | glow.Image = player.Color == Color.Blue? blueGlow : orangeGlow; |
---|
163 | platform.Color = player.Color; |
---|
164 | |
---|
165 | List<GameObject> colored = GetObjects(o => (o.Color == red.Color || o.Color == blue.Color) && (String)o.Tag == "platform"); |
---|
166 | percentageTracker.Value = (double)colored.FindAll(o => o.Color == red.Color).Count/colored.Count * 100; |
---|
167 | } |
---|
168 | |
---|
169 | void AddControls() |
---|
170 | { |
---|
171 | ControllerOne.Listen(Button.Back, ButtonState.Pressed, Exit, "Poistu pelistä"); |
---|
172 | |
---|
173 | ControllerOne.ListenAnalog(AnalogControl.LeftStick, 0.1, Move, "Liikuta pelaajaa", red); |
---|
174 | ControllerOne.Listen(Button.A, ButtonState.Pressed, Jump, "Pelaaja hyppää", red, JUMPSPEED); |
---|
175 | ControllerOne.ListenAnalog(AnalogControl.RightStick, 0.1, Aim, "Tähtää", red); |
---|
176 | ControllerOne.ListenAnalog(AnalogControl.RightTrigger, 0.1, Shoot, "", red); |
---|
177 | ControllerOne.ListenAnalog(AnalogControl.LeftTrigger, 0.1, Shoot, "", red); |
---|
178 | |
---|
179 | ControllerTwo.ListenAnalog(AnalogControl.LeftStick, 0.1, Move, "Liikuta pelaajaa", blue); |
---|
180 | ControllerTwo.Listen(Button.A, ButtonState.Pressed, Jump, "Pelaaja hyppää", blue, JUMPSPEED); |
---|
181 | ControllerOne.ListenAnalog(AnalogControl.RightStick, 0.1, Aim, null, blue); |
---|
182 | ControllerTwo.ListenAnalog(AnalogControl.RightTrigger, 0.1, Shoot, "", blue); |
---|
183 | ControllerTwo.ListenAnalog(AnalogControl.LeftTrigger, 0.1, Shoot, "", blue); |
---|
184 | |
---|
185 | Keyboard.Listen(Key.F1, ButtonState.Pressed, ShowControlHelp, "Ohjeet"); |
---|
186 | Keyboard.Listen(Key.Escape, ButtonState.Pressed, Exit, "Lopeta"); |
---|
187 | |
---|
188 | Mouse.Listen(MouseButton.Left, ButtonState.Pressed, () => shader.NewWave(Mouse.PositionOnWorld), null); |
---|
189 | } |
---|
190 | |
---|
191 | void Move(AnalogState stick, Player player) |
---|
192 | { |
---|
193 | if (stick.StateVector.Magnitude > 0.15) |
---|
194 | player.Walk(stick.StateVector.X > 0 ? Direction.Right : Direction.Left); |
---|
195 | } |
---|
196 | |
---|
197 | void Shoot(AnalogState trigger, Player player) |
---|
198 | { |
---|
199 | if (player.Weapon != null) |
---|
200 | { |
---|
201 | PhysicsObject bullet = player.Weapon.Shoot(); |
---|
202 | if (bullet != null) |
---|
203 | { |
---|
204 | bullet.Color = player.Color; |
---|
205 | } |
---|
206 | } |
---|
207 | } |
---|
208 | |
---|
209 | void Jump(Player player, double speed) |
---|
210 | { |
---|
211 | player.Jump(speed); |
---|
212 | } |
---|
213 | |
---|
214 | void Aim(AnalogState stickState, Player player) |
---|
215 | { |
---|
216 | |
---|
217 | } |
---|
218 | |
---|
219 | protected override void Draw(Microsoft.Xna.Framework.GameTime gameTime) |
---|
220 | { |
---|
221 | //shader.Draw(gameTime, base.Draw); |
---|
222 | |
---|
223 | shader.Draw(gameTime); |
---|
224 | base.Draw(gameTime); |
---|
225 | shader.DrawEnd(gameTime); |
---|
226 | } |
---|
227 | } |
---|