//-----------------------------------------------------------------------------
// BasePlayer.cs
//
// Microsoft XNA Community Game Platform
// Copyright (C) Microsoft Corporation. All rights reserved.
//-----------------------------------------------------------------------------
using System;
using System.Collections.Generic;
using System.Text;
using RacingGame.GameScreens;
using RacingGame.Graphics;
using RacingGame.Helpers;
using RacingGame.Landscapes;
using RacingGame.Properties;
using RacingGame.Sounds;
using RacingGame.Tracks;
namespace RacingGame.GameLogic
{
///
/// Base player helper class, holds all the current game values:
/// Game time, game over, level number, victory.
/// More stuff that has impact to the CarController or ChaseCamera classes
/// should be included here, else add them directly to the Player class,
/// which handles all the game logic.
/// For example adding items or powerups should be done in this class
/// if they affect the speed or physics of our car.
/// For multiplayer purposes this class should be extended to assign
/// a player id to each player and link the network stuff up here.
///
public class BasePlayer
{
///
/// Current game time in ms. Used for time display in game. Also used to
/// update the sun position and for the highscores.
/// Will be stopped if we die or if we are still zooming in.
///
protected float currentGameTimeMilliseconds = 0;
///
/// Current lap. Increases and when we reach 3, the game is won.
///
protected int lap;
///
/// Current lap
///
public int CurrentLap
{
get
{
return lap;
}
}
///
/// Remember best lap time, unused until we complete the first lap.
/// Then it is set every lap, always using the best and fastest lap time.
///
private float bestLapTimeMilliseconds = 0;
///
/// Best lap time we have archived in this game
///
public float BestTimeMilliseconds
{
get
{
return bestLapTimeMilliseconds;
}
}
///
/// Start new lap, will reset all lap variables and the game time.
/// If all laps are done the game is over.
///
protected void StartNewLap()
{
lap++;
RacingGameManager.Landscape.StartNewLap();
// Got new best time?
if (bestLapTimeMilliseconds == 0 ||
currentGameTimeMilliseconds < bestLapTimeMilliseconds)
bestLapTimeMilliseconds = currentGameTimeMilliseconds;
// Start at 0:00.00 again
currentGameTimeMilliseconds = zoomInTime;
}
///
/// Game time ms, will return negative values if currently zooming in!
///
/// Int
public float GameTimeMilliseconds
{
get
{
return currentGameTimeMilliseconds - zoomInTime;
}
}
///
/// How long do we zoom in.
///
public const int StartGameZoomTimeMilliseconds = 5000;
///
/// Zoom in time
///
private float zoomInTime = StartGameZoomTimeMilliseconds;
///
/// Zoom in time
///
/// Float
protected float ZoomInTime
{
get
{
return zoomInTime;
}
set
{
zoomInTime = value;
}
}
///
/// The amount of time to remain fully zoomed in waiting for start light;
///
public const int StartGameZoomedInTime = 3000;
///
/// Won or lost?
///
protected bool victory;
///
/// Property for Victory
///
public bool Victory
{
get
{
return victory;
}
}
///
/// Level num, set when starting game!
///
protected int levelNum;
public int LevelNum
{
get
{
return levelNum;
}
}
///
/// Game over?
///
protected bool isGameOver;
///
/// Is game over?
///
/// Bool
public bool GameOver
{
get
{
return isGameOver;
}
}
///
/// Did the player win the game? Makes only sense if GameOver is true!
///
public bool WonGame
{
get
{
return victory;
}
}
///
/// Remember if we already uploaded our highscore for this game.
/// Don't do this twice (e.g. when pressing esc).
///
private bool alreadyUploadedHighscore = false;
///
/// Set game over and upload highscore
///
public void SetGameOverAndUploadHighscore()
{
// Set gameOver to true to mark this game as ended.
isGameOver = true;
// Upload highscore
if (alreadyUploadedHighscore == false)
{
alreadyUploadedHighscore = true;
Highscores.SubmitHighscore(levelNum,
(int)currentGameTimeMilliseconds);
}
}
///
/// Helper to determinate if user can control the car.
/// If game just started we still zoom into the chase camera.
///
/// Bool
public bool CanControlCar
{
get
{
return zoomInTime <= 0 &&
GameOver == false;
}
}
private bool firstFrame = true;
///
/// Reset all player entries for restarting a game.
/// In derived classes reset all the variables we need to reset for
/// a new game there (e.g. car speed in CarController or
/// cameraWobbel in ChaseCamera).
///
public virtual void Reset()
{
levelNum = TrackSelection.SelectedTrackNumber;
isGameOver = false;
alreadyUploadedHighscore = false;
currentGameTimeMilliseconds = 0;
bestLapTimeMilliseconds = 0;
lap = 0;
victory = false;
zoomInTime = StartGameZoomTimeMilliseconds;
firstFrame = true;
}
///
/// Clear variables for game over
///
public virtual void ClearVariablesForGameOver()
{
}
///
/// Update game logic, called every frame. In Rocket Commander we did
/// all the game logic in one big method inside the player class, but it
/// was hard to add new game logic and many small things were also in
/// the GameAsteroidManager. For this game we split everything up into
/// much more classes and every class handles only its own variables.
/// For example this class just handles the game time and zoom in time,
/// for the car speed and physics just go into the CarController class.
///
public virtual void Update()
{
// Since there is no loading screen, we need to skip the first frame because
// the loading will cause ElapsedTimeThisFrameInMilliseconds to be too high
if (firstFrame)
{
firstFrame = false;
return;
}
// Handle zoomInTime at the beginning of a game
if (!RacingGameManager.InMenu && zoomInTime > 0)
{
float lastZoomInTime = zoomInTime;
zoomInTime -= BaseGame.ElapsedTimeThisFrameInMilliseconds;
if (zoomInTime < 2000 &&
(int)((lastZoomInTime + 1000) / 1000) != (int)((zoomInTime + 1000) / 1000))
{
// Handle start traffic light object (red, yellow, green!)
RacingGameManager.Landscape.ReplaceStartLightObject(
2 - (int)((zoomInTime + 1000) / 1000));
}
}
// Don't handle any more game logic if game is over or still zooming in.
if (CanControlCar == false)
return;
// Increase game time
currentGameTimeMilliseconds += BaseGame.ElapsedTimeThisFrameInMilliseconds;
}
}
}