Example #1
0
        private static GunState ProcessGunstate(GunMount mount, GunState gunState, bool triggerDown, float deltaTime, out bool projectileFired)
        {
            int   nextGunToFire  = gunState.NextGunToFire;
            float timeToNextShot = Math.Max(gunState.TimeToNextShot - deltaTime, 0f);

            projectileFired = triggerDown && (timeToNextShot == 0f);
            if (projectileFired)
            {
                int gunCount = mount.LocalMountOffsets.Length;
                timeToNextShot = mount.MountedGun.DelayBetweenShots / (float)gunCount;
                nextGunToFire  = (nextGunToFire + 1) % gunCount;
            }
            return(new GunState(nextGunToFire, timeToNextShot));
        }
Example #2
0
        public static DynamicPosition2 CreateProjectileState(DynamicTransform2 shooterDynamicState, GunMount gunMount, int firingBarrel, float deltaTime)
        {
            Vector2          gunLocalOffset  = gunMount.LocalMountOffsets[firingBarrel];
            Vector2          shotPosition    = shooterDynamicState.Position + shooterDynamicState.Orientation.LocalToGlobal(gunLocalOffset);
            Vector2          shotVelocity    = gunMount.MountedGun.ShotSpeed * shooterDynamicState.Orientation.Facing + shooterDynamicState.Velocity;
            DynamicPosition2 projectileState = new DynamicPosition2(shotPosition + deltaTime * shotVelocity, shotVelocity);

            return(projectileState);
        }
Example #3
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);
        }