private void PrepareAttackOn(GameObject defender)
    {
        GameObject attacker           = gameObject;
        UnitData   defenderData       = defender.GetComponent <UnitData>();
        Parameters defenderParameters = defender.GetComponent <Parameters>();



        Int2 aPos = new Int2(
            (int)Mathf.Floor(this.transform.position.x),
            (int)Mathf.Floor(this.transform.position.z));

        Int2 dPos = new Int2(
            (int)Mathf.Floor(defender.transform.position.x),
            (int)Mathf.Floor(defender.transform.position.z));

        if (unitData.UnitTeam == defender.GetComponent <UnitData>().UnitTeam)
        {
            Debug.Log("Cannot attack an ally!");
            return;
        }

        if (Int2.Distance(aPos, dPos) > weaponRange)
        {
            Debug.Log("Cannot attack a unit that is out of range!");
            return;
        }

        Debug.Log("Unit is attacking this unit for " + Attack.CalculateProjectedDamage(attacker, defender) + " damage but will take " +
                  Attack.CalculateProjectedDamage(defender, attacker) + " damage.");

        Attack.CommenceBattle(gameObject, defender);
        Debug.Log(unitData.UnitName + " is now at " + unitParameters.CHP + " HP and " + defenderData.UnitName + " now has " + defenderParameters.CHP);

        if (defenderParameters.CHP == 0)
        {
            // Ded
            UnitStateController defenderController = defender.GetComponent <UnitStateController>();
            defenderController.KillUnit();
        }
        else if (gameObject.GetComponent <Parameters>().CHP == 0)
        {
            // This ded
            this.KillUnit();
        }
    }
Beispiel #2
0
        public override void OnFixedUpdate()
        {
            fups++;
            var camPos       = Camera.MainCamera.Position;
            var intCamPos    = new Int2((int)camPos.X, (int)camPos.Z);
            var currentChunk = intCamPos / Chunk.BLOCK_SIZE_CM / Chunk.SEGMENT_SIZE;

            // Fast way to sync chunk pos with negative numbers
            if (intCamPos.X < 0)
            {
                currentChunk.X--;
            }
            if (intCamPos.Y < 0)
            {
                currentChunk.Y--;
            }

            StringBuilder debugText = new StringBuilder();

            debugText.AppendLine("View mode: " + MainRenderTask.Instance.View.Mode);
            debugText.AppendLine($"Camera position (real):{camPos / Chunk.BLOCK_SIZE_CM} ({camPos})");
            debugText.AppendLine($"Chunk: X:{currentChunk.X} Z:{currentChunk.Y}");
            debugText.AppendLine($"FPS: {Time.FramesPerSecond}");
            debugText.AppendLine($"UPS (target): {lastUPS} ({Time.UpdateFPS})");
            debugText.AppendLine($"PUPS: {lastFUPS} ({Time.PhysicsFPS})");
            debugText.AppendLine("QUEUE STATS:");

            lock (_loadQueueLocker)
                debugText.AppendLine("    LOAD: " + _loadQueue.Count);

            /* lock (_unloadQueueLocker)
             *   debugText.AppendLine("    UNLOAD: " + _unloadQueue.Count);*/

            lock (_updateQueueLocker)
                debugText.AppendLine("    UPDATE: " + _updateQueue.Count);


            if (DebugTextLabel != null && DebugTextLabel.Control is Label l2)
            {
                l2.Text = debugText.ToString();
            }

            // Spawn chunks to fill the grid
            for (var x = -ChunkRadius; x <= ChunkRadius; x++)
            {
                for (var z = -ChunkRadius; z <= ChunkRadius; z++)
                {
                    if (x * x + z * z > ChunkRadius * ChunkRadius)
                    {
                        continue;
                    }
                    var chunkX   = x + currentChunk.X;
                    var chunkZ   = z + currentChunk.Y;
                    var chunkPos = new Int2(chunkX, chunkZ);

                    if (!_chunks.TryGetValue(chunkPos, out _))
                    {
                        SpawnChunk(chunkPos);
                    }
                }
            }


            // Remove chunks outside of view

            var toRemove = _chunks.AsParallel().Where(c =>
                                                      Int2.Distance(c.Key, currentChunk) > ChunkRadius + ChunkUnloadOffset &&
                                                      !c.Value.IsQueuedForUnload &&
                                                      !c.Value.IsQueuedForLoad
                                                      ).Select(c => c.Value).ToArray();

            if (toRemove.Length == 0)
            {
                return;
            }

            foreach (var chunk in toRemove)
            {
                chunk.IsQueuedForUnload = true;
                Destroy(chunk.Actor);
            }

            /* lock (_unloadQueueLocker)
             *   _unloadQueue.EnqueueRange(toRemove);*/
        }
 static float Heuristic(Int2 start, Int2 goal)
 {
     return(Int2.Distance(start, goal)); // manhattan distance(per-axis)
 }