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 | using AdvanceMath; |
---|
9 | |
---|
10 | namespace Pallospeli |
---|
11 | { |
---|
12 | public class Peli : PhysicsGame |
---|
13 | { |
---|
14 | const int LEVELWIDTH = 500; |
---|
15 | const int WIDTH = 10; |
---|
16 | const int DIAMETER = LEVELWIDTH / WIDTH; |
---|
17 | const int HEIGHT = 7; |
---|
18 | |
---|
19 | GameObject launchpad; |
---|
20 | Vector start; |
---|
21 | |
---|
22 | Playfield playfield; |
---|
23 | |
---|
24 | public override void Begin() |
---|
25 | { |
---|
26 | SetLevel(); |
---|
27 | SetControls(); |
---|
28 | } |
---|
29 | |
---|
30 | /// <summary> |
---|
31 | /// Level setup |
---|
32 | /// </summary> |
---|
33 | public void SetLevel() |
---|
34 | { |
---|
35 | this.Level.Width = LEVELWIDTH; |
---|
36 | this.Level.Height = 800; |
---|
37 | this.Level.CreateBorders(true); |
---|
38 | |
---|
39 | playfield = new Playfield(WIDTH, HEIGHT); |
---|
40 | playfield.FillColorset(3); |
---|
41 | playfield.Balls = null; |
---|
42 | playfield.RepairBallLocations(); |
---|
43 | |
---|
44 | launchpad = new GameObject(DIAMETER, DIAMETER, Shape.Circle); |
---|
45 | launchpad.Move(new Vector(this.Level.Center.X, this.Level.Bottom + (DIAMETER / 2))); |
---|
46 | launchpad.Color = playfield.Colorset[RandomGen.NextInt(playfield.Colorset.Length)]; |
---|
47 | this.Add(launchpad); |
---|
48 | MessageDisplay.TextColor = Color.White; |
---|
49 | |
---|
50 | CreateBalls(); |
---|
51 | } |
---|
52 | |
---|
53 | /// <summary> |
---|
54 | /// Creates balls from playfield.Balls. |
---|
55 | /// |
---|
56 | /// Adds PhysicsObject to Balls. |
---|
57 | /// </summary> |
---|
58 | public void CreateBalls() |
---|
59 | { |
---|
60 | for (int i = 0; i < playfield.Balls.Length; i++) |
---|
61 | { |
---|
62 | for (int j = 0; j < playfield.Balls[i].Length; j++) |
---|
63 | { |
---|
64 | if (playfield.Balls[i][j] != null) |
---|
65 | { |
---|
66 | //Luo PhysicsObjectin vastaamaan palloa |
---|
67 | PhysicsObject ball = new PhysicsObject(DIAMETER, DIAMETER, Shape.Circle, CollisionShapeQuality.Best); |
---|
68 | ball.IgnoresCollisionResponse = true; |
---|
69 | ball.Position = new Vector((j * DIAMETER - Level.Width / 2 + (DIAMETER / 2)) + (i % 2) * (DIAMETER / 2), -(i * DIAMETER) + (Level.Height / 2) - (DIAMETER / 2)); |
---|
70 | ball.Color = playfield.Balls[i][j].Color; |
---|
71 | ball.Tag = playfield.Balls[i][j].Location; |
---|
72 | Add(ball); |
---|
73 | playfield.Balls[i][j].Presentation = ball; |
---|
74 | } |
---|
75 | } |
---|
76 | } |
---|
77 | } |
---|
78 | |
---|
79 | /// <summary> |
---|
80 | /// Binds controls |
---|
81 | /// </summary> |
---|
82 | public void SetControls() |
---|
83 | { |
---|
84 | Mouse.Listen(MouseButton.Left, ButtonState.Pressed, StartAim, "Ampuu"); |
---|
85 | Mouse.Listen(MouseButton.Left, ButtonState.Released, Shoot, null); |
---|
86 | |
---|
87 | Keyboard.Listen(Key.F1, ButtonState.Pressed, ShowControlHelp, "Näytä ohjeet"); |
---|
88 | Keyboard.Listen(Key.Escape, ButtonState.Pressed, Exit, "Poistu"); |
---|
89 | |
---|
90 | IsMouseVisible = true; |
---|
91 | } |
---|
92 | |
---|
93 | private void StartAim() |
---|
94 | { |
---|
95 | start = Mouse.PositionOnWorld; |
---|
96 | } |
---|
97 | |
---|
98 | /// <summary> |
---|
99 | /// Changes color of launchpad |
---|
100 | /// </summary> |
---|
101 | public void NextBall() |
---|
102 | { |
---|
103 | launchpad.Color = playfield.Colorset[RandomGen.NextInt(playfield.Colorset.Length)]; |
---|
104 | } |
---|
105 | |
---|
106 | /// <summary> |
---|
107 | /// Shoots the ball |
---|
108 | /// |
---|
109 | /// Color of the bullet/ball is the same as color of the launchpad |
---|
110 | /// |
---|
111 | /// Also calls NextBall |
---|
112 | /// </summary> |
---|
113 | public void Shoot() |
---|
114 | { |
---|
115 | PhysicsObject ammus = new PhysicsObject(DIAMETER, DIAMETER, Shape.Circle); |
---|
116 | ammus.Position = launchpad.Position + new Vector(0, 40); |
---|
117 | ammus.Color = launchpad.Color; |
---|
118 | NextBall(); |
---|
119 | this.Add(ammus); |
---|
120 | this.AddCollisionHandler(ammus, Collision); |
---|
121 | ammus.Hit(Mouse.PositionOnWorld - start); |
---|
122 | } |
---|
123 | |
---|
124 | private void Collision(PhysicsObject obj, PhysicsObject obj2) |
---|
125 | { |
---|
126 | obj.Destroy(); |
---|
127 | if (obj2.Tag is Location) |
---|
128 | { |
---|
129 | if (obj.Color == obj2.Color) |
---|
130 | { |
---|
131 | playfield.Destroy((Location)obj2.Tag); |
---|
132 | obj.Destroy(); |
---|
133 | playfield.StabilityCheck(); |
---|
134 | |
---|
135 | } |
---|
136 | } |
---|
137 | else { /*obj.Destroy();*/ } |
---|
138 | } |
---|
139 | } |
---|
140 | |
---|
141 | } |
---|