private void createSomeHealthPickup(int n)
        {
            //随机生成大量血包
            int i = 0;

            for (i = 0; i < n; i++)
            {
                var(spawnPosition, spawnYaw, spawnPitch) = SpawnPoints.GetRandomSpawnPoint();
                CreateHealthPickupByPos(spawnPosition);
            }
        }
Пример #2
0
        public static EntityTemplate Player(string workerId, Vector3f position)
        {
            const string gameLogic = WorkerUtils.UnityGameLogic;
            var          client    = $"workerId:{workerId}";

            var(spawnPosition, spawnYaw, spawnPitch) = SpawnPoints.GetRandomSpawnPoint();

            var serverResponse = new ServerResponse
            {
                Position = spawnPosition.ToIntAbsolute()
            };

            var serverMovement     = ServerMovement.Component.CreateSchemaComponentData(serverResponse);
            var clientMovementData = new ClientRequest();
            var rotationUpdate     = new RotationUpdate
            {
                Yaw   = spawnYaw.ToInt1k(),
                Pitch = spawnPitch.ToInt1k()
            };
            var clientMovement    = ClientMovement.Component.CreateSchemaComponentData(clientMovementData);
            var clientRotation    = ClientRotation.Component.CreateSchemaComponentData(rotationUpdate);
            var shootingComponent = ShootingComponent.Component.CreateSchemaComponentData();
            var gunStateComponent = GunStateComponent.Component.CreateSchemaComponentData(false);
            var gunComponent      = GunComponent.Component.CreateSchemaComponentData(PlayerGunSettings.DefaultGunIndex);
            var maxHealth         = PlayerHealthSettings.MaxHealth;

            var healthComponent      = HealthComponent.Component.CreateSchemaComponentData(maxHealth, maxHealth);
            var healthRegenComponent = HealthRegenComponent.Component.CreateSchemaComponentData(false,
                                                                                                0,
                                                                                                PlayerHealthSettings.SpatialCooldownSyncInterval,
                                                                                                PlayerHealthSettings.RegenAfterDamageCooldown,
                                                                                                PlayerHealthSettings.RegenInterval,
                                                                                                PlayerHealthSettings.RegenAmount);

            return(EntityBuilder.Begin()
                   .AddPosition(spawnPosition.x, spawnPosition.y, spawnPosition.z, gameLogic)
                   .AddMetadata("Player", gameLogic)
                   .SetPersistence(false)
                   .SetReadAcl(AllWorkerAttributes)
                   .AddComponent(serverMovement, gameLogic)
                   .AddComponent(clientMovement, client)
                   .AddComponent(clientRotation, client)
                   .AddComponent(shootingComponent, client)
                   .AddComponent(gunComponent, gameLogic)
                   .AddComponent(gunStateComponent, client)
                   .AddComponent(healthComponent, gameLogic)
                   .AddComponent(healthRegenComponent, gameLogic)
                   .AddPlayerLifecycleComponents(workerId, client, gameLogic)
                   .Build());
        }
        private void OnRequestRespawn(HealthComponent.RequestRespawn.RequestResponder request)
        {
            // Reset the player's health.
            var healthUpdate = new HealthComponent.Update
            {
                Health = health.Data.MaxHealth
            };

            health.Send(healthUpdate);

            // Move to a spawn point (position and rotation)
            var(spawnPosition, spawnYaw, spawnPitch) = SpawnPoints.GetRandomSpawnPoint();
            var newLatest = new ServerResponse
            {
                Position     = spawnPosition.ToIntAbsolute(),
                IncludesJump = false,
                Timestamp    = serverMovement.Data.Latest.Timestamp,
                TimeDelta    = 0
            };

            var serverMovementUpdate = new ServerMovement.Update
            {
                Latest = newLatest
            };

            serverMovement.Send(serverMovementUpdate);

            // Update the spatial position on respawn.
            var spatialPositionUpdate = new Position.Update
            {
                Coords = spawnPosition.ToSpatialCoordinates()
            };

            spatialPosition.Send(spatialPositionUpdate);

            transform.position = spawnPosition + spatial.Worker.Origin;

            var forceRotationRequest = new RotationUpdate
            {
                Yaw       = spawnYaw.ToInt1k(),
                Pitch     = spawnPitch.ToInt1k(),
                TimeDelta = 0
            };

            serverMovement.SendForcedRotation(forceRotationRequest);

            // Trigger the respawn event.
            health.SendRespawn(new Empty());
        }
        public static EntityTemplate Player(string workerId, byte[] args)
        {
            var client = EntityTemplate.GetWorkerAccessAttribute(workerId);

            var(spawnPosition, spawnYaw, spawnPitch) = SpawnPoints.GetRandomSpawnPoint();

            var serverResponse = new ServerResponse
            {
                Position = spawnPosition.ToIntAbsolute()
            };

            var rotationUpdate = new RotationUpdate
            {
                Yaw   = spawnYaw.ToInt1k(),
                Pitch = spawnPitch.ToInt1k()
            };

            var pos = new Position.Snapshot {
                Coords = spawnPosition.ToSpatialCoordinates()
            };
            var serverMovement = new ServerMovement.Snapshot {
                Latest = serverResponse
            };
            var clientMovement = new ClientMovement.Snapshot {
                Latest = new ClientRequest()
            };
            var clientRotation = new ClientRotation.Snapshot {
                Latest = rotationUpdate
            };
            var shootingComponent = new ShootingComponent.Snapshot();
            var gunComponent      = new GunComponent.Snapshot {
                GunId = PlayerGunSettings.DefaultGunIndex
            };
            var gunStateComponent = new GunStateComponent.Snapshot {
                IsAiming = false
            };
            var healthComponent = new HealthComponent.Snapshot
            {
                Health    = PlayerHealthSettings.MaxHealth,
                MaxHealth = PlayerHealthSettings.MaxHealth,
            };

            var healthRegenComponent = new HealthRegenComponent.Snapshot
            {
                CooldownSyncInterval = PlayerHealthSettings.SpatialCooldownSyncInterval,
                DamagedRecently      = false,
                RegenAmount          = PlayerHealthSettings.RegenAmount,
                RegenCooldownTimer   = PlayerHealthSettings.RegenAfterDamageCooldown,
                RegenInterval        = PlayerHealthSettings.RegenInterval,
                RegenPauseTime       = 0,
            };

            var sessionQuery  = InterestQuery.Query(Constraint.Component <Session.Component>());
            var checkoutQuery = InterestQuery.Query(Constraint.RelativeCylinder(150));

            var interestTemplate  = InterestTemplate.Create().AddQueries <ClientMovement.Component>(sessionQuery, checkoutQuery);
            var interestComponent = interestTemplate.ToSnapshot();

            var playerName = Encoding.ASCII.GetString(args);

            var playerStateComponent = new PlayerState.Snapshot
            {
                Name   = playerName,
                Kills  = 0,
                Deaths = 0,
            };


            var playerResComponent      = new Pickups.PlayerRes.Snapshot(0);
            var soldierManagerComponent = new Soldiers.SoldierManager.Snapshot();


            var template = new EntityTemplate();

            template.AddComponent(pos, WorkerUtils.UnityGameLogic);
            template.AddComponent(new Metadata.Snapshot {
                EntityType = "Player"
            }, WorkerUtils.UnityGameLogic);
            template.AddComponent(serverMovement, WorkerUtils.UnityGameLogic);
            template.AddComponent(clientMovement, client);
            template.AddComponent(clientRotation, client);

            template.AddComponent(shootingComponent, client);
            template.AddComponent(gunComponent, WorkerUtils.UnityGameLogic);
            template.AddComponent(gunStateComponent, client);
            template.AddComponent(healthComponent, WorkerUtils.UnityGameLogic);
            template.AddComponent(healthRegenComponent, WorkerUtils.UnityGameLogic);
            template.AddComponent(playerStateComponent, WorkerUtils.UnityGameLogic);

            template.AddComponent(interestComponent, WorkerUtils.UnityGameLogic);

            template.AddComponent(playerResComponent, WorkerUtils.UnityGameLogic);
            template.AddComponent(soldierManagerComponent, WorkerUtils.UnityGameLogic);


            PlayerLifecycleHelper.AddPlayerLifecycleComponents(template, workerId, WorkerUtils.UnityGameLogic);

            template.SetReadAccess(WorkerUtils.UnityClient, WorkerUtils.UnityGameLogic, WorkerUtils.MobileClient);
            template.SetComponentWriteAccess(EntityAcl.ComponentId, WorkerUtils.UnityGameLogic);

            return(template);
        }
        public static EntityTemplate Player(string workerId, byte[] args)
        {
            var client = EntityTemplate.GetWorkerAccessAttribute(workerId);

            var(spawnPosition, spawnYaw, spawnPitch) = SpawnPoints.GetRandomSpawnPoint();

            var serverResponse = new ServerResponse
            {
                Position = spawnPosition.ToIntAbsolute()
            };

            var rotationUpdate = new RotationUpdate
            {
                Yaw   = spawnYaw.ToInt1k(),
                Pitch = spawnPitch.ToInt1k()
            };

            var pos = new Position.Snapshot {
                Coords = spawnPosition.ToSpatialCoordinates()
            };
            var serverMovement = new ServerMovement.Snapshot {
                Latest = serverResponse
            };
            var clientMovement = new ClientMovement.Snapshot {
                Latest = new ClientRequest()
            };
            var clientRotation = new ClientRotation.Snapshot {
                Latest = rotationUpdate
            };
            var shootingComponent = new ShootingComponent.Snapshot();
            var gunComponent      = new GunComponent.Snapshot {
                GunId = PlayerGunSettings.DefaultGunIndex
            };
            var gunStateComponent = new GunStateComponent.Snapshot {
                IsAiming = false
            };
            var healthComponent = new HealthComponent.Snapshot
            {
                Health    = PlayerHealthSettings.MaxHealth,
                MaxHealth = PlayerHealthSettings.MaxHealth,
            };

            var healthRegenComponent = new HealthRegenComponent.Snapshot
            {
                CooldownSyncInterval = PlayerHealthSettings.SpatialCooldownSyncInterval,
                DamagedRecently      = false,
                RegenAmount          = PlayerHealthSettings.RegenAmount,
                RegenCooldownTimer   = PlayerHealthSettings.RegenAfterDamageCooldown,
                RegenInterval        = PlayerHealthSettings.RegenInterval,
                RegenPauseTime       = 0,
            };

            var template = new EntityTemplate();

            template.AddComponent(pos, WorkerUtils.UnityGameLogic);
            template.AddComponent(new Metadata.Snapshot {
                EntityType = "Player"
            }, WorkerUtils.UnityGameLogic);
            template.AddComponent(serverMovement, WorkerUtils.UnityGameLogic);
            template.AddComponent(clientMovement, client);
            template.AddComponent(clientRotation, client);
            template.AddComponent(shootingComponent, client);
            template.AddComponent(gunComponent, WorkerUtils.UnityGameLogic);
            template.AddComponent(gunStateComponent, client);
            template.AddComponent(healthComponent, WorkerUtils.UnityGameLogic);
            template.AddComponent(healthRegenComponent, WorkerUtils.UnityGameLogic);
            PlayerLifecycleHelper.AddPlayerLifecycleComponents(template, workerId, WorkerUtils.UnityGameLogic);

            template.SetReadAccess(WorkerUtils.UnityClient, WorkerUtils.UnityGameLogic, WorkerUtils.AndroidClient, WorkerUtils.iOSClient);
            template.SetComponentWriteAccess(EntityAcl.ComponentId, WorkerUtils.UnityGameLogic);

            return(template);
        }