#pragma strict
public enum GameState { playing, gameover };
public var platformPrefab : Transform;
public static var gameState:GameState;
private var playerTrans:Transform ;
private var platformsSpawnedUpTo :float = 0;
private var platforms: ArrayList ;
private var nextPlatformCheck:float = 0.0f;
function Awake () {
playerTrans = GameObject.FindGameObjectWithTag("Player").transform;
platforms = new ArrayList();
SpawnPlatforms(25.0);
StartGame();
}
function Start ()
{
Time.timeScale = 1.0f;
gameState = GameState.playing;
}
function StartGame()
{
Time.timeScale = 1.0f;
gameState = GameState.playing;
}
function GameOver()
{
Time.timeScale = 0.0f; //Pause the game
gameState = GameState.gameover;
GameGUI.SP.CheckHighscore();
}
function Update () {
//Do we need to spawn new platforms yet? (we do this every X meters we climb)
var playerHeight:float = playerTrans.position.y;
if (playerHeight > nextPlatformCheck)
{
PlatformMaintenaince(); //Spawn new platforms
}
//Update camera position if the player has climbed and if the player is too low: Set gameover.
var currentCameraHeight:float = transform.position.y;
var newHeight:float = Mathf.Lerp(currentCameraHeight, playerHeight, Time.deltaTime * 10);
if (playerTrans.position.y > currentCameraHeight)
{
transform.position = new Vector3(transform.position.x, newHeight, transform.position.z);
}else{
//Player is lower..maybe below the cameras view?
if (playerHeight < (currentCameraHeight - 10))
{
GameOver();
}
}
//Have we reached a new score yet?
if (playerHeight > GameGUI.score)
{
GameGUI.score = playerHeight;
}
}
function PlatformMaintenaince()
{
nextPlatformCheck = playerTrans.position.y + 10;
//Delete all platforms below us (save performance)
for(var i:int = platforms.Count-1;i>=0;i--)
{
var plat:Transform = platforms[i];
if (plat.position.y < (transform.position.y - 10))
{
Destroy(plat.gameObject);
platforms.RemoveAt(i);
}
}
//Spawn new platforms, 25 units in advance
SpawnPlatforms(nextPlatformCheck + 25);
}
function SpawnPlatforms(upTo:float )
{
var spawnHeight:float = platformsSpawnedUpTo;
while (spawnHeight <= upTo)
{
var x:float = Random.Range(-10.0f, 10.0f);
var pos :Vector3 = new Vector3(x, spawnHeight, 12.0f);
var plat:Transform = Instantiate(platformPrefab, pos, Quaternion.identity);
platforms.Add(plat);
spawnHeight += Random.Range(1.6f, 3.5f);
}
platformsSpawnedUpTo = upTo;
}
댓글 없음:
댓글 쓰기