using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class MainCtrl : MonoBehaviour {
private static Vector3 velocity_out = new Vector3(30.0f, 0.0f, 0.0f);
private static Vector3 velocity_in = new Vector3(-30.0f, 0.0f, 0.0f);
public static Text USER_MONEY_LB;
public GameObject dices;
public GameObject disk;
public GameObject bowl;
public Text gold_text;
private float shake_time = 0.25f;//1 s
public float speed = 1.2f;
public float shake_speed = 50;
public float amount = 0.2f;
private Vector3 bowl_close_pos = new Vector3(0, 0.6f, -1);
private Vector3 bowl_open_pos = new Vector3(15,0.6f,-1);
private Vector3 bowl_shake_pos ;
private static int bowl_stt = 1; //0 close 1 open 2 move out 3 move in 5 shake
// Use this for initialization
private GameObject dice1;
private GameObject dice2;
private GameObject dice3;
private int[] dice_rs = { 0,0,0};
private List list_dice_price = new List();
public static GameObject canvas;
public static GameObject MONEY_NOTIFY;
void Start () {
if (MONEY_NOTIFY == null)
{
MONEY_NOTIFY = Resources.Load("Prefabs/wingold_lb", typeof(GameObject)) as GameObject;
}
if (canvas == null)
{
canvas = GameObject.Find("Canvas") ;
}
bowl_shake_pos = disk.transform.position;
dice1 = GameObject.Find("dices/dice1");
dice2 = GameObject.Find("dices/dice2");
dice3 = GameObject.Find("dices/dice3");
Sprite temp = Resources.Load("Board/naiCube");
list_dice_price.Add(temp);
temp = Resources.Load("Board/bauCube");
list_dice_price.Add(temp);
temp = Resources.Load("Board/gaCube");
list_dice_price.Add(temp);
temp = Resources.Load("Board/caCube");
list_dice_price.Add(temp);
temp = Resources.Load("Board/cuaCube");
list_dice_price.Add(temp);
temp = Resources.Load("Board/tomCube");
list_dice_price.Add(temp);
USER_MONEY_LB = gold_text;
if (PlayerPrefs.HasKey("user_money"))
MoneyCtrl.user_money = PlayerPrefs.GetInt("user_money");
updateMoney(0);
startGame();
}
public static bool updateMoney(int amount)
{
if(amount < 0 && MoneyCtrl.user_money < Mathf.Abs(amount))
{
return false;
}
MoneyCtrl.user_money += amount;
USER_MONEY_LB.text = "$" + MoneyCtrl.user_money;
PlayerPrefs.SetInt("user_money", MoneyCtrl.user_money);
return true;
}
void FixedUpdate()
{
if (bowl_stt == 3)
{
float step = speed * Time.deltaTime;
bowl.transform.Translate(Vector3.left * speed);
if (bowl.transform.position.x <= bowl_close_pos.x)
{
Vector3 c_pos = bowl.transform.position;
bowl.transform.position = new Vector3(bowl_close_pos.x, c_pos.y, c_pos.z);
bowl_stt = 5;
}
}
if (bowl_stt == 2)
{
float step = speed * Time.deltaTime;
bowl.transform.Translate(Vector3.right * speed);
if (bowl.transform.position.x >= bowl_open_pos.x)
{
Vector3 c_pos = bowl.transform.position;
bowl.transform.position = new Vector3(bowl_open_pos.x, c_pos.y, c_pos.z);
bowl_stt = 1;
computeResult();
}
}
}
// Update is called once per frame
void Update () {
if (bowl_stt == 5)
{
shake_time -= Time.deltaTime;
if (shake_time <= 0)
{
bowl_stt = 0;
shake_time = 0.25f;
bowl_shake_pos.x = 0;
}
else
{
bowl_shake_pos.x = Mathf.Sin(Time.time * shake_speed) * amount;
}
disk.transform.position = bowl_shake_pos;
}
}
public void playClick()
{
}
private void computeResult()
{
BoardCtrl.removeAllMoneys();
int money = 0;
foreach(int rs in dice_rs){
money += BoardCtrl.use_choose[rs];
BoardCtrl.use_choose[rs] = 0;
}
money = money * 2;
updateMoney(money);
for (int i = 0; i < 6; i++)
{
BoardCtrl.use_choose[i] = 0;
}
if (money > 0)
{
GameObject instance = Instantiate(MONEY_NOTIFY, canvas.transform) as GameObject;
Text lb = instance.GetComponent();
lb.text = "+" + money;
AudioCtrl.playWin();
}
else
{
AudioCtrl.playLose();
}
}
private void randomDice()
{
int rs = Random.Range(0, 6);
SpriteRenderer spriteR = dice1.GetComponent();
spriteR.sprite = list_dice_price[rs];
dice_rs[0] = rs;
rs = Random.Range(0, 6);
spriteR = dice2.GetComponent();
spriteR.sprite = list_dice_price[rs];
dice_rs[1] = rs;
rs = Random.Range(0, 6);
spriteR = dice3.GetComponent();
spriteR.sprite = list_dice_price[rs];
dice_rs[2] = rs;
}
void OnMouseUpAsButton()
{
if (bowl_stt == 0)// close -> open
{
randomDice();
bowl_stt = 2;
}
else if (bowl_stt == 1)
{
bowl_stt = 3;
}
}
public static void startGame()
{
if(bowl_stt == 1)
bowl_stt = 3;
}
}