Create a Scoring/Game information Data Structure that uses the Singleton Design
ID: 3743453 • Letter: C
Question
Create a Scoring/Game information Data Structure that uses the Singleton Design Pattern.The data structure should store at least three types of information if not more:
Score,
Main character health,
Money,
and so on.
Your singleton will need to have public properties so that you can update the information.
You will need a scene in your project to show your singleton works.
The easiest way to do this is to use the UI components of Unity3D.
This project has to work in Unity3D.
Your Singleton class will NOT extend the MonoBehaviour class.
All code will be done in C#.
Explanation / Answer
using UnityEngine;
using UnityEngine.EventSystems;
using System.Collections;
using System.Collections.Generic;
// this is player instance appy this script to the 2d sprite player with 2d collider and 2d rigidbody
public class Player : MonoBehaviour {
[HideInInspector]
public bool facingUp = true; // For determining which way the player is currently facing.
public static Player instance;
public float speed; //Speed of player
public AudioClip deadSound; //Sound When player dead
public AudioClip coinSound; //Coin Picker sound
private Vector3 direction; //Direction of player
private int fingerId = -1; //For every non-mobile Platform
private bool isDead; //For check status of Player
public List<Sprite> playerSprite = new List<Sprite>();
float timer = 5.0f;
void Awake(){
instance = this;
if (Application.isMobilePlatform) {
fingerId = 0; //for mobile and unity
}
transform.GetComponent<SpriteRenderer>().sprite = playerSprite[Random.Range(0,playerSprite.Length)];
direction = Vector3.up; //When starting the game. Rabbit will go to the top first.
}
// Update is called once per frame
void Update () {
//When click and Player alive and not paused.
if (Input.GetMouseButtonDown (0) && !isDead && Time.timeScale != 0) {
//For Block click through UI
if(EventSystem.current.IsPointerOverGameObject(fingerId)){
return; //Not Change Direction
}
ChangeDirection (); //Change Direction
}
transform.Translate (direction * speed * Time.deltaTime);
}
void ChangeDirection(){
if (direction == Vector3.down) {
direction = Vector3.up; //Change direction to down
} else {
direction = Vector3.down; //Change direction to up
}
Flip ();
}
void Flip ()
{
// Switch the way the player is labelled as facing.
facingUp = !facingUp;
// Multiply the player's x local scale by -1.
Vector3 theScale = transform.localScale;
theScale.y *= -1;
transform.localScale = theScale;
}
void OnCollisionEnter2D(Collision2D coll){
if (coll.transform.tag == "myrope")
{
ChangeDirection (); //Change direction when it collides with the rope.
}
//When Player collide with Obstacle while alive.
if (coll.transform.tag == "Obstacle" && !isDead) {
//Debug.Log(coll.collider.name);
isDead = true;
SoundManager.instance.PlaySingle(deadSound); //Play Dead sound
GameController.instance.GameOver(); //Call Gameover
}
}
void OnTriggerEnter2D(Collider2D coll){
//When Player trigger with Coin while alive. && !isDead
if (coll.transform.tag == "Coin") {
Destroy(coll.gameObject); //Destroy Coin
SoundManager.instance.PlaySingle(coinSound); //Play coin picker sound
GameController.instance.addScore(); //Add score
CoinSpawner.instance.SpawnCoin(); //Spawn a new coin
}
}
}
================Coin spawner instance ====================
using UnityEngine;
using System.Collections;
public class CoinSpawner : MonoBehaviour {
public GameObject coin; //Coin object
public float yPosition; //Position for spawn
private bool isTop; //For check last position of coin
public static CoinSpawner instance;
// Use this for initialization
void Awake () {
instance = this;
}
void Start(){
SpawnCoin (); //Spawn coin to top when start
}
public void SpawnCoin(){
GameObject c = Instantiate (coin) as GameObject; //Spawn Coin
c.name = "Coin";
c.transform.tag = "Coin";
if (isTop) {
c.transform.position = new Vector2 (0.0f, -yPosition); //Spawn down Coin
} else {
c.transform.position = new Vector2 (0.0f, yPosition); //Spawn Top Coin
}
isTop = !isTop;
}
}
========================================
After that put two rope with tag "myrope" in the scene with collider and spring joints at one position
(0,1,0) and (0,-1,0)
=========================================
- make a button to start game and start timer as appropriate time , ex 60 sec or 100 sec. after that how many coins pllayer collected you can count
======================================
for timer ===
using UnityEngine;
using System.Collections;
public class ObstacleSpawner : MonoBehaviour {
public text Timer;
public float timer;
void Update()
{
int Min1 = Mathf.FloorToInt (timer / 60F);
int Sec1 = Mathf.FloorToInt (timer - Min1 * 60);
timer -= Time.deltaTime;
Timer.Text = Sec1.tostring("00");
}}
This is a simple game to collect coins at given time.
I hope this will help you.