Example #1
0
        public Shell(Vector3 position, Vector3 cannonFacing, Vector3 cannonVelocity, float startSpeed, object shellOwner)
        {
            int segments = 8;
            List<Vector3> vertices = new List<Vector3>(segments + 1);

            //float unitradius = (float)Math.Sqrt(8);
            float angleStep = (float)(2 * Math.PI / segments);
            for (int i = 0; i < segments; i++)
            {
                vertices.Add(new Vector3((float)Math.Cos(angleStep * i) * radius, (float)Math.Sin(angleStep * i) * radius, 0));
            }
            vertices.Add(new Vector3(radius, 0, 0));

            graphics = GraphicsAspect.Create(this, vertices, position, 1, Color.White, Color.Red);

            // вычисляем вектор полёта снаряда - сумма импульса выстрела и собственной скорости оружия
            cannonFacing.NormalizeFast();
            Vector3 shootVelocity = cannonFacing * startSpeed + cannonVelocity;
            Vector2 shootDirection = shootVelocity.Xy;
            shootDirection.NormalizeFast();
            //physics = new PhysicsAspect(this, position, shootVelocity.Xy, startSpeed);

            physics = PhysicsAspect.Create(this, position, shootDirection, shootVelocity.LengthFast);
            bounds = BoundSetAspect.Create(this, null);
            BoundsAspect bound = CircleBoundsAspect.Create(bounds, position.Xy, radius);
            bounds.AddBound(bound);
            // снаряд будет быстродвижущимся объектом, для него особый алгоритм определения столкновений
            bounds.SetAttribute(Strings.CollisionDetectionSpeedType, Strings.CollisionDetectionSpeedTypeFast);
            damage = DamageAspect.Create(this, 1);

            //physics = new PhysicsAspect(this, position, Vector2.Zero, 0);
            //timer = DestroyByTimerAspect.Create(this, new TimeSpan(0, 0, 0, 2, 500));
            timer = DestroyByTimerAspect.Create(this, new TimeSpan(0, 0, 0, 1, 0));

            this.shellOwner = shellOwner;
            this.name = "shell";

            MessageDispatcher.RegisterHandler(typeof(SetPosition), bounds);
            MessageDispatcher.RegisterHandler(typeof(SetPosition), graphics);
            MessageDispatcher.RegisterHandler(typeof(DestroyChildrenOf), this);
            MessageDispatcher.RegisterHandler(typeof(Kill), this);

            messageHandler.Handlers.Add(typeof(Kill), HandleKill);
        }
Example #2
0
        private bool HandleKill(object message)
        {
            Kill kill = (Kill)message;
            if (kill.Target == this)
            {
                // убираем все аспекты, кроме графического, чтобы показать анимацию уничтожения
                MessageDispatcher.Post(new DestroySelf(thrusterController));
                MessageDispatcher.Post(new DestroySelf(physics));
                MessageDispatcher.Post(new DestroySelf(bounds));
                MessageDispatcher.Post(new DestroySelf(damage));
                MessageDispatcher.Post(new DestroySelf(leftCannon));
                MessageDispatcher.Post(new DestroySelf(rightCannon));
                MessageDispatcher.Post(new DestroySelf(rearCannon));

                timer = DestroyByTimerAspect.Create(this, new TimeSpan(0, 0, 0, 2, 0));
            }

            return true;
        }
 public static DestroyByTimerAspect Create(object owner, TimeSpan timeToLive)
 {
     DestroyByTimerAspect aspect = new DestroyByTimerAspect(owner, timeToLive);
     aspect.RegisterAllStuff();
     return aspect;
 }