コード例 #1
0
        protected override void OnUpdate()
        {
            var rand = _rand;

            var mapEntity = _mapQuery.GetSingletonEntity();
            var mapState  = EntityManager.GetBuffer <MapState>(mapEntity);
            var mapData   = EntityManager.GetComponentData <MapData>(mapEntity);

            Entities
            .WithAll <Monster>()
            .WithAll <TakingATurn>()
            .WithStoreEntityQueryInField(ref _monsterQuery)
            .ForEach((Entity e, ref Position pos, ref Energy energy) =>
            {
                // Come on man
                var r = rand[0];

                var dir = GetRandomDirection(ref r);
                rand[0] = r;

                int2 dest     = pos + dir;
                int destIndex = dest.y * mapData.width + dest.x;

                if (MapUtility.CellIsUnblocked(destIndex, mapState))
                {
                    int oldIndex = MapUtility.PosToIndex(pos, mapData.width);
                    pos          = dest;

                    MapUtility.MoveActor(e, oldIndex, destIndex, mapState);
                }

                energy -= Energy.ActionThreshold;
            }).Run();
        }
コード例 #2
0
        protected override void OnUpdate()
        {
            if (_quitAction.triggered)
            {
                Application.Quit();
                return;
            }

            int2 dir = (int2)(_moveAction.triggered ? (float2)_moveAction.ReadValue <Vector2>() : float2.zero);

            if (dir.x == 0 && dir.y == 0)
            {
                return;
            }

            var mapEntity = _mapQuery.GetSingletonEntity();
            var mapData   = EntityManager.GetComponentData <MapData>(mapEntity);
            var mapState  = EntityManager.GetBuffer <MapState>(mapEntity);

            var monsterFromEntity = GetComponentDataFromEntity <Monster>(true);

            var buffer = _barrier.CreateCommandBuffer();

            Entities
            .WithReadOnly(monsterFromEntity)
            .WithAll <Player>()
            .WithAll <TakingATurn>()
            .ForEach((Entity e, ref Energy energy, ref Position pos) =>
            {
                int2 dest     = pos + dir;
                int destIndex = dest.y * mapData.width + dest.x;

                // Don't use up an action if we don't do anything meaningful
                int cost = 0;

                if (MapUtility.CellIsUnblocked(destIndex, mapState))
                {
                    int oldIndex = pos.value.y * mapData.width + pos.value.x;
                    pos          = dest;

                    // Immediately update map state so the next actors have up to date state.
                    MapUtility.MoveActor(e, oldIndex, destIndex, mapState);

                    cost = Energy.ActionThreshold;
                }
                else
                {
                    var cell = mapState[destIndex];

                    if (monsterFromEntity.Exists(cell.content))
                    {
                        buffer.AddComponent(e, new Attacking
                        {
                            target = cell.content
                        });

                        cost = Energy.ActionThreshold;
                    }
                }

                energy -= cost;
            }).Run();
        }