Skip to content

Commit

Permalink
← 实现僵尸跑酷游戏的 UGUI 实践的脚本代码
Browse files Browse the repository at this point in the history
  • Loading branch information
Isayes committed Feb 8, 2017
1 parent 458b2c7 commit 80b6716
Show file tree
Hide file tree
Showing 11 changed files with 469 additions and 0 deletions.
67 changes: 67 additions & 0 deletions UGUI/002/Scripts/CameraFollow.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
using UnityEngine;
using System.Collections;

public class CameraFollow : MonoBehaviour
{
public float xMargin = 1f; // Distance in the x axis the player can move before the camera follows.
public float yMargin = 1f; // Distance in the y axis the player can move before the camera follows.
public float xSmooth = 8f; // How smoothly the camera catches up with it's target movement in the x axis.
public float ySmooth = 8f; // How smoothly the camera catches up with it's target movement in the y axis.
public Vector2 maxXAndY; // The maximum x and y coordinates the camera can have.
public Vector2 minXAndY; // The minimum x and y coordinates the camera can have.


private Transform player; // Reference to the player's transform.


void Awake ()
{
// Setting up the reference.
player = GameObject.FindGameObjectWithTag("Player").transform;
}


bool CheckXMargin()
{
// Returns true if the distance between the camera and the player in the x axis is greater than the x margin.
return Mathf.Abs(transform.position.x - player.position.x) > xMargin;
}


bool CheckYMargin()
{
// Returns true if the distance between the camera and the player in the y axis is greater than the y margin.
return Mathf.Abs(transform.position.y - player.position.y) > yMargin;
}


void FixedUpdate ()
{
TrackPlayer();
}


void TrackPlayer ()
{
// By default the target x and y coordinates of the camera are it's current x and y coordinates.
float targetX = transform.position.x;
float targetY = transform.position.y;

// If the player has moved beyond the x margin...
if(CheckXMargin())
// ... the target x coordinate should be a Lerp between the camera's current x position and the player's current x position.
targetX = Mathf.Lerp(transform.position.x, player.position.x, xSmooth * Time.deltaTime);

// If the player has moved beyond the y margin...
if(CheckYMargin())
// ... the target y coordinate should be a Lerp between the camera's current y position and the player's current y position.
targetY = Mathf.Lerp(transform.position.y, player.position.y, ySmooth * Time.deltaTime);

// The target x and y coordinates should not be larger than the maximum or smaller than the minimum.
targetX = Mathf.Clamp(targetX, minXAndY.x, maxXAndY.x);
targetY = Mathf.Clamp(targetY, minXAndY.y, maxXAndY.y);

// Set the camera's position to the target position with the same z component.
transform.position = new Vector3(targetX, targetY, transform.position.z);
}
}
22 changes: 22 additions & 0 deletions UGUI/002/Scripts/Coin.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
using UnityEngine;
using System.Collections;

public class Coin : MonoBehaviour
{
private CoinCounter coinCounter;

void Awake()
{
coinCounter = GameObject.FindGameObjectWithTag ("TextCoinsCount").GetComponent<CoinCounter> ();
}

void OnTriggerEnter2D(Collider2D other)
{
if(other.gameObject.tag == "Player")
print ("You picked up a coin!");

coinCounter.coinCount++;
gameObject.SetActive(false);
}

}
17 changes: 17 additions & 0 deletions UGUI/002/Scripts/CoinCounter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class CoinCounter : MonoBehaviour {

public int coinCount = 0;

void Start () {

}

void Update () {
GetComponent<Text>().text = coinCount.ToString ();
}
}
129 changes: 129 additions & 0 deletions UGUI/002/Scripts/Enemy.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
using UnityEngine;
using System.Collections;

public class Enemy : MonoBehaviour
{
public float moveSpeed = 2f; // The speed the enemy moves at.
public int HP = 2; // How many times the enemy can be hit before it dies.
//public Sprite deadEnemy; // A sprite of the enemy when it's dead.
//public Sprite damagedEnemy; // An optional sprite of the enemy when it's damaged.
//public AudioClip[] deathClips; // An array of audioclips that can play when the enemy dies.
//public GameObject hundredPointsUI; // A prefab of 100 that appears when the enemy dies.
public float deathSpinMin = -100f; // A value to give the minimum amount of Torque when dying
public float deathSpinMax = 100f; // A value to give the maximum amount of Torque when dying
public LayerMask collideMask;


private SpriteRenderer ren; // Reference to the sprite renderer.
private Transform frontCheck; // Reference to the position of the gameobject used for checking if something is in front.
private bool dead = false; // Whether or not the enemy is dead.
//private CoinCounter coinCounter; // Reference to the Score script.


void Awake()
{
// Setting up the references.
ren = transform.Find("Brain").GetComponent<SpriteRenderer>();
frontCheck = transform.Find("FrontCheck").transform;
//score = GameObject.Find("Score").GetComponent<Score>();
}

void FixedUpdate ()
{
// Create an array of all the colliders in front of the enemy.
Collider2D[] frontHits = Physics2D.OverlapPointAll(frontCheck.position, collideMask);

// Check each of the colliders.
foreach(Collider2D c in frontHits)
{
// If any of the colliders is an Obstacle...
if(c.tag == "Obstacle" || c.tag == "Enemy" || c.tag == "Player")
{
// ... Flip the enemy and stop checking the other colliders.
Flip ();
}

if(c.tag == "Player")
{
//Apply Damage to player
print ("I damaged the player!");
break;
}
}

// Set the enemy's velocity to moveSpeed in the x direction.
GetComponent<Rigidbody2D>().velocity = new Vector2(transform.localScale.x * moveSpeed, GetComponent<Rigidbody2D>().velocity.y);

// If the enemy has one hit point left and has a damagedEnemy sprite...
//if(HP == 1 && damagedEnemy != null)
// ... set the sprite renderer's sprite to be the damagedEnemy sprite.
//ren.sprite = damagedEnemy;

// If the enemy has zero or fewer hit points and isn't dead yet...
if(HP <= 0 && !dead)
// ... call the death function.
Death ();
}

public void Hurt()
{
// Reduce the number of hit points by one.
HP--;
}

void Death()
{
// Find all of the sprite renderers on this object and it's children.
SpriteRenderer[] otherRenderers = GetComponentsInChildren<SpriteRenderer>();

// Disable all of them sprite renderers.
foreach(SpriteRenderer s in otherRenderers)
{
s.enabled = false;
}

// Re-enable the main sprite renderer and set it's sprite to the deadEnemy sprite.
ren.enabled = true;
//ren.sprite = deadEnemy;

// Increase the score by 100 points
//score.score += 100;

// Set dead to true.
dead = true;

// Allow the enemy to rotate and spin it by adding a torque.
GetComponent<Rigidbody2D>().fixedAngle = false;
GetComponent<Rigidbody2D>().AddTorque(Random.Range(deathSpinMin,deathSpinMax));

// Find all of the colliders on the gameobject and set them all to be triggers.
Collider2D[] cols = GetComponents<Collider2D>();
foreach(Collider2D c in cols)
{
c.isTrigger = true;
}

// Play a random audioclip from the deathClips array.
//int i = Random.Range(0, deathClips.Length);
//AudioSource.PlayClipAtPoint(deathClips[i], transform.position);

// Create a vector that is just above the enemy.
//Vector3 scorePos;
//scorePos = transform.position;
//scorePos.y += 1.5f;

// Instantiate the 100 points prefab at this point.
//Instantiate(hundredPointsUI, scorePos, Quaternion.identity);
}


public void Flip()
{
print ("I should have flipped!");
// Multiply the x component of localScale by -1.
Vector3 enemyScale = transform.localScale;
enemyScale.x *= -1;
transform.localScale = enemyScale;
print (enemyScale);
}
}
44 changes: 44 additions & 0 deletions UGUI/002/Scripts/GameState.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
using UnityEngine;
using UnityEngine.UI;
using System.Collections;

public class GameState : MonoBehaviour
{
private GameObject[] coins;
public int totalCoins; // 所有的金币总数

public bool gameRunning = false;

private CoinCounter coinCounter;
private LivesCounter liveCounter;

void Awake ()
{
coinCounter = GameObject.FindGameObjectWithTag ("TextCoinsCount").GetComponent<CoinCounter> ();
liveCounter = GameObject.FindGameObjectWithTag ("TextLiveCount").GetComponent<LivesCounter> ();

coins = GameObject.FindGameObjectsWithTag("Coin");
totalCoins = coins.Length;
}

void Update ()
{
int collectedCoins;
collectedCoins = coinCounter.coinCount; // 当前收集到的金币数量
liveCounter.extraLives = collectedCoins / totalCoins;
if (liveCounter.totalLives < 0) {
print ("Game Over!");
}
}

public void StartGame()
{
gameRunning = true;
}

public void GameOver()
{
gameRunning = false;
print ("Game Over!");
}
}
17 changes: 17 additions & 0 deletions UGUI/002/Scripts/HudController.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class HudController : MonoBehaviour {

private Animator hudAnim;

void Awake(){
hudAnim = GetComponent<Animator> ();
}

public void HudFade(){
hudAnim.SetTrigger ("FadeIn");
}

}
26 changes: 26 additions & 0 deletions UGUI/002/Scripts/KillZone.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
using UnityEngine;
using System.Collections;

public class KillZone : MonoBehaviour {

//private LivesCounter livesCounter;
// Use this for initialization
void Start ()
{
//livesCounter = GameObject.Find("LivesCount").GetComponent<LivesCounter>();
}

// Update is called once per frame
void Update ()
{

}

void OnTriggerEnter2D(Collider2D other)
{
if(other.gameObject.tag == "Player")
print ("You died!");
//Update the GUI
//livesCounter.totalLives--;
}
}
28 changes: 28 additions & 0 deletions UGUI/002/Scripts/LivesCounter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
using UnityEngine;
using UnityEngine.UI;
using System.Collections;
[ExecuteInEditMode]

public class LivesCounter : MonoBehaviour
{
public int initialLives = 3; // 游戏刚开始的时候的生命数量
public int extraLives = 0; // 游戏进行时额外增加的生命数量
public int totalLives; // 主角生命数量的总数

void Start()
{
GetLives();
}

// Update is called once per frame
void Update ()
{
totalLives = initialLives + extraLives;
GetComponent<Text> ().text = totalLives.ToString ();
}

void GetLives()
{
totalLives = initialLives + extraLives;
}
}
17 changes: 17 additions & 0 deletions UGUI/002/Scripts/MainMenuController.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class MainMenuController : MonoBehaviour {

private Animator mainMenuAnim;

void Awake(){
mainMenuAnim = GetComponent<Animator> ();
}

public void MenuFade(){
mainMenuAnim.SetTrigger ("FadeOut");
}

}
Loading

0 comments on commit 80b6716

Please sign in to comment.