Example #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);
        }
        protected override void LoadSceneAssets()
        {
            SKTextureAtlas atlas = SKTextureAtlas.FromName("Environment");

            // Load archived emitters and create copyable sprites.
            sharedProjectileSparkEmitter = GraphicsUtilities.EmitterNodeWithEmitterNamed("ProjectileSplat");
            sharedSpawnEmitter           = GraphicsUtilities.EmitterNodeWithEmitterNamed("Spawn");

            sharedSmallTree = new Tree(new SKNode[] {
                SKSpriteNode.FromTexture(atlas.TextureNamed("small_tree_base.png")),
                SKSpriteNode.FromTexture(atlas.TextureNamed("small_tree_middle.png")),
                SKSpriteNode.FromTexture(atlas.TextureNamed("small_tree_top.png"))
            }, 25);
            sharedBigTree = new Tree(new SKNode[] {
                SKSpriteNode.FromTexture(atlas.TextureNamed("big_tree_base.png")),
                SKSpriteNode.FromTexture(atlas.TextureNamed("big_tree_middle.png")),
                SKSpriteNode.FromTexture(atlas.TextureNamed("big_tree_top.png"))
            }, 150);
            sharedBigTree.FadeAlpha = true;
            sharedLeafEmitterA      = GraphicsUtilities.EmitterNodeWithEmitterNamed("Leaves_01");
            sharedLeafEmitterB      = GraphicsUtilities.EmitterNodeWithEmitterNamed("Leaves_02");

            // Load the tiles that make up the ground layer.
            LoadWorldTiles();

            // Load assets for all the sprites within this scene.
            Cave.LoadSharedAssetsOnce();
            HeroCharacter.LoadSharedAssetsOnce();
            Archer.LoadSharedAssetsOnce();
            Warrior.LoadSharedAssetsOnce();
            Goblin.LoadSharedAssetsOnce();
            Boss.LoadSharedAssetsOnce();
        }
        public override void DidSimulatePhysics()
        {
            base.DidSimulatePhysics();

            // Get the position either of the default hero or the hero spawn point.
            HeroCharacter defaultHero = DefaultPlayer.Hero;
            CGPoint       position;

            if (defaultHero != null && Heroes.Contains(defaultHero))
            {
                position = defaultHero.Position;
            }
            else
            {
                position = DefaultSpawnCGPoint;
            }

            // Update the alphas of any trees that are near the hero (center of the camera) and therefore visible or soon to be visible.
            foreach (Tree tree in trees)
            {
                if (GraphicsUtilities.DistanceBetweenCGPoints(tree.Position, position) < 1024)
                {
                    tree.UpdateAlphaWithScene(this);
                }
            }

            if (!WorldMovedForUpdate)
            {
                return;
            }

            // Show any nearby hidden particle systems and hide those that are too far away to be seen.
            foreach (SKEmitterNode particles in particleSystems)
            {
                bool particlesAreVisible = GraphicsUtilities.DistanceBetweenCGPoints(particles.Position, position) < 1024;

                if (!particlesAreVisible && !particles.Paused)
                {
                    particles.Paused = true;
                }
                else if (particlesAreVisible && particles.Paused)
                {
                    particles.Paused = false;
                }
            }

            // Update nearby parallax sprites.
            foreach (ParallaxSprite sprite in parallaxSprites)
            {
                if (GraphicsUtilities.DistanceBetweenCGPoints(sprite.Position, position) >= 1024)
                {
                    continue;
                }

                sprite.UpdateOffset();
            }
        }
        public override void HeroWasKilled(HeroCharacter hero)
        {
            foreach (Cave cave in goblinCaves)
            {
                cave.StopGoblinsFromTargettingHero(hero);
            }

            base.HeroWasKilled(hero);
        }
        public void StartLevel()
        {
            HeroCharacter hero = AddHeroFor(DefaultPlayer);

#if MOVE_NEAR_TO_BOSS
            CGPoint bossPosition = _levelBoss.Position;             // set earlier from buildWorld in addSpawnPoints
            bossPosition.X += 128;
            bossPosition.Y += 512;
            hero.Position   = bossPosition;
#endif

            CenterWorld(hero);
        }
Example #6
0
        public override void DidSimulatePhysics()
        {
            base.DidSimulatePhysics();

            HeroCharacter defaultHero = DefaultPlayer.Hero;

            // Move the world relative to the default player position.
            if (defaultHero != null)
            {
                CGPoint heroPosition = defaultHero.Position;
                CGPoint worldPos     = world.Position;

                var yCoordinate = worldPos.Y + heroPosition.Y;
                if (yCoordinate < MIN_HERO_TO_EDGE_DISTANCE)
                {
                    worldPos.Y          = -heroPosition.Y + MIN_HERO_TO_EDGE_DISTANCE;
                    WorldMovedForUpdate = true;
                }
                else if (yCoordinate > Frame.Size.Height - MIN_HERO_TO_EDGE_DISTANCE)
                {
                    worldPos.Y          = Frame.Size.Height - heroPosition.Y - MIN_HERO_TO_EDGE_DISTANCE;
                    WorldMovedForUpdate = true;
                }

                var xCoordinate = worldPos.X + heroPosition.X;
                if (xCoordinate < MIN_HERO_TO_EDGE_DISTANCE)
                {
                    worldPos.X          = -heroPosition.X + MIN_HERO_TO_EDGE_DISTANCE;
                    WorldMovedForUpdate = true;
                }
                else if (xCoordinate > Frame.Size.Width - MIN_HERO_TO_EDGE_DISTANCE)
                {
                    worldPos.X          = Frame.Size.Width - heroPosition.X - MIN_HERO_TO_EDGE_DISTANCE;
                    WorldMovedForUpdate = true;
                }

                world.Position = worldPos;
            }

            // Using performSelector:withObject:afterDelay: withg a delay of 0.0 means that the selector call occurs after
            // the current pass through the run loop.
            // This means the property will be cleared after the subclass implementation of didSimluatePhysics completes.
            PerformSelector(new Selector("clearWorldMoved"), null, 0.0f);
        }
Example #7
0
        public virtual void HeroWasKilled(HeroCharacter hero)
        {
            if (hero == null)
            {
                throw new ArgumentNullException("hero");
            }

            Player player = hero.Player;

            Heroes.Remove(hero);

                        #if __IOS__
            player.MoveRequested = false;
                        #endif

            if (--player.LivesLeft < 1)
            {
                return;                 // In a real game, you'd want to end the game when there are no lives left.
            }
            updateHUDAfterHeroDeathFor(hero.Player);

            hero = AddHeroFor(hero.Player);
            CenterWorld(hero);
        }
Example #8
0
        public override void Update(double currentTime)
        {
            // Handle time delta.
            // If we drop below 60fps, we still want everything to move the same distance.
            double timeSinceLast = currentTime - lastUpdateTimeInterval;

            lastUpdateTimeInterval = currentTime;

            // more than a second since last update
            if (timeSinceLast > 1)
            {
                timeSinceLast       = MIN_TIME_INTERVAL;
                WorldMovedForUpdate = true;
            }

            UpdateWithTimeSinceLastUpdate(timeSinceLast);

            var           defaultPlayer = DefaultPlayer;
            HeroCharacter hero          = Heroes.Count > 0 ? defaultPlayer.Hero : null;

                        #if __IOS__
            if (hero != null && !hero.Dying &&
                defaultPlayer.TargetLocation != CGPoint.Empty)
            {
                if (defaultPlayer.FireAction)
                {
                    hero.FaceTo(defaultPlayer.TargetLocation);
                }

                if (defaultPlayer.MoveRequested)
                {
                    if (defaultPlayer.TargetLocation != hero.Position)
                    {
                        hero.MoveTowards(defaultPlayer.TargetLocation, timeSinceLast);
                    }
                    else
                    {
                        defaultPlayer.MoveRequested = false;
                    }
                }
            }
                        #endif

            foreach (var player in players)
            {
                if (player == null)
                {
                    continue;
                }

                hero = player.Hero;
                if (hero == null || hero.Dying)
                {
                    continue;
                }

                // heroMoveDirection is used by game controllers.
                CGPoint heroMoveDirection = player.HeroMoveDirection;
                if (GraphicsUtilities.Hypotenuse(heroMoveDirection.X, heroMoveDirection.Y) > 0f)
                {
                    hero.MoveInDirection(heroMoveDirection, timeSinceLast);
                }
                else
                {
                    if (player.MoveForward)
                    {
                        hero.Move(MoveDirection.Forward, timeSinceLast);
                    }
                    else if (player.MoveBack)
                    {
                        hero.Move(MoveDirection.Back, timeSinceLast);
                    }

                    if (player.MoveLeft)
                    {
                        hero.Move(MoveDirection.Left, timeSinceLast);
                    }
                    else if (player.MoveRight)
                    {
                        hero.Move(MoveDirection.Right, timeSinceLast);
                    }
                }

                if (player.FireAction)
                {
                    hero.PerformAttackAction();
                }
            }
        }
        public virtual void HeroWasKilled(HeroCharacter hero)
        {
            if (hero == null)
                throw new ArgumentNullException ("hero");

            Player player = hero.Player;

            Heroes.Remove (hero);

            #if __IOS__
            player.MoveRequested = false;
            #endif

            if (--player.LivesLeft < 1)
                return; // In a real game, you'd want to end the game when there are no lives left.

            updateHUDAfterHeroDeathFor (hero.Player);

            hero = AddHeroFor (hero.Player);
            CenterWorld (hero);
        }