Пример #1
0
        public void update(GameTime gameTime, Car car)
        {
            // Calculate target angular acceleration
            float angular = car.angle - angle;

            // Clamp angular acceleration
            if (Math.Abs(angular) > maxAngularAcceleration)
            {
                angular /= Math.Abs(angular);
                angular *= maxAngularAcceleration;
            }

            if (Math.Abs(angular) < 0.022f)
            {
                rotation = 0.0f;
                angular = 0.0f;
                angle = car.angle;
            }

            // Add angular to rotation
            rotation += angular * gameTime.ElapsedGameTime.Milliseconds / 1000.0f;

            // Limit rotation
            if (Math.Abs(rotation) > maxRotation)
            {
                rotation /= Math.Abs(rotation);
                rotation *= maxRotation;
            }

            // Add rotation to angle
            angle += rotation * gameTime.ElapsedGameTime.Milliseconds / 1000.0f;

            Vector3 target = new Vector3((float)System.Math.Sin(-angle), (float)System.Math.Cos(-angle), 0.0f);
            target *= distance;
            target = car.position - target;
            position = target;

            // Look ahead
            Vector3 lookAt = new Vector3((float)System.Math.Sin(-car.angle), (float)System.Math.Cos(-car.angle), 0.0f);
            lookAt.Normalize();
            lookAt *= aheadDistance;
            lookAt += car.position;

            // Update camera view
            view = Matrix.CreateLookAt(new Vector3(position.X, height, -position.Y),
                                                   new Vector3(lookAt.X, height, -lookAt.Y),
                                                   Vector3.UnitY);
        }
Пример #2
0
        public void setInitialPosition(Car car)
        {
            angle = car.angle;
            Vector3 translation = new Vector3((float)System.Math.Sin(angle), (float)System.Math.Cos(angle), 0.0f);
            translation *= distance;
            position = car.position - translation;

            game.camera.view = Matrix.CreateLookAt(new Vector3(position.X, height, -position.Y),
                                                   new Vector3(car.position.X, height, -car.position.Y),
                                                   Vector3.UnitY);
        }
Пример #3
0
        public GameState(UrbanRace game)
            : base(game)
        {
            // Initialise attributes and properties
            this.type = Type.GAME;
            this.laps = 1;
            this.levelName = "level.xml";
            this.level = null;
            this.car = null;
            this.checkPoints = new List<CheckPoint>();
            this.timeBonuses = new List<TimeBonus>();
            this.sceneObjects = new List<GameObject>();
            this.geometry = new List<GameObject>();
            this.skyBox = null;
            this.terrain = null;
            this.collisionManager = new CollisionManager();
            this.font = null;
            this.speedPanel = null;
            this.nextCheckPoint = 0;

            // Audio
            this.song = null;
            this.carCrash = null;
            this.pickTime = null;

            // GUI
            speedPanelPos = new Vector2(Settings.getOpt("gameSpeedPanelX"), Settings.getOpt("gameSpeedPanelY"));
            speedTextPos = new Vector2(Settings.getOpt("gameSpeedTextX"), Settings.getOpt("gameSpeedTextY"));
            lapsPanelPos = new Vector2(Settings.getOpt("gameLapsPanelX"), Settings.getOpt("gameLapsPanelY"));
            lapsText1Pos = new Vector2(Settings.getOpt("gameLapsText1X"), Settings.getOpt("gameLapsText1Y"));
            lapsText2Pos = new Vector2(Settings.getOpt("gameLapsText2X"), Settings.getOpt("gameLapsText2Y"));
            timePanelPos = new Vector2(Settings.getOpt("gameTimePanelX"), Settings.getOpt("gameTimePanelY"));
            timeTextPos = new Vector2(Settings.getOpt("gameTimeTextX"), Settings.getOpt("gameTimeTextY"));

            // Time (in ms)
            this.remainingTime = 0.0;
            this.totalTime = 0.0;
            this.playTime = 0.0;

            // Configure callbacks
            collisionManager.addCallback(GameObject.Type.CAR, GameObject.Type.SCENEOBJECT, collisionCarScene, CollisionManager.CallbackType.DURING);
            collisionManager.addCallback(GameObject.Type.CAR, GameObject.Type.TIMEBONUS, collisionCarBonus, CollisionManager.CallbackType.BEGIN);
            collisionManager.addCallback(GameObject.Type.CAR, GameObject.Type.CHECKPOINT, collisionCarCheckPoint, CollisionManager.CallbackType.BEGIN);

            // Load shapes
            CollisionManager.initShapeCatalog(game.Content.RootDirectory);

            this.spriteBatch = new SpriteBatch(game.GraphicsDevice);
        }
Пример #4
0
        public override void unload()
        {
            // Remove objects from collision manager
            collisionManager.removeAllObjects();

            // Set every game element to null so we can save memory
            car = null;
            level = null;
            checkPoints.Clear();
            timeBonuses.Clear();
            sceneObjects.Clear();
            geometry.Clear();
            skyBox = null;
            terrain = null;

            font = null;
            retroFont = null;
            speedPanel = null;
            timePanel = null;

            MediaPlayer.Stop();
            song = null;
            carCrash = null;
            pickTime = null;

            // Clear input callbacks
            Input.clearCallbacks();

            loaded = false;
        }
Пример #5
0
        public override void load()
        {
            Log.log(Log.Type.INFO, "Loading state game type " + type);

            // Parse level and create game objects
            level = new Level(this, levelName);
            level.load();

            // Sort check point list
            checkPoints.Sort();

            // Create car with level data
            car = new Car(game, level.carPosition, level.carOrientation);

            // Add gameobjects to collision manager
            collisionManager.addObject(car);

            foreach (TimeBonus timeBonus in timeBonuses)
            {
                collisionManager.addObject(timeBonus);
            }

            foreach (CheckPoint checkPoint in checkPoints)
            {
                collisionManager.addObject(checkPoint);
            }

            foreach (GameObject sceneObject in sceneObjects)
            {
                collisionManager.addObject(sceneObject);
            }

            font = game.Content.Load<SpriteFont>("Fonts\\LED");
            retroFont = game.Content.Load<SpriteFont>("Fonts\\MenuBig");
            speedPanel = game.Content.Load<Texture2D>("Images\\speedPanel");
            timePanel = game.Content.Load<Texture2D>("Images\\speedPanel");

            // Time (in ms)
            this.remainingTime = totalTime * 1000.0;
            this.playTime = 0.0f;

            // Song
            song = game.Content.Load<Song>("Audio\\game");
            MediaPlayer.IsRepeating = true;
            MediaPlayer.Play(song);

            // Set input callbacks
            Input.setKeyPressedCallback(keyPressed);
            Input.setKeyReleasedCallback(keyReleased);

            game.camera.setInitialPosition(car);

            // Next checkpoint
            nextCheckPoint = 0;

            lapsDone = 1;

            loaded = true;
        }
Пример #6
0
        public void collisionCarScene(GameObject o1, GameObject o2)
        {
            // Get objects
            Car car;
            GameObject sceneObject;

            if (o1.type == GameObject.Type.CAR)
            {
                car = (Car)o1;
                sceneObject = o2;
            }
            else
            {
                car = (Car)o2;
                sceneObject = o1;
            }

            OrientedBox obb1 = (OrientedBox)car.shape;
            OrientedBox obb2 = (OrientedBox)sceneObject.shape;

            // Crash sfx
            game.audioEngine.SetGlobalVariable("crashVolume", car.velocity.Length() / car.maxSpeed);
            carCrash = game.soundBank.GetCue("crash");
            carCrash.Play();

            // Restore oldPos
            car.position = car.oldPos;

            // Get closest point
            Vector3 q = CollisionManager.closestPointToOBB(obb1.center + obb1.transform.Translation, obb2);

            // Get normal vector
            Vector3 normal = obb1.center + obb1.transform.Translation - q;
            normal.Normalize();

            // Change car velocity to bounce
            float angle = (float)Math.Atan2(normal.Y - car.velocity.Y, normal.X - car.velocity.X);
            Vector3 newDirection = new Vector3((float)System.Math.Sin(-angle), (float)System.Math.Cos(-angle), 0.0f);
            newDirection.Normalize();
            car.velocity = newDirection * car.velocity.Length() * 0.85f;

            // Get angle between normal and car direction
            Vector2 carDirection = new Vector2((float)System.Math.Sin(-car.angle), (float)System.Math.Cos(-car.angle));
            Vector2 normal2D = new Vector2(normal.X, normal.Y);

            float angleNormalCar = (float)Math.Atan2(normal2D.Y - carDirection.Y, normal2D.X - carDirection.X);
        }