Exemplo n.º 1
0
        //Player input gets processed into a ControlState, and AI will provide a control state
        public static SimulationState ProcessState(SimulationState state, SimulationData simulationData, Dictionary <uint, VehicleControls> controllerInputs, float deltaTime)
        {
            int             currentVehicleCount  = state.Vehicles.Length;
            SimulationState nextSimState         = new SimulationState(currentVehicleCount);
            int             totalProjectileCount = 0;

            for (int i = 0; i < state.Projectiles.Length; ++i)
            {
                var projectiles = state.Projectiles[i];
                int localCount  = 0;
                if (projectiles != null)
                {
                    localCount = projectiles.Count;
                }
                totalProjectileCount += localCount;
                nextSimState.SetProjectileCount(state.IndexToID[i], localCount + 1);
            }
            for (int vehicleIndex = 0; vehicleIndex < state.Vehicles.Length; ++vehicleIndex)
            {
                uint controllerID = state.IndexToID[vehicleIndex];
                Debug.Assert(controllerInputs.ContainsKey(controllerID));

                VehiclePrototype prototype           = simulationData.GetVehiclePrototype(controllerID);
                VehicleControls  inputControlState   = controllerInputs[controllerID];
                VehicleState     currentVehicleState = state.Vehicles[vehicleIndex];

                DynamicTransform2 newDynamicTransform = ProcessVehicleDrive(currentVehicleState.DynamicTransform, prototype, inputControlState.DriveControls, deltaTime);
                newDynamicTransform = ProcessCollision(newDynamicTransform, prototype, simulationData);

                GunState currentGunState = currentVehicleState.GunState;
                GunMount gunMount        = prototype.Guns;
                bool     projectileFired;
                GunState nextGunState = ProcessGunstate(gunMount, currentGunState, inputControlState.GunTriggerDown, deltaTime, out projectileFired);
                if (projectileFired)
                {
                    DynamicPosition2 projectileState = CreateProjectileState(newDynamicTransform, gunMount, currentGunState.NextGunToFire, deltaTime);
                    SpawnProjectile(vehicleIndex, nextSimState, projectileState);
                }

                VehicleState newVehicleState = new VehicleState(newDynamicTransform, inputControlState.DriveControls, nextGunState);

                nextSimState.AddVehicle(controllerID, newVehicleState);
            }
            //TODO: The above resulting transforms can be put in a collection ready for collision detection below!
            for (int projectileIndex = 0; projectileIndex < state.Projectiles.Length; ++projectileIndex)
            {
                var projectiles = state.Projectiles[projectileIndex];
                if (projectiles != null)
                {
                    foreach (var projectile in projectiles)
                    {
                        bool hit = false;
                        for (int targetVehicleIndex = 0; targetVehicleIndex < state.Vehicles.Length; ++targetVehicleIndex)
                        {
                            if (targetVehicleIndex != projectileIndex)
                            {
                                VehicleState vehicleToHit = state.Vehicles[targetVehicleIndex];
                                if (ProjectileHitsVehicle(vehicleToHit.DynamicTransform, simulationData.GetVehiclePrototype(state.IndexToID[targetVehicleIndex]), projectile, deltaTime))
                                {
                                    hit = true;
                                    break;
                                }
                            }
                        }
                        if (!hit)
                        {
                            Vector2 nextPosition = projectile.Position + deltaTime * projectile.Velocity;
                            if (simulationData.InsideArena(nextPosition))
                            {
                                DynamicPosition2 nextProjectileState = new DynamicPosition2(nextPosition, projectile.Velocity);
                                nextSimState.Projectiles[projectileIndex].Add(nextProjectileState);
                            }
                        }
                        else
                        {
                            RegisterHit(nextSimState, projectileIndex);
                        }
                    }
                }
            }
            return(nextSimState);
        }