1 | using System; |
---|
2 | using System.Collections.Generic; |
---|
3 | using Jypeli; |
---|
4 | using Jypeli.Widgets; |
---|
5 | using Jypeli.Assets; |
---|
6 | using Jypeli.Effects; |
---|
7 | |
---|
8 | public class Peli : PhysicsGame |
---|
9 | { |
---|
10 | PhysicsObject newestBlock, dropArrow, ground; |
---|
11 | Stack<PhysicsObject> tower; |
---|
12 | enum DropArrowDirection |
---|
13 | { |
---|
14 | Left, |
---|
15 | Right |
---|
16 | } |
---|
17 | DropArrowDirection dAD; |
---|
18 | Image dropArrowImage, perfect; |
---|
19 | Boolean drop, cameraMoving; |
---|
20 | WandererBrain dropArrowBrain; |
---|
21 | double startZoom, towerH; |
---|
22 | Label towerHeight; |
---|
23 | ParticleSystem scoreEffect; |
---|
24 | HighScoreList highestTowers; |
---|
25 | |
---|
26 | protected override void Begin() |
---|
27 | { |
---|
28 | LoadContent(); |
---|
29 | |
---|
30 | StartGame(); |
---|
31 | |
---|
32 | LoadEffect(); |
---|
33 | } |
---|
34 | void LoadContent() |
---|
35 | { |
---|
36 | dropArrowImage = LoadImage("dropArrow"); |
---|
37 | perfect = LoadImage("perfectText"); |
---|
38 | highestTowers = HighScoreList.LoadOrCreate("highestTowers.cmo", 20); |
---|
39 | } |
---|
40 | |
---|
41 | void LoadEffect() |
---|
42 | { |
---|
43 | scoreEffect = new ParticleSystem(perfect, 10, false); |
---|
44 | scoreEffect.MaxAcceleration = 1; |
---|
45 | scoreEffect.MinAcceleration = 1; |
---|
46 | scoreEffect.MaxVelocity = 0; |
---|
47 | scoreEffect.MinVelocity = 0; |
---|
48 | scoreEffect.MinScale = 0.7; |
---|
49 | scoreEffect.MaxScale = 1.0; |
---|
50 | scoreEffect.MaxRotation = 0; |
---|
51 | scoreEffect.MinRotation = 0; |
---|
52 | scoreEffect.MaxRotationSpeed = 0; |
---|
53 | scoreEffect.MinRotationSpeed = 0; |
---|
54 | scoreEffect.MaxLifetime = 2; |
---|
55 | scoreEffect.MinLifetime = 1; |
---|
56 | Add(scoreEffect); |
---|
57 | } |
---|
58 | |
---|
59 | void StartGame() |
---|
60 | { |
---|
61 | ClearAll(); |
---|
62 | towerHeight = new Label(); |
---|
63 | towerHeight.TextColor = Color.Black; |
---|
64 | towerHeight.SizeMode = TextSizeMode.AutoSize; |
---|
65 | towerHeight.DecimalPlaces = 2; |
---|
66 | towerHeight.X = Camera.X + 300; |
---|
67 | Add(towerHeight); |
---|
68 | tower = new Stack<PhysicsObject>(); |
---|
69 | Level.Width = 3000; |
---|
70 | Gravity = new Vector(0, -400); |
---|
71 | CreateDropArrow(); |
---|
72 | Keyboard.Listen(Key.Space, ButtonState.Released, CreateBlock, ""); |
---|
73 | NewGame(); |
---|
74 | } |
---|
75 | |
---|
76 | void NewGame() |
---|
77 | { |
---|
78 | while (tower.Count > 0) |
---|
79 | { |
---|
80 | tower.Pop().Destroy(); |
---|
81 | } |
---|
82 | ground = Level.CreateGround(Level.Bottom, 40, 20, Color.Green, 20); |
---|
83 | //tower.Push(ground); |
---|
84 | drop = true; |
---|
85 | cameraMoving = false; |
---|
86 | startZoom = Camera.Y - ground.Y; |
---|
87 | Camera.Zoom(0.8); |
---|
88 | dropArrow.Position = new Vector(Camera.X, Camera.Y + 380); |
---|
89 | } |
---|
90 | |
---|
91 | void CreateDropArrow() |
---|
92 | { |
---|
93 | dropArrow = new PhysicsObject(48, 48); |
---|
94 | dropArrow.Image = dropArrowImage; |
---|
95 | dropArrow.IgnoresGravity = true; |
---|
96 | Add(dropArrow); |
---|
97 | } |
---|
98 | |
---|
99 | void CreateBlock() |
---|
100 | { |
---|
101 | if (drop) |
---|
102 | { |
---|
103 | drop = false; |
---|
104 | PhysicsObject block = new PhysicsObject(100, 100, Shapes.Rectangle); |
---|
105 | block.Position = new Vector(dropArrow.X, Camera.Y + 200); |
---|
106 | block.KineticFriction = 1.0; |
---|
107 | block.Color = Color.Red; |
---|
108 | block.AngularVelocity = 0; |
---|
109 | block.AngularDamping = 0.3; |
---|
110 | //block.CanRotate = false; |
---|
111 | block.LinearDamping = 1.0; |
---|
112 | block.Restitution = 0.1; |
---|
113 | block.Tag = "block"; |
---|
114 | Add(block); |
---|
115 | newestBlock = block; |
---|
116 | AddCollisionHandler(block, BlockCollision); |
---|
117 | } |
---|
118 | } |
---|
119 | |
---|
120 | void BlockCollision(PhysicsObject collider, PhysicsObject target) |
---|
121 | { |
---|
122 | if (collider == newestBlock) |
---|
123 | { |
---|
124 | drop = true; |
---|
125 | } |
---|
126 | if (collider.Y > Camera.Y) |
---|
127 | { |
---|
128 | //cameraMoving = true; |
---|
129 | //UpdateDropArrowBrain(); |
---|
130 | } |
---|
131 | if (collider == newestBlock && target == ground && tower.Count == 0) |
---|
132 | { |
---|
133 | tower.Push(collider); |
---|
134 | tower.Peek().Tag = "base"; |
---|
135 | } |
---|
136 | if (collider.Tag.ToString() == "block" && target == ground) |
---|
137 | { |
---|
138 | GameOver(); |
---|
139 | } |
---|
140 | if (collider == newestBlock && target == tower.Peek()) |
---|
141 | { |
---|
142 | if (tower.Peek().X - 5 < collider.X && collider.X < tower.Peek().X + 5) |
---|
143 | { |
---|
144 | collider.X = tower.Peek().X; |
---|
145 | //tower.Peek().Angle = collider.Angle; |
---|
146 | tower.Peek().Stop(); |
---|
147 | collider.Stop(); |
---|
148 | scoreEffect.AddEffect(Camera.X, Camera.Y, 1); |
---|
149 | } |
---|
150 | tower.Peek().Color = Color.White; |
---|
151 | tower.Push(collider); |
---|
152 | //hittedBlock = collider; |
---|
153 | //hittedBlock.Color = Color.Green; |
---|
154 | tower.Peek().Color = Color.Green; |
---|
155 | cameraMoving = true; |
---|
156 | } |
---|
157 | } |
---|
158 | |
---|
159 | void GameOver() |
---|
160 | { |
---|
161 | int roundedHeight = (int)Math.Round(towerH, 0); |
---|
162 | highestTowers.Show(roundedHeight); |
---|
163 | KysyUusipeli(); |
---|
164 | } |
---|
165 | |
---|
166 | void KysyUusipeli() |
---|
167 | { |
---|
168 | PushButton aloitaUusipeli = new PushButton(50, 20, "Uusi peli"); |
---|
169 | PushButton lopetaPeli = new PushButton(50, 20, "Lopeta peli"); |
---|
170 | aloitaUusipeli.Clicked +=new Action(NewGame); |
---|
171 | lopetaPeli.Clicked += new Action(QuitGame); |
---|
172 | } |
---|
173 | |
---|
174 | void QuitGame() |
---|
175 | { |
---|
176 | Exit(); |
---|
177 | } |
---|
178 | |
---|
179 | protected override void Update(Time time) |
---|
180 | { |
---|
181 | if (tower.Count > 0) |
---|
182 | { |
---|
183 | if (towerH < Math.Round(tower.Peek().Y - ground.Y, 2)) |
---|
184 | towerH = Math.Round(tower.Peek().Y - ground.Y, 2); |
---|
185 | towerHeight.Text = "Tornin korkeus: " + Math.Round(tower.Peek().Y - ground.Y, 2).ToString() |
---|
186 | + " metriä\nTornin suurin korkeus: " + towerH + " metriä"; |
---|
187 | } |
---|
188 | towerHeight.Y = 360; |
---|
189 | if (tower.Count > 0 && tower.Peek().Y < Camera.Y - 400 && tower.Peek().Tag.ToString() == "block" && Camera.ZoomFactor > (startZoom/(Camera.Y-ground.Y)-0.05)) |
---|
190 | { |
---|
191 | Camera.Zoom(0.9); |
---|
192 | } |
---|
193 | dropArrow.Y = Camera.Y + 380; |
---|
194 | if (cameraMoving) |
---|
195 | { |
---|
196 | Camera.Y += 2; |
---|
197 | if (tower.Peek().Y < Camera.Y) |
---|
198 | { |
---|
199 | cameraMoving = false; |
---|
200 | } |
---|
201 | } |
---|
202 | if (tower.Count > 0 && Camera.Y < tower.Peek().Y) |
---|
203 | { |
---|
204 | cameraMoving = true; |
---|
205 | } |
---|
206 | |
---|
207 | if (dAD == DropArrowDirection.Left) |
---|
208 | { |
---|
209 | dropArrow.X -= 5; |
---|
210 | if (dropArrow.X <= -150) |
---|
211 | dAD = DropArrowDirection.Right; |
---|
212 | } |
---|
213 | else if (dAD == DropArrowDirection.Right) |
---|
214 | { |
---|
215 | dropArrow.X += 5; |
---|
216 | if (dropArrow.X >= 150) |
---|
217 | dAD = DropArrowDirection.Left; |
---|
218 | } |
---|
219 | base.Update(time); |
---|
220 | } |
---|
221 | } |
---|