Exemple #1
0
        protected HeroCharacter AddHeroFor(Player player)
        {
            if (player == null)
            {
                throw new ArgumentNullException("player", "Player should not be null");
            }

            if (player.Hero != null && !player.Hero.Dying)
            {
                player.Hero.RemoveFromParent();
            }

            CGPoint       spawnPos = DefaultSpawnCGPoint;
            HeroCharacter hero     = CreateHeroBy(player.HeroType, spawnPos, player);

            if (hero != null)
            {
                var emitter = (SKEmitterNode)SharedSpawnEmitter.Copy();
                emitter.Position = spawnPos;
                AddNode(emitter, WorldLayer.AboveCharacter);
                GraphicsUtilities.RunOneShotEmitter(emitter, 0.15f);

                hero.FadeIn(2f);
                hero.AddToScene(this);
                Heroes.Add(hero);
            }
            player.Hero = hero;

            return(hero);
        }
        void OnDidBeginContact(object sender, EventArgs e)
        {
            var contact = (SKPhysicsContact)sender;
            // Either bodyA or bodyB in the collision could be a character.
            var node = contact.BodyA.Node as Character;

            if (node != null)
            {
                node.CollidedWith(contact.BodyB);
            }

            // Check bodyB too.
            node = contact.BodyB.Node as Character;
            if (node != null)
            {
                node.CollidedWith(contact.BodyA);
            }

            // Handle collisions with projectiles.
            var isBodyA = (contact.BodyA.CategoryBitMask & (uint)ColliderType.Projectile) != 0;
            var isBodyB = (contact.BodyB.CategoryBitMask & (uint)ColliderType.Projectile) != 0;

            if (isBodyA || isBodyB)
            {
                SKNode projectile = isBodyA ? contact.BodyA.Node : contact.BodyB.Node;
                projectile.RunAction(SKAction.RemoveFromParent());

                // Build up a "one shot" particle to indicate where the projectile hit.
                var emitter = (SKEmitterNode)SharedProjectileSparkEmitter.Copy();
                AddNode(emitter, WorldLayer.AboveCharacter);
                emitter.Position = projectile.Position;
                GraphicsUtilities.RunOneShotEmitter(emitter, 0.15f);
            }
        }
        // Apply damage and return true if death.
        public virtual bool ApplyDamage(nfloat damage)
        {
            Health -= damage;

            if (Health <= 0)
            {
                PerformDeath();
                return(true);
            }

            var emitter = (SKEmitterNode)DamageEmitter.Copy();

            CharacterScene.AddNode(emitter, WorldLayer.AboveCharacter);
            emitter.Position = Position;
            GraphicsUtilities.RunOneShotEmitter(emitter, 0.15f);

            // Show the damage.
            RunAction(DamageAction);

            return(false);
        }
        public override void AnimationDidComplete(AnimationState animation)
        {
            switch (animation)
            {
            case AnimationState.Death:
                var emitter = (SKEmitterNode)DeathEmitter.Copy();
                emitter.ZPosition = -0.8f;
                AddChild(emitter);
                GraphicsUtilities.RunOneShotEmitter(emitter, 4.5f);

                RunAction(SKAction.Sequence(new [] {
                    SKAction.WaitForDuration(4),
                    SKAction.Run(() => CharacterScene.HeroWasKilled(this)),
                    SKAction.RemoveFromParent()
                }));
                break;

            case AnimationState.Attack:
                FireProjectile();
                break;
            }
        }