Ejemplo n.º 1
0
    private void UpdateClosestGoal()
    {
        closestGoal = null;

        var closestDistance = float.PositiveInfinity;
        var flags           = FindObjectsOfType <Flag>();

        foreach (var flag in flags)
        {
            if (flag.locked || (flag.CapturedTeam == myMob.team && flag.CapturedAmount >= 1f - float.Epsilon))
            {
                continue;
            }

            var distance = (flag.transform.position - transform.position).sqrMagnitude;

            foreach (var playerInfo in MobTeams.GetTeam(myMob.team).Players)
            {
                if (!playerInfo.IsAlive())
                {
                    continue;
                }

                var player         = playerInfo.mob;
                var playerDistance = Vector3.Distance(player.transform.position, flag.transform.position);
                distance *= GoalPlayerProximityMultiplier(playerDistance, persona);
            }

            if (distance < closestDistance)
            {
                closestDistance = distance;
                closestGoal     = flag;
            }
        }
    }
Ejemplo n.º 2
0
    private void LateUpdate()
    {
        // add missing UIs for new teams
        for (var i = transform.childCount; i < MobTeams.GetNumberOfTeams() - 1; i++)
        {
            var newUI = Instantiate(teamStatusUI);
            newUI.transform.SetParent(transform, false);
            newUI.GetComponent <RectTransform>().offsetMax = new Vector2(0, -10 - 80 * i);
        }

        // get all flags
        var flags = FindObjectsOfType <Flag>();

        // update the UI for each team
        for (var i = 0; i < transform.childCount; i++)
        {
            var team    = (uint)i + 1;
            var teamUI  = transform.GetChild(i);
            var mobTeam = MobTeams.GetTeam(team);

            teamUI.Find("MobIcon").GetComponent <Image>().color  = mobTeam.teamColor;
            teamUI.Find("FlagIcon").GetComponent <Image>().color = mobTeam.teamColor;

            teamUI.Find("MobCountText").GetComponent <Text>().text  = $"{mobTeam.Mobs.Count}";
            teamUI.Find("FlagCountText").GetComponent <Text>().text = $"{flags.Where(flag => flag.IsCapturedByTeam(team)).Count()}";
            teamUI.Find("RespawnText").GetComponent <Text>().text   = $"{mobTeam.respawns}";
            teamUI.Find("ScoreText").GetComponent <Text>().text     = $"{mobTeam.score}";
        }
    }
    private void Update()
    {
        for (uint i = 0; i < MobTeams.GetNumberOfTeams(); i++)
        {
            var team = MobTeams.GetTeam(i);

            // decrease the timer
            team.respawnTimer -= Time.deltaTime;

            // respawn if zero
            if (team.respawnTimer <= 0 && team.respawns > 0)
            {
                var prefabRegistry = FindObjectOfType <PrefabRegistry>();
                var capturedFlags  = FindObjectsOfType <Flag>()
                                     .Where(flag => flag.IsCapturedByTeam(i))
                                     .ToArray();

                if (capturedFlags.Length > 0 && prefabRegistry.teamMobs[i])
                {
                    var spawn = capturedFlags[Random.Range(0, capturedFlags.Length)];
                    spawn.SpawnMob(prefabRegistry.teamMobs[i]).GetComponent <Mob>();

                    team.respawnTimer = RespawnTimerForTeam(team.Mobs.Count, capturedFlags.Length);
                    team.respawns--;
                }
            }
        }
    }
Ejemplo n.º 4
0
    public void Update()
    {
        // respawn the player if he is not alive, the player must press the respawn key
        if (IsAlive())
        {
            return;
        }

        respawnTimer -= Time.deltaTime;

        var playerTeam = MobTeams.GetTeam(team);

        if (playerTeam.respawns <= 0)
        {
            return;
        }

        var prefabRegistry = Object.FindObjectOfType <PrefabRegistry>();
        var playerFlags    = Object.FindObjectsOfType <Flag>()
                             .Where(flag => flag.IsCapturedByTeam(team))
                             .ToArray();

        if (respawnTimer < 0f && Input.GetButtonDown("Respawn") && playerFlags.Length > 0)
        {
            playerTeam.respawns--;
            respawnTimer = Globals.playerRespawnTime;

            var spawn = playerFlags[Random.Range(0, playerFlags.Length)];
            mob = spawn.SpawnMob(prefabRegistry.playerMobs[team]).GetComponent <Mob>();
        }
    }
Ejemplo n.º 5
0
    public void Awake() {
        physicsObject = GetComponent<PhysicsObject>();
        myCollider = GetComponent<Collider2D>();
        spriteRenderer = GetComponent<SpriteRenderer>();

        var mobTeam = MobTeams.GetTeam(team);
        mobTeam.Mobs.Add(this);
        if (setTeamColor) {
            mobTeam.teamColor = spriteRenderer.color;
        }

        // randomize the color a little
        var newColor = spriteRenderer.color;
        const float variance = .04f;
        newColor.r += Random.Range(-variance, variance);
        newColor.g += Random.Range(-variance, variance);
        newColor.b += Random.Range(-variance, variance);
        spriteRenderer.color = newColor;

        stunTime = Random.Range(Globals.mobStunTimeMin, Globals.mobStunTimeMax);

        eyeTarget = transform.position;

        SetRandomBody();
    }
    private void LateUpdate()
    {
        if (playersManager.LocalPlayer.IsAlive())
        {
            backdrop.enabled    = false;
            respawnText.enabled = false;
        }
        else
        {
            backdrop.enabled    = true;
            respawnText.enabled = true;

            var respawnTime = playersManager.LocalPlayer.GetRespawnTimer();
            var team        = MobTeams.GetTeam(playersManager.LocalPlayer.team);

            if (team.respawns <= 0)
            {
                respawnText.text = "Your team is out of respawn points";
            }
            else if (respawnTime < Globals.playerRespawnTime - 2f && respawnTime > 0f)
            {
                respawnText.text = $"You can spawn in {Mathf.Ceil(respawnTime)} seconds";
            }
            else if (respawnTime <= 0f)
            {
                respawnText.text = "Spawn now using the 'Respawn' key";
            }
            else
            {
                respawnText.enabled = false;
            }
        }
    }
 public void Update()
 {
     for (uint i = 0; i < MobTeams.GetNumberOfTeams(); i++)
     {
         var players = MobTeams.GetTeam(i).Players;
         players.ForEach(player => player.Update());
     }
 }
    /// <summary>
    /// Add a player to a specific team. Initializes a new playerInfo.
    /// </summary>
    /// <param name="team">The number of the team</param>
    /// <param name="local">Whether this is the local player</param>
    public void CreatePlayer(uint team, bool local = false)
    {
        var playerInfo = new PlayerInfo(team);

        MobTeams.GetTeam(team).Players.Add(playerInfo);

        if (local)
        {
            LocalPlayer = playerInfo;
        }
    }
Ejemplo n.º 9
0
    private void UpdateClosestEnemy()
    {
        closestEnemy = null;

        var numberOfTeams   = MobTeams.GetNumberOfTeams();
        var closestDistance = float.PositiveInfinity;

        for (uint i = 0; i < numberOfTeams; i++)
        {
            if (myMob.team == i && i != 0)
            {
                continue;
            }

            var team = MobTeams.GetTeam(i);
            if (team.Mobs.Count > 0)
            {
                foreach (var enemy in team.Mobs)
                {
                    var distance = (transform.position - enemy.transform.position).sqrMagnitude;

                    foreach (var playerInfo in MobTeams.GetTeam(myMob.team).Players)
                    {
                        if (!playerInfo.IsAlive())
                        {
                            continue;
                        }

                        var player         = playerInfo.mob;
                        var playerDistance = Vector3.Distance(player.transform.position, enemy.transform.position);
                        distance *= EnemyPlayerProximityMultiplier(playerDistance, persona);
                    }

                    if (enemy.IsStunned())
                    {
                        distance *= persona.StunnedEnemyPenalty();
                    }

                    if (distance < closestDistance)
                    {
                        closestDistance = distance;
                        closestEnemy    = enemy;
                    }
                }
            }
        }
    }
Ejemplo n.º 10
0
    protected AITarget GetClosestTarget()
    {
        var closestEnemy = controller.GetClosestEnemy();
        var closestGoal  = controller.GetClosestGoal();
        var persona      = controller.GetPersona();

        if (closestEnemy && closestGoal)
        {
            var enemyDistance = Vector3.Distance(mob.transform.position, closestEnemy.transform.position);
            var goalDistance  = Vector3.Distance(mob.transform.position, closestGoal.transform.position);

            foreach (var playerInfo in MobTeams.GetTeam(mob.team).Players)
            {
                if (!playerInfo.IsAlive())
                {
                    continue;
                }

                var player = playerInfo.mob;

                var enemyPlayerDistance = Vector3.Distance(player.transform.position, closestEnemy.transform.position);
                var goalPlayerDistance  = Vector3.Distance(player.transform.position, closestGoal.transform.position);

                enemyDistance *= controller.EnemyPlayerProximityMultiplier(enemyPlayerDistance, persona);
                goalDistance  *= controller.GoalPlayerProximityMultiplier(goalPlayerDistance, persona);
            }

            if (closestEnemy.IsStunned())
            {
                enemyDistance *= persona.StunnedEnemyPenalty();
            }

            goalDistance = goalDistance * persona.EnemyGoalImportanceRatio() + persona.GoalDistanceOffset();

            return(enemyDistance < goalDistance ? AITarget.Enemy : AITarget.Goal);
        }

        if (closestEnemy)
        {
            return(AITarget.Enemy);
        }

        return(closestGoal ? AITarget.Goal : AITarget.None);
    }
Ejemplo n.º 11
0
    private void HandleDeadPlayer()
    {
        deadTimer -= Time.deltaTime;

        // set initial position
        if (!initialPosition && fallbackFlag)
        {
            initialPosition = true;
            FollowObject(fallbackFlag);
        }

        if (deadTimer <= 0)
        {
            if (followedMob)
            {
                FollowObject(followedMob);
            }
            else
            {
                // reset death timer
                deadTimer = Globals.cameraAfterDeathTimeout;
            }
        }
        else if (!followedMob)
        {
            // quickly find another mob
            var mobs = MobTeams.GetTeam(playersManager.LocalPlayer.team).Mobs;
            if (mobs.Count > 0)
            {
                followedMob = mobs[Random.Range(0, mobs.Count)];
            }

            // fallback
            else if (fallbackFlag)
            {
                FollowObject(fallbackFlag);
            }
        }
    }
Ejemplo n.º 12
0
 public void OnDestroy() {
     MobTeams.GetTeam(team).Mobs.Remove(this);
 }
Ejemplo n.º 13
0
    private void CheckHeadStomping() {
        var boxcastPosition = new Vector2(transform.position.x,
            transform.position.y + myCollider.bounds.extents.y / 2f);
        var boxcastSize = physicsObject.size;
        boxcastSize.x -= Globals.skinThickness * 2f;
        boxcastSize.y -= Globals.skinThickness * 2f;

        // TODO: Use only OverlapBox
        var colliders = Physics2D.OverlapBoxAll(boxcastPosition, boxcastSize, 0,
            LayerMask.GetMask(Globals.mobLayerName));
        foreach (var col in colliders) {
            if (col == myCollider) {
                continue;
            }

            var otherMob = col.GetComponent<Mob>();
            if (!otherMob) {
                continue;
            }

            var otherTransform = col.transform;
            var otherPhysicsObject = col.GetComponent<PhysicsObject>();

            // stunned mobs can't stomp
            if (otherMob.IsStunned()) {
                continue;
            }

            if (team == otherMob.team && team != 0) {
                continue;
            }

            if (otherPhysicsObject.velocity.y < 0 &&
                otherTransform.position.y - col.bounds.extents.y > transform.position.y &&
                otherTransform.position.x - col.bounds.extents.x < transform.position.x +
                myCollider.bounds.extents.x - Globals.skinThickness * 2f &&
                otherTransform.position.x + col.bounds.extents.x > transform.position.x -
                myCollider.bounds.extents.x + Globals.skinThickness * 2f) {
                // apply positive velocity to the other mob according to the formula:
                // new Y velocity = maximum(standard jump velocity / 2, mob's current positive velocity * .7 + my velocity * .4)
                otherPhysicsObject.velocity.y = Mathf.Max(CalculateVelocityForJumpHeight(Globals.mobJumpHeight / 2f),
                    -otherPhysicsObject.velocity.y * .7f + physicsObject.velocity.y * .4f);

                lives--;

                if (physicsObject.velocity.y > Mathf.Epsilon) {
                    physicsObject.velocity.x /= 1.5f;
                    physicsObject.velocity.y *= -1;
                }
                else {
                    var xRange = lives == 0 ? 4f : 2f;
                    var yRange = 2f;
                    var yHeight = lives == 0 ? 1.5f : .5f;

                    physicsObject.velocity.x = Random.Range(-xRange, xRange);
                    physicsObject.velocity.y = CalculateVelocityForJumpHeight(yHeight) + Random.Range(-yRange, yRange);
                }

                var prefabRegistry = FindObjectOfType<PrefabRegistry>();

                // add score to the enemy
                MobTeams.GetTeam(otherMob.team).score += 10;
                var scoreText = Instantiate(prefabRegistry.floatingText, transform.position, transform.rotation);
                scoreText.GetComponent<FloatingText>().SetText("+10");

                if (lives == 0) {
                    var body = Instantiate(prefabRegistry.deadBody, transform.position, transform.rotation);
                    body.GetComponent<SpriteRenderer>().color = spriteRenderer.color;
                    body.GetComponent<SpriteRenderer>().sprite = spriteRenderer.sprite;
                    body.GetComponent<PhysicsObject>().velocity = physicsObject.velocity;

                    Destroy(gameObject);

                    // add more score to the enemy for killing (10 already added)
                    MobTeams.GetTeam(otherMob.team).score += 10;
                    scoreText.GetComponent<FloatingText>().SetText("+20");
                }
                else {
                    stunned = true;
                    Instantiate(prefabRegistry.lostHeart, new Vector3(transform.position.x,
                        transform.position.y + myCollider.bounds.extents.y,
                        transform.position.z), transform.rotation);
                }

                for (var i = 0; i < Globals.starParticleCount; i++) {
                    Instantiate(prefabRegistry.starParticle, new Vector3(transform.position.x,
                        transform.position.y + myCollider.bounds.extents.y,
                        transform.position.z), transform.rotation);
                }
            }
        }
    }
Ejemplo n.º 14
0
    private void CheckCapturing()
    {
        if (locked)
        {
            return;
        }

        var mobs = Physics2D.OverlapBoxAll(myCollider.bounds.center, myCollider.bounds.size, 0f,
                                           LayerMask.GetMask(Globals.mobLayerName));

        var someoneCapturing = false;

        foreach (var mobObject in mobs)
        {
            var mob = mobObject.GetComponent <Mob>();

            // stunned mobs can't capture
            if (mob.IsStunned())
            {
                continue;
            }

            someoneCapturing = true;

            if (capturedTeam == mob.team)
            {
                capturedAmount = Mathf.Min(1f, capturedAmount + Globals.goalCaptureAmountPerMob * Time.deltaTime);

                if (capturedAmount >= 1f - float.Epsilon && !scoredForCapturing)
                {
                    scoredForCapturing                = true;
                    previousCapturedTeam              = (int)capturedTeam;
                    MobTeams.GetTeam(mob.team).score += 5;

                    var prefabRegistry = FindObjectOfType <PrefabRegistry>();
                    var scoreText      = Instantiate(prefabRegistry.floatingText, transform.position, transform.rotation);
                    scoreText.GetComponent <FloatingText>().SetText("+5");
                }
            }
            else
            {
                capturedAmount -= Globals.goalCaptureAmountPerMob * Time.deltaTime;

                if (capturedAmount < -float.Epsilon)
                {
                    capturedTeam   = mob.team;
                    capturedAmount = 0f;
                    movingFlag.GetComponent <SpriteRenderer>().color = MobTeams.GetTeam(mob.team).teamColor;
                    scoredForCapturing = false;
                }
            }
        }

        // if noone is capturing, slowly decrease the amount if not fully captured
        if (!someoneCapturing)
        {
            if (capturedTeam == previousCapturedTeam)
            {
                capturedAmount = Mathf.Min(1f, capturedAmount + Globals.goalCaptureAmountAlone * Time.deltaTime);
            }
            else
            {
                capturedAmount = Mathf.Max(0f, capturedAmount - Globals.goalCaptureAmountAlone * Time.deltaTime);
            }
        }
    }
Ejemplo n.º 15
0
 public void Start()
 {
     movingFlag.GetComponent <SpriteRenderer>().color = MobTeams.GetTeam(capturedTeam).teamColor;
 }