source: 2014/27/AleksanteriV/Protokolla236/Protokolla236/Protokolla236/MikonPhysicsObject.cs @ 5212

Revision 5212, 2.9 KB checked in by mijoilmo, 9 years ago (diff)
Line 
1using System;
2using System.Collections.Generic;
3using Jypeli;
4using Jypeli.Assets;
5using Jypeli.Controls;
6using Jypeli.Effects;
7using Jypeli.Widgets;
8
9public class MikonPhysicsObject : PhysicsObject
10{
11    private bool broken = false;
12    private Protokolla236 game;
13    public void Break(int method, int pieceCountX = 2, int pieceCountY = 2)
14    {
15        if (this.broken) { return; }
16        this.Destroy();
17        this.broken = true;
18        switch (method)
19        {
20            case 0://uniform break
21                var pieceWidth = this.Width / pieceCountX;
22                var pieceHeight = this.Height / pieceCountY;
23                if (pieceWidth < 3 || pieceHeight < 3) { return; }
24                var useCollisions = true;
25                if (pieceWidth < 10 || pieceHeight < 10) { useCollisions = false; }
26                //loop x and y through
27                //calc left corner
28                var startCorner = Vector.FromLengthAndAngle(this.Width / 2 - pieceWidth / 2, this.AbsoluteAngle + Angle.StraightAngle) + Vector.FromLengthAndAngle(this.Height / 2 - pieceHeight / 2, this.AbsoluteAngle - Angle.RightAngle); //vasen alakulma
29
30                for (int y = 0; y < pieceCountY; y++)
31                {
32                    for (int x = 0; x < pieceCountX; x++)
33                    {
34                        MikonPhysicsObject childBox = new MikonPhysicsObject(this.game, pieceWidth, pieceHeight);
35                        if (!useCollisions)
36                        {
37                            childBox.CollisionIgnoreGroup = 2;
38                        }
39                        childBox.Color = this.Color;
40                        childBox.Tag = this.Tag;
41                        childBox.AbsoluteAngle = this.AbsoluteAngle;
42                        childBox.AngularVelocity = this.AngularVelocity;
43
44                        childBox.Position = this.Position + startCorner
45                            + Vector.FromLengthAndAngle(x * pieceWidth, this.AbsoluteAngle) //plus x                                                               
46                            + Vector.FromLengthAndAngle(y * pieceHeight, this.AbsoluteAngle + Angle.RightAngle); //plus y                                         
47
48                        childBox.Velocity = this.Velocity;
49
50                        //childBox.Position += offset;
51
52                        if (RandomGen.NextDouble(0, 1) < 0.2) {
53                            childBox.Break(method,pieceCountX,pieceCountY);
54                        }
55
56                        Game.Add(childBox);
57                    }
58                }
59                break;
60        }
61    }
62    public MikonPhysicsObject(Protokolla236 game, double w, double h)
63        : base(w, h)
64    {
65        // TODO: Complete member initialization
66        //game.AddCollisionHandler(game.level, this, delegate { this.Break(0, 2, 2); });
67        this.game = game;
68    }
69}
Note: See TracBrowser for help on using the repository browser.