void SpawnArcher(int playerId, bool firstPlayer, Transform spawnPoint)
    {
        var ghostId = firstPlayer
            ? MobileRTSGhostSerializerCollection.FindGhostType <AArcherSnapshotData>()
            : MobileRTSGhostSerializerCollection.FindGhostType <BArcherSnapshotData>();
        var prefab = GetPrefab(ghostId);

        var unit        = EntityManager.Instantiate(prefab);
        var spawnOffset = new float3(spawnPoint.transform.position)
        {
            y = 0
        };

        PostUpdateCommands.SetComponent(unit, new PlayerUnit
        {
            PlayerId = playerId,
            UnitId   = 2
        });
        PostUpdateCommands.AddComponent(unit, new MoveTo
        {
            position  = spawnOffset + new float3(random.NextFloat(-10f, 10f), 0, random.NextFloat(-10f, 10f)),
            moveSpeed = 6f,
            move      = true
        });
        PostUpdateCommands.AddComponent(unit, new Attack
        {
            AttackRadius = 50,
            AttackedAt   = 0
        });
        PostUpdateCommands.SetComponent(unit, new Health {
            Value = 50
        });
    }
Ejemplo n.º 2
0
    protected override void OnUpdate()
    {
        Entities.WithNone <SendRpcCommandRequestComponent>().ForEach(
            (Entity reqEnt, ref GoInGameRequest req, ref ReceiveRpcCommandRequestComponent reqSrc) =>
        {
            PostUpdateCommands.AddComponent <NetworkStreamInGame>(reqSrc.SourceConnection);
            Debug.Log(string.Format("Server setting connection {0} to in game",
                                    EntityManager.GetComponentData <NetworkIdComponent>(reqSrc.SourceConnection).Value));


            var ghostCollection = GetSingleton <GhostPrefabCollectionComponent>();
            var ghostId         = MobileRTSGhostSerializerCollection.FindGhostType <PlayerSnapshotData>();
            var prefab          = EntityManager.GetBuffer <GhostPrefabBuffer>(ghostCollection.serverPrefabs)[ghostId]
                                  .Value;
            var player = EntityManager.Instantiate(prefab);
            EntityManager.SetComponentData(player,
                                           new Player
            {
                PlayerId = EntityManager.GetComponentData <NetworkIdComponent>(reqSrc.SourceConnection).Value
            });

            PostUpdateCommands.AddBuffer <PlayerInput>(player);
            PostUpdateCommands.SetComponent(reqSrc.SourceConnection,
                                            new CommandTargetComponent {
                targetEntity = player
            });

            PostUpdateCommands.AddComponent(player, new PlayerConfig {
                Civilians = 2, Archers = 1
            });

            PostUpdateCommands.DestroyEntity(reqEnt);
        });
    }
Ejemplo n.º 3
0
    protected override JobHandle OnUpdate(JobHandle inputDeps)
    {
        var ECB             = new EntityCommandBuffer(Allocator.Temp);
        var ghostCollection = GetSingleton <GhostPrefabCollectionComponent>();
        var ghostId         = MobileRTSGhostSerializerCollection.FindGhostType <ArrowSnapshotData>();
        var arrowPrefab     = EntityManager.GetBuffer <GhostPrefabBuffer>(ghostCollection.serverPrefabs)[ghostId].Value;
        var time            = UnityEngine.Time.time;

        Entities.WithoutBurst().ForEach((
                                            Entity Entity,
                                            ref Archery archery,
                                            in PlayerUnit playerUnit,
                                            in Translation translation) =>
        {
            var produced = time - archery.ProducedAt < 10;

            if (produced || archery.Units == 0)
            {
                return;
            }

            var firstPlayer = playerUnit.PlayerId == 1;
            SpawnArcher(ECB, playerUnit.PlayerId, firstPlayer, translation.Value + new float3(5, 0, 0));

            archery.ProducedAt = time;
            archery.Units--;
        }).Run();
Ejemplo n.º 4
0
    protected override JobHandle OnUpdate(JobHandle inputDeps)
    {
        var ECB             = new EntityCommandBuffer(Allocator.Temp);
        var ghostCollection = GetSingleton <GhostPrefabCollectionComponent>();
        var ghostId         = MobileRTSGhostSerializerCollection.FindGhostType <ArrowSnapshotData>();
        var arrowPrefab     = EntityManager.GetBuffer <GhostPrefabBuffer>(ghostCollection.serverPrefabs)[ghostId].Value;

        var time = UnityEngine.Time.time;

        Entities.WithoutBurst().ForEach((
                                            Entity entity,
                                            ref Attack attack,
                                            in PlayerUnit playerUnit,
                                            in Translation translation) =>
        {
            var attacked = time - attack.AttackedAt < 2;
            if (attacked)
            {
                return;
            }

            var query       = GetEntityQuery(ComponentType.ReadOnly <PlayerUnit>(), ComponentType.ReadOnly <Translation>());
            var positions   = query.ToComponentDataArray <Translation>(Allocator.TempJob);
            var playerUnits = query.ToComponentDataArray <PlayerUnit>((Allocator.TempJob));

            for (int i = 0; i < playerUnits.Length; i++)
            {
                if (playerUnits[i].PlayerId == playerUnit.PlayerId)
                {
                    continue;
                }

                var distance = math.distance(positions[i].Value, translation.Value);
                if (distance < attack.AttackRadius)
                {
                    Shoot(ECB, arrowPrefab, translation.Value, positions[i].Value);

                    // update components
                    attack.AttackedAt = time;
                    attacked          = true;
                    break;
                }
            }

            positions.Dispose();
            playerUnits.Dispose();
        }).Run();
Ejemplo n.º 5
0
    protected override void OnUpdate()
    {
        var group     = World.GetExistingSystem <GhostPredictionSystemGroup>();
        var tick      = group.PredictingTick;
        var deltaTime = Time.DeltaTime;
        var time      = UnityEngine.Time.time;

        // for each  input from each client
        Entities.ForEach((DynamicBuffer <PlayerInput> inputBuffer, ref PredictedGhostComponent prediction, ref Player player) =>
        {
            if (!GhostPredictionSystemGroup.ShouldPredict(tick, prediction))
            {
                Debug.Log("discarted");
                return;
            }
            inputBuffer.GetDataAtTick(tick, out PlayerInput input);

            var playerId       = player.PlayerId;
            var selectionInput = false;
            bool buildInput    = false;

            float minX = math.min(input.selectionX1, input.selectionX2);
            float maxX = math.max(input.selectionX1, input.selectionX2);
            float minZ = math.min(input.selectionZ1, input.selectionZ2);
            float maxZ = math.max(input.selectionZ1, input.selectionZ2);

            if (minX != 0 || maxX != 0 || minZ != 0 || maxZ != 0)
            {
                selectionInput = true;
            }

            if (input.buildX != 0 || input.buildZ != 0)
            {
                buildInput = true;
            }

            if (buildInput)
            {
                Debug.Log("buidl");
                var firstPlayer     = player.PlayerId == 1;
                var ghostCollection = GetSingleton <GhostPrefabCollectionComponent>();
                var ghostId         = firstPlayer
                    ? MobileRTSGhostSerializerCollection.FindGhostType <A_BarracksSnapshotData>()
                    : MobileRTSGhostSerializerCollection.FindGhostType <B_BarracksSnapshotData>();
                var prefab   = EntityManager.GetBuffer <GhostPrefabBuffer>(ghostCollection.serverPrefabs)[ghostId].Value;
                var building = EntityManager.Instantiate(prefab);
                EntityManager.SetComponentData(building, new PlayerUnit {
                    PlayerId = player.PlayerId
                });
                EntityManager.SetComponentData(building, new Translation
                {
                    Value = new float3(input.buildX, 0, input.buildZ)
                });
                EntityManager.SetComponentData(building, new Health {
                    Value = 100
                });
                EntityManager.AddComponentData(building, new Archery
                {
                    ProducedAt = time,
                    Units      = 15
                });

                input.buildX = input.buildZ = 0;
            }
            else if (selectionInput)
            {
                // deselect all units // not at all optimal
                Entities.ForEach((Entity entity, ref UnitSelectionState selectionState, ref PlayerUnit playerUnit) =>
                {
                    if (playerUnit.PlayerId == playerId)
                    {
                        PostUpdateCommands.SetComponent(entity, new UnitSelectionState());
                    }
                });

                // for each unit of client
                Entities.ForEach((Entity entity, ref Translation unitTrans, ref PlayerUnit playerUnit) =>
                {
                    if (playerUnit.PlayerId == playerId)
                    {
                        if (minX <= unitTrans.Value.x &&
                            maxX >= unitTrans.Value.x &&
                            minZ <= unitTrans.Value.z &&
                            maxZ >= unitTrans.Value.z)
                        {
                            PostUpdateCommands.SetComponent(entity, new UnitSelectionState()
                            {
                                IsSelected = true
                            });
                        }
                    }
                });
            }
            // if destination input
            else if (input.destinationX != 0 || input.destinationZ != 0)
            {
                var destination   = new float3(input.destinationX, 0, input.destinationZ);
                var positions     = GetPositionListAround(destination);
                int positionIndex = 0;
                Entities.ForEach((Entity entity, ref MoveTo moveTo, ref UnitSelectionState selectionState, ref PlayerUnit playerUnit) =>
                {
                    if (selectionState.IsSelected && playerUnit.PlayerId == playerId)
                    {
                        moveTo.position = positions[positionIndex++];
                        moveTo.move     = true;
                    }
                });
            }
        });
    }