// Start is called before the first frame update void Start() { Random rand = new Random((uint)UnityEngine.Random.Range(1, int.MaxValue)); var randRef = new NativeReference <Random>(Allocator.Temp); randRef.Value = rand; for (int i = 0; i < Iterations; ++i) { int val = randRef.RollDice(NumDice, Sides); while (val >= ints.Count) { ints.Add(0); } ints[val]++; } }
public void Execute(ArchetypeChunk chunk, int chunkIndex, int firstEntityIndex) { var viewAccessor = chunk.GetBufferAccessor(ROViewHandle); var obstacles = MapStateData.Obstacles; var mapEntities = MapStateData.Entities; int playerIndex = 0; if (PlayerEntity != Entity.Null) { int2 playerPos = PosFromEntity[PlayerEntity]; playerIndex = obstacles.PosToIndex(playerPos); } var entities = chunk.GetNativeArray(EntityHandle); var energyArr = chunk.GetNativeArray(EnergyHandle); var attackArr = chunk.GetNativeArray(AttackPowerHandle); //Debug.Log("Monster AI running"); for (int i = 0; i < viewAccessor.Length; ++i) { Entity e = entities[i]; var viewBuffer = viewAccessor[i].Reinterpret <bool>(); var pathMap = new PathingBitGrid(obstacles, Allocator.Temp); //Debug.Log($"Monster is taking a turn"); energyArr[i] -= Energy.ActionThreshold; // For testing purposes - comment out // "RequireSingletonForUpdate<Player>()" in OnCreate to // let monsters wander in "real time" if (PlayerEntity == Entity.Null) { Wander(e, RNG, PosFromEntity, pathMap, obstacles, mapEntities); return; } if (viewBuffer.Length == 0) { return; } bool playerIsVisible = viewBuffer[playerIndex]; if (playerIsVisible) { int2 curr = PosFromEntity[e]; int startIndex = obstacles.PosToIndex(curr); if (startIndex == playerIndex) { continue; } // Open the player's space for pathfinding pathMap[playerIndex] = false; var astar = new AStar(10, Allocator.Temp); var path = new NativeList <int>(10, Allocator.Temp); astar.FindPath(pathMap, startIndex, playerIndex, path); if (path.Length > 2) { //Debug.Log("MONSTER MOVED"); int2 next = obstacles.IndexToPos(path[1]); MapUtility.MoveActor(e, PosFromEntity, curr, next, obstacles, mapEntities); } if (path.Length == 2) { //Debug.Log("Monster is next to the player"); var defend = DefendBFE[PlayerEntity]; var attack = attackArr[i]; int dmg = RNG.RollDice(attack); defend.Add(new CombatDefendBuffer { Attacker = e, Damage = dmg }); } pathMap[playerIndex] = true; } } }