- Timestamp:
- 2013-07-04 15:09:56 (10 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
2013/27/MikkoI/omapeli/omapeli/omapeli/omapeli.cs
r4400 r4441 7 7 using Jypeli.Widgets; 8 8 9 10 public class Laatikko : PhysicsObject 11 { 12 GameObject topNappi; 13 GameObject bottomNappi; 14 15 public Laatikko(double leveys, double korkeus) 16 : base(leveys, korkeus) 17 { 18 IsUpdated = true; 19 topNappi = new GameObject(3, 3, Shape.Circle); 20 topNappi.Color = Color.Red; 21 Game.Add(topNappi, 1); 22 bottomNappi = new GameObject(3, 3, Shape.Circle); 23 bottomNappi.Color = Color.Green; 24 Game.Add(bottomNappi, 1); 25 } 26 27 public override void Update(Time time) 28 { 29 topNappi.Position = new Vector(this.X, this.Top); 30 bottomNappi.Position = new Vector(this.X, this.Bottom); 31 base.Update(time); 32 } 33 34 } 35 36 37 38 9 39 public class omapeli : PhysicsGame 10 40 { … … 12 42 { 13 43 Mouse.IsCursorVisible = true; 14 CreateBox(0, 800, 200, 200, "destroyer", double.MinValue, double.MaxValue); 15 CreateBox(10, 10, 150, 150, "normal", double.MinValue, double.MaxValue); 44 CreateBox(0, 0, 600, 600, "normal", double.MinValue, double.MaxValue, Angle.FromDegrees(0)); 45 CreateBox(0, 1600, 300, 300, "destroyer", double.MinValue, double.MaxValue, Angle.FromDegrees(45)); 46 47 //.AngularVelocity=0.1; 48 //CreateBox(250, 400, 80, 80, "destroyer", double.MinValue, double.MaxValue).Angle = Angle.FromDegrees(14); 49 //CreateBox(0, 90, 150, 150, "normal", double.MinValue, double.MaxValue); CreateBox(0, 280, 150, 150, "normal", double.MinValue, double.MaxValue); 16 50 AssignLevel(); 17 51 Keyboard.Listen(Key.Escape, ButtonState.Pressed, delegate { Exit(); }, "Lopeta peli"); … … 20 54 void CreateShards(double x, double y, double w, double h, Vector velocity) 21 55 { 56 //poista return 57 return; 22 58 double shardWidth = w; double shardHeight = h; 23 59 if (shardWidth < 4) { shardWidth = 4; } 24 60 if (shardHeight < 4) { shardHeight = 4; } 25 61 26 for (int i = 0; i < ((w * h) / 10); i++)62 for (int i = 0; i < ((w * h) / 20); i++) 27 63 { 28 64 PhysicsObject shard = new PhysicsObject(shardWidth / 2, shardHeight / 2); … … 35 71 double randomY = RandomGen.NextDouble((y - h / 2) + (shardHeight / 2), (y + h / 2) - (shardHeight / 2)); 36 72 AddCollisionHandler(shard, "destroyer", CollisionHandler.DestroyObject); 73 74 shard.CollisionIgnoreGroup = 56; 37 75 shard.Shape = Shape.Triangle; 38 76 shard.Color = Color.Black; … … 47 85 } 48 86 49 PhysicsObject CreateBox(double x, double y, double w, double h, string type, double min, double max )87 PhysicsObject CreateBox(double x, double y, double w, double h, string type, double min, double max, Angle kulma) 50 88 { 51 89 … … 62 100 box.X = x; 63 101 box.Y = y; 102 box.Angle = kulma; 64 103 Mouse.ListenOn(box, MouseButton.Left, ButtonState.Pressed, BreakBox, null, box); 65 104 switch (type) … … 67 106 case "normal": 68 107 box.Color = Color.Black; 69 box.Restitution = 0.4; 70 AddCollisionHandler(box, "destroyer", delegate(PhysicsObject obj, PhysicsObject toin) { if (toin.Velocity.X > 10 || toin.Velocity.Y > 10) { BreakBox(obj); } }); 108 box.Restitution = 0; 109 AddCollisionHandler(box, "destroyer", delegate(PhysicsObject obj, PhysicsObject toin) { if (toin.Velocity.X > 50 || toin.Velocity.Y > 50 || toin.Velocity.X < -50 || toin.Velocity.Y < -50) { BreakBox(obj); } }); 110 71 111 break; 72 112 case "destroyer": 73 113 box.Color = Color.Red; 74 114 box.Tag = "destroyer"; 75 box.Mass = 60;115 box.Mass = 9; 76 116 break; 77 } 117 case "ylaboxi": 118 box.Color = Color.Black; 119 box.Restitution = 0; 120 //box.Color = Color.Red; 121 break; 122 } 123 78 124 Add(box); 79 125 return box; … … 86 132 //Add(explosion); 87 133 //explosion.AddEffect(obj.Position,10); 88 Vector maxShardSize = new Vector(obj.Width / 1.6, obj.Height / 1.6);134 Vector maxShardSize = new Vector(obj.Width, obj.Height); 89 135 ObjSlicer(obj, 90 RandomGen.NextDouble(obj.Left + (obj.Width / 1.6 / 2), obj.Right - (obj.Width / 1.6 / 2)),91 RandomGen.NextDouble(obj.Top - (obj.Width / 1.6 / 2), obj.Bottom + (obj.Width / 1.6 / 2)),92 136 50, maxShardSize, 4); 93 137 obj.Destroy(); 94 138 } 95 void ObjSlicer(PhysicsObject obj, double parentBoxX, double parentBoxY, int maxShards, Vector maxShardSize, double minShardSize) 96 { 139 void ObjSlicer(PhysicsObject obj, int maxShards, Vector maxShardSize, double minShardSize) 140 { 141 double divider = 1.6; 142 maxShardSize.X /= divider; 143 maxShardSize.Y /= divider; 144 145 //Vector parentBoxPos = new Vector(, ); 97 146 double maxSize = 0; 98 147 if (maxShardSize.X > maxShardSize.Y) maxSize = maxShardSize.X; 99 148 else maxSize = maxShardSize.Y; 100 149 101 PhysicsObject parent = CreateBox(parentBoxX, parentBoxY, maxShardSize.X, maxShardSize.Y, "normal", minShardSize, maxSize); // keski(parent) 150 //Vector pos = obj.Position + Vector.FromLengthAndAngle(obj.Height / 2 - maxShardSize.Y / 2, obj.Angle+Angle.FullAngle); 151 152 // Keskimmäisen suorakulmion paikka 153 Vector pos = new Vector(obj.X,obj.Y); 154 //Vector pos = new Vector(RandomGen.NextDouble(obj.Left + (obj.Width / divider / 2), obj.Right - (obj.Width / divider / 2)), RandomGen.NextDouble(obj.Top - (obj.Height / divider / 2), obj.Bottom + (obj.Height / divider / 2))); 155 // pos = pos + (2 * (obj.Position - pos)); 156 // Vector pos = parent.Position + Vector.FromLengthAndAngle(parent.Height / 2 + height / 2, parent.Angle + Angle.RightAngle);// - (obj.Angle)); 157 158 PhysicsObject parent = CreateBox(pos.X, pos.Y, maxShardSize.X, maxShardSize.Y, "normal", minShardSize, maxSize, obj.Angle); // keski(parent) 102 159 if (parent == null) 103 160 { 104 CreateShards(p arentBoxX, parentBoxY, maxShardSize.X, maxShardSize.Y, obj.Velocity);161 CreateShards(pos.X, pos.Y, maxShardSize.X, maxShardSize.Y, obj.Velocity); 105 162 return; 106 163 } 107 164 else 108 165 { 109 parent.Velocity = obj.Velocity; 110 parent.Angle = obj.Angle; 111 } 112 113 double x = parent.Left - ((parent.Left - obj.Left) / 2); 114 double y = parent.Top + (obj.Top - parent.Top) / 2; 115 double width = parent.Left - obj.Left; 166 parent.Velocity = obj.Velocity; 167 parent.AngularVelocity = obj.AngularVelocity; 168 } 169 170 171 172 //double x = pos.X; 173 //double y = parent.Top + (obj.Top - parent.Top) / 2; 174 double width = parent.Width; 116 175 double height = obj.Top - parent.Top; 117 176 118 177 PhysicsObject box = new PhysicsObject(1, 1); 119 178 120 box = CreateBox(x, y, width, height, "normal", minShardSize, maxSize); 179 // parent on keskimmäinen suorakulmio 180 pos = parent.Position + Vector.FromLengthAndAngle(parent.Height / 2 + height / 2, parent.Angle + Angle.RightAngle);// - (obj.Angle)); 181 PhysicsObject Topbox = CreateBox(pos.X, pos.Y, width, height, "normal", minShardSize, maxSize, parent.Angle); //ylä kesk 182 183 if (Topbox == null) 184 { 185 CreateShards(pos.X, pos.Y, width, height, obj.Velocity); 186 } 187 else 188 { 189 Topbox.Velocity = obj.Velocity; 190 //x = parent.Left - ((parent.Left - obj.Left) / 2); 191 width = parent.Left - obj.Left; 192 pos = Topbox.Position - Vector.FromLengthAndAngle(Topbox.Width / 2 + width / 2, Topbox.Angle);// - (obj.Angle)); 193 box = CreateBox(pos.X, pos.Y, width, height, "normal", minShardSize, maxSize, Topbox.Angle); //ylä vas 194 if (box == null) 195 { 196 CreateShards(pos.X, pos.Y, width, height, obj.Velocity); 197 } 198 else 199 { 200 201 box.Velocity = parent.Velocity; 202 box.AngularVelocity = parent.AngularVelocity; 203 204 } 205 206 207 width = obj.Right - parent.Right; 208 209 pos = Topbox.Position + Vector.FromLengthAndAngle(Topbox.Width / 2 + width / 2, Topbox.Angle);// - (obj.Angle)); 210 box = CreateBox(pos.X, pos.Y, width, height, "normal", minShardSize, maxSize, Topbox.Angle); //ylä oik 211 212 if (box == null) 213 { 214 CreateShards(pos.X, pos.Y, width, height, obj.Velocity); 215 } 216 else { box.Velocity = obj.Velocity; box.Angle = obj.Angle; } 217 } 218 219 220 221 //x = parent.Left - ((parent.Left - obj.Left) / 2); 222 //y = parent.Y; 223 width = obj.Right - parent.Right; 224 height = parent.Height; 225 226 pos = parent.Position + Vector.FromLengthAndAngle(parent.Width / 2 + width / 2, parent.Angle);// - (obj.Angle)); 227 box = CreateBox(pos.X, pos.Y, width, height, "normal", minShardSize, maxSize, parent.Angle); //keski oik 121 228 122 229 if (box == null) 123 230 { 124 CreateShards(x, y, width, height, obj.Velocity); 125 } 126 else { box.Velocity = obj.Velocity; box.Angle = obj.Angle; } 127 128 x = parentBoxX; 231 CreateShards(pos.X, pos.Y, width, height, obj.Velocity); 232 } 233 else { box.Velocity = obj.Velocity; } 234 235 width = parent.Left - obj.Left; 236 237 pos = parent.Position - Vector.FromLengthAndAngle(parent.Width / 2 + width / 2, parent.Angle);// - (obj.Angle)); 238 box = CreateBox(pos.X, pos.Y, width, height, "normal", minShardSize, maxSize, parent.Angle); //keski vas 239 240 241 242 //x = parent.Right + ((obj.Right - parent.Right) / 2); 129 243 width = parent.Width; 130 131 box = CreateBox(x, y, width, height, "normal", minShardSize, maxSize);132 133 if (box == null)134 {135 CreateShards(x, y, width, height, obj.Velocity);136 }137 else { box.Velocity = obj.Velocity; box.Angle = obj.Angle; }138 x = parent.Right + ((obj.Right - parent.Right) / 2);139 width = obj.Right - parent.Right;140 141 box = CreateBox(x, y, width, height, "normal", minShardSize, maxSize);142 143 if (box == null)144 {145 CreateShards(x, y, width, height, obj.Velocity);146 }147 else { box.Velocity = obj.Velocity; box.Angle = obj.Angle; }148 149 x = parent.Left - ((parent.Left - obj.Left) / 2);150 y = parent.Y;151 width = parent.Left - obj.Left;152 height = parent.Height;153 154 box = CreateBox(x, y, width, height, "normal", minShardSize, maxSize);155 156 if (box == null)157 {158 CreateShards(x, y, width, height, obj.Velocity);159 }160 else { box.Velocity = obj.Velocity; box.Angle = obj.Angle; }161 x = parent.Right + ((obj.Right - parent.Right) / 2);162 width = obj.Right - parent.Right;163 164 box = CreateBox(x, y, width, height, "normal", minShardSize, maxSize);165 166 if (box == null)167 {168 CreateShards(x, y, width, height, obj.Velocity);169 }170 else { box.Velocity = obj.Velocity; box.Angle = obj.Angle; }171 x = parent.Left - ((parent.Left - obj.Left) / 2);172 244 height = (parent.Bottom - obj.Bottom); 173 y = parent.Bottom - height / 2; 174 width = parent.Left - obj.Left; 175 176 box = CreateBox(x, y, width, height, "normal", minShardSize, maxSize); 177 178 if (box == null) 179 { 180 CreateShards(x, y, width, height, obj.Velocity); 181 } 182 else { box.Velocity = obj.Velocity; box.Angle = obj.Angle; } 183 x = parentBoxX; 184 width = parent.Width; 185 186 box = CreateBox(x, y, width, height, "normal", minShardSize, maxSize); 187 188 if (box == null) 189 { 190 CreateShards(x, y, width, height, obj.Velocity); 191 } 192 else { box.Velocity = obj.Velocity; box.Angle = obj.Angle; } 193 x = parent.Right + ((obj.Right - parent.Right) / 2); 194 width = obj.Right - parent.Right; 195 196 box = CreateBox(x, y, width, height, "normal", minShardSize, maxSize); 197 198 if (box == null) 199 { 200 CreateShards(x, y, width, height, obj.Velocity); 201 } 202 else { box.Velocity = obj.Velocity; box.Angle = obj.Angle; } 203 204 //if (parent.Left - obj.Left > minShardSize){ObjSlicer(parent, parent.X, parent.Y, maxShards, (parent.Left - obj.Left), minShardSize);} 245 246 pos = parent.Position + Vector.FromLengthAndAngle(parent.Height / 2 + height / 2, parent.Angle - Angle.RightAngle);// - (obj.Angle)); 247 PhysicsObject BotBox = CreateBox(pos.X, pos.Y, width, height, "normal", minShardSize, maxSize, parent.Angle); //ala kesk 248 249 if (BotBox == null) 250 { 251 CreateShards(pos.X, pos.Y, width, height, obj.Velocity); 252 } 253 else 254 { 255 256 BotBox.Velocity = obj.Velocity; 257 258 width = parent.Left - obj.Left; 259 260 pos = BotBox.Position - Vector.FromLengthAndAngle(BotBox.Width / 2 + width / 2, BotBox.Angle);// - (obj.Angle)); 261 box = CreateBox(pos.X, pos.Y, width, height, "normal", minShardSize, maxSize, BotBox.Angle); //ala vas 262 263 if (box == null) 264 { 265 CreateShards(pos.X, pos.Y, width, height, obj.Velocity); 266 } 267 else { box.Velocity = obj.Velocity; } 268 269 width = obj.Right - parent.Right; 270 271 pos = BotBox.Position + Vector.FromLengthAndAngle(BotBox.Width / 2 + width / 2, BotBox.Angle);// - (obj.Angle)); 272 box = CreateBox(pos.X, pos.Y, width, height, "normal", minShardSize, maxSize, BotBox.Angle); //ala oik 273 274 if (box == null) 275 { 276 CreateShards(pos.X, pos.Y, width, height, obj.Velocity); 277 } 278 else { box.Velocity = obj.Velocity; } 279 280 } 281 282 205 283 206 284 }
Note: See TracChangeset
for help on using the changeset viewer.