-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #9 from Prefab5/S1-#31-Score
S1 #31 Score
- Loading branch information
Showing
7 changed files
with
185 additions
and
57 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,66 +1,84 @@ | ||
using UnityEngine; | ||
/************************************************ | ||
* Definition: Methods for controlling the player. | ||
* | ||
* Created: ??/??/?? | ||
* **********************************************/ | ||
|
||
using UnityEngine; | ||
using System.Collections; | ||
|
||
public class PlayerController : MonoBehaviour { | ||
public class PlayerController : MonoBehaviour | ||
{ | ||
|
||
public ChunkController chunkController; | ||
|
||
private float jumpHeight = 500f; | ||
private float knockDownTime = 2f; | ||
private float timeKnockedDown = 0; | ||
private bool collision = false; | ||
|
||
public ChunkController chunkController; | ||
//For detecting grounded state to allow jumping. | ||
public Transform groundPoint; | ||
public float groundPointRadius; | ||
public LayerMask groundMask; | ||
bool isGrounded; | ||
|
||
private float jumpHeight = 500f; | ||
private float knockDownTime = 2f; | ||
private float timeKnockedDown = 0; | ||
private bool collision = false; | ||
Rigidbody2D rb2D; | ||
|
||
public Transform groundPoint; | ||
public float groundPointRadius; | ||
public LayerMask groundMask; | ||
//For controlling the score. | ||
public GameObject scoreObject; | ||
private ScoreController score; | ||
|
||
bool isGrounded; | ||
Rigidbody2D rb2D; | ||
|
||
void Start() | ||
{ | ||
rb2D = GetComponent<Rigidbody2D>(); | ||
} | ||
void Start () | ||
{ | ||
rb2D = GetComponent<Rigidbody2D> (); | ||
score = scoreObject.GetComponent<ScoreController> (); | ||
score.SetScoreRate (10); | ||
} | ||
|
||
void Update() | ||
{ | ||
//Checks to see if player is in contact with ground directly beneath them. | ||
isGrounded = Physics2D.OverlapCircle(groundPoint.position, groundPointRadius, groundMask); | ||
void Update () | ||
{ | ||
JumpControl (); | ||
|
||
//Jumping controls. | ||
if (Input.GetKeyDown(KeyCode.Space) && isGrounded) | ||
{ | ||
rb2D.AddForce(new Vector2(0, jumpHeight)); | ||
if (collision) { | ||
KnockDown (); | ||
} | ||
|
||
} | ||
} | ||
|
||
PlayerCollision(); | ||
void JumpControl () | ||
{ | ||
//Checks to see if player is in contact with ground directly beneath them. | ||
isGrounded = Physics2D.OverlapCircle (groundPoint.position, groundPointRadius, groundMask); | ||
|
||
} | ||
//Jumping controls. | ||
if (Input.GetKeyDown (KeyCode.Space) && isGrounded) { | ||
rb2D.AddForce (new Vector2 (0, jumpHeight)); | ||
|
||
void OnTriggerEnter2D(Collider2D other) | ||
{ | ||
} | ||
} | ||
|
||
void OnTriggerEnter2D (Collider2D other) | ||
{ | ||
|
||
if (other.gameObject.tag == "collision_obstacle") | ||
{ | ||
chunkController.PlayerCollision(); | ||
collision = true; | ||
} | ||
} | ||
|
||
void PlayerCollision() | ||
{ | ||
if (collision) | ||
{ | ||
timeKnockedDown += Time.deltaTime; | ||
|
||
if(timeKnockedDown > knockDownTime) | ||
{ | ||
timeKnockedDown = 0; | ||
collision = false; | ||
chunkController.GetComponent<ChunkController>().ResumeMovement(); | ||
} | ||
} | ||
} | ||
if (other.gameObject.tag == "collision_obstacle") { | ||
chunkController.PlayerCollision (); | ||
collision = true; | ||
score.PauseScore (); | ||
} | ||
} | ||
|
||
void KnockDown () | ||
{ | ||
timeKnockedDown += Time.deltaTime; | ||
|
||
if (timeKnockedDown > knockDownTime) { | ||
timeKnockedDown = 0; | ||
collision = false; | ||
chunkController.GetComponent<ChunkController> ().ResumeMovement (); | ||
score.ResumeScore (); | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
/******************************************************************** | ||
* Definition: Methods for controlling the score. | ||
* Requirements: PlayerController references the gameObject | ||
* containing this script. | ||
* | ||
* Created: 7/3/16 | ||
* ******************************************************************/ | ||
|
||
using UnityEngine; | ||
using System.Collections; | ||
|
||
public class ScoreController : MonoBehaviour | ||
{ | ||
|
||
public int scoreRate; | ||
private float score; | ||
private bool pause = false; | ||
|
||
void Start () | ||
{ | ||
|
||
} | ||
|
||
void Update () | ||
{ | ||
IncrementScore (); | ||
} | ||
|
||
void IncrementScore () | ||
{ | ||
if (scoreRate != null && !pause) { | ||
score += scoreRate * Time.deltaTime; | ||
UpdateScore (); | ||
} | ||
} | ||
|
||
void UpdateScore () | ||
{ | ||
gameObject.GetComponent<GUIText> ().text = "Score: " + Mathf.Round (score).ToString(); | ||
} | ||
|
||
public void PauseScore () | ||
{ | ||
pause = true; | ||
} | ||
|
||
public void ResumeScore () | ||
{ | ||
pause = false; | ||
} | ||
|
||
public void SetScoreRate (int RatePerSecond) | ||
{ | ||
scoreRate = RatePerSecond; | ||
} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters