Example #1
0
        void touchDownBattle(Touch touch)
        {
            if (botMothership.parent != null)
            {
                if (botMothership.contains(touch.locationIn(botMothership.parent)))
                {
                    Spaceship.selectedSpaceship?.setTarget(botMothership);
                    return;
                }
            }

            Spaceship nearestSpaceship = getNearestSpaceship(new[] { mothership.spaceships, botMothership.spaceships }, touch);

            if (nearestSpaceship != null)
            {
                switch (nearestSpaceship.team)
                {
                case Mothership.Team.red:
                    Spaceship.selectedSpaceship?.setTarget(nearestSpaceship);
                    break;

                case Mothership.Team.green:
                    break;

                case Mothership.Team.blue:
                    nearestSpaceship.touchUp(touch);
                    break;
                }
                return;
            }

            if (mothership.parent != null)
            {
                if (mothership.contains(touch.locationIn(mothership.parent)))
                {
                    Spaceship.selectedSpaceship?.retreat();
                    return;
                }
            }

            Spaceship.selectedSpaceship?.touchUp(touch);
        }
Example #2
0
        void updateBattle()
        {
            if (mothership.health <= 0 || botMothership.health <= 0)
            {
                nextState = State.battleEnd;
            }

            mothership.update(botMothership, botMothership.spaceships);
            botMothership.update(mothership, mothership.spaceships);

            if (currentTime - lastBotUpdate > 1.0f)
            {
                lastBotUpdate = currentTime;

                int health = 0;
                foreach (var spaceship in mothership.spaceships)
                {
                    health += spaceship.health;
                }
                if (health <= 0 || currentTime - battleBeginTime > maxBattleDuration)
                {
                    mothership.health -= mothership.maxHealth / 10;
                    mothership.updateHealthBar(mothership.health, mothership.maxHealth);
                    if (mothership.health <= 0)
                    {
                        mothership.die();
                    }
                    else
                    {
                        run(mothership.explosionAction());
                    }
                }

                health = 0;
                foreach (var spaceship in botMothership.spaceships)
                {
                    health += spaceship.health;
                }
                if (health <= 0 || currentTime - battleBeginTime > maxBattleDuration)
                {
                    botMothership.health -= botMothership.maxHealth / 10;
                    botMothership.updateHealthBar(botMothership.health, botMothership.maxHealth);
                    if (botMothership.health <= 0)
                    {
                        botMothership.die();
                    }
                    else
                    {
                        run(botMothership.explosionAction());
                    }
                }

                List <Spaceship> aliveBotSpaceships = botMothership.spaceships.Where(spaceship => {
                    if (spaceship.destination != null)
                    {
                        if (spaceship.destination == spaceship.startingPosition)
                        {
                            return(false);
                        }
                    }

                    if ((spaceship.position - spaceship.startingPosition).LengthSquared() < 4.0f)
                    {
                        return(spaceship.health >= spaceship.maxHealth);
                    }

                    return(spaceship.health > 0);
                }).ToList();

                if (aliveBotSpaceships.Count() > 0)
                {
                    Spaceship botSpaceship = aliveBotSpaceships[random.Next(aliveBotSpaceships.Count())];

                    var aliveSpaceships = mothership.spaceships.Where(spaceship =>
                    {
                        return(spaceship.health > 0);
                    }).OrderByDescending(spaceship => {
                        float value = spaceship.health;

                        if (spaceship.element.weakness == botSpaceship.element.type)
                        {
                            value /= Element.damageMultiplier;
                        }

                        if (spaceship.element.strength == botSpaceship.element.type)
                        {
                            value *= Element.damageMultiplier;
                        }

                        return(value);
                    });

                    var targets = aliveSpaceships.Where(spaceship => {
                        if (spaceship.targetNode is Mothership)
                        {
                            var point = new Vector2(spaceship.position.X, spaceship.targetNode.position.Y);
                            if (spaceship.position.distanceTo(point) <= spaceship.weaponRange + botMothership.size.Y / 2.0f)
                            {
                                return(true);
                            }
                        }
                        return(false);
                    });

                    if (targets.Count() > 0)
                    {
                        botSpaceship.setTarget(targets.First());
                    }
                    else
                    {
                        if (botSpaceship.targetNode != null)
                        {
                            if (botSpaceship.health < botSpaceship.maxHealth / 2)
                            {
                                botSpaceship.retreat();
                            }
                        }
                        else
                        {
                            if (botSpaceship.physicsBody != null)
                            {
                                SKPhysicsBody botSpaceshipPhysicsBody = botSpaceship.physicsBody;
                                if (botSpaceshipPhysicsBody.BodyType == BodyType.Dynamic || botSpaceship.health == botSpaceship.maxHealth)
                                {
                                    if (botSpaceship.health < botSpaceship.maxHealth)
                                    {
                                        botSpaceship.retreat();
                                    }
                                    else
                                    {
                                        targets = aliveSpaceships.Where(spaceship =>
                                        {
                                            if (spaceship.targetNode is Spaceship)
                                            {
                                                return(spaceship.element.weakness == botSpaceship.element.type);
                                            }
                                            return(false);
                                        });

                                        if (targets.Count() > 0)
                                        {
                                            botSpaceship.setTarget(targets.First());
                                        }
                                        else
                                        {
                                            var x     = random.Next(-55 / 2, 55 / 2);
                                            var y     = random.Next((int)(Mothership.height / 2.0f), (int)Mothership.height);
                                            var point = botSpaceship.position + new Vector2(x, y);
                                            if (mothership.contains(point))
                                            {
                                                botSpaceship.setTarget(mothership);
                                            }
                                            else
                                            {
                                                botSpaceship.physicsBody.BodyType = BodyType.Dynamic;
                                                botSpaceship.destination          = point;
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }