Skip to content

Instantly share code, notes, and snippets.

@bibinba bibinba/game_manager.cs
Last active Jul 10, 2019

Embed
What would you like to do?
uniRxを使ったトマト集めゲームのスクリプト(http://bibinbaleo.hatenablog.com/entry/2019/07/03/212152
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UniRx;
using System;
using UnityEngine.SceneManagement;
using UniRx.Triggers;
public class game_manager : MonoBehaviour
{
public Text timeText;
public Text pointText;
public Text endText;
public Button startButton;
public int nowTime = 15;
public float totalTime = 15f;
public float tomatoFrequently = 0.4f;
public AudioSource eatSound;
public GameObject tomato;
public GameObject EndPanel;
public GameObject textUI;
private float timeleft = 1.0f;
private bool isGameStart = false;
private int point = 0;
// Start is called before the first frame update
void Start()
{
pointText.text = point + "";
textUI.SetActive(false);
EndPanel.SetActive(false);
isGameStart = true;
tomatoDestroy.OnEat.Subscribe(_ => { Eat(); }).AddTo(this);
var disposable = new SingleAssignmentDisposable();
startButton.onClick.AsObservable().Subscribe((_) => {
textUI.SetActive(true);
disposable.Disposable = this.UpdateAsObservable().Subscribe(x =>
{
TomatoMake();
Timer();
if (isGameStart == false)
{
disposable.Dispose();
}
});
});
}
private void Eat()
{
eatSound.Play();
point++;
pointText.text = point + "";
}
private void Timer()
{
if (nowTime > 0)
{
totalTime -= Time.deltaTime;
nowTime = (int)totalTime;
timeText.text = nowTime.ToString();
}
else if (nowTime == 0)
{
GameEnd();
}
}
private void TomatoMake()
{
timeleft -= Time.deltaTime;
if (timeleft <= 0.0)
{
timeleft = tomatoFrequently;
Vector2 pos = new Vector2(UnityEngine.Random.Range(-3.0f, 3.0f), 4f);
Instantiate(tomato, pos, Quaternion.identity);
}
}
public void GameRestart()
{
SceneManager.LoadScene("MainScene");
}
private void GameEnd()
{
isGameStart = false;
EndPanel.SetActive(true);
endText.text = point + "";
textUI.SetActive(false);
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class packman : MonoBehaviour
{
public GameObject tomato;
Vector3 player_pos;
public float moving_distance=0.1f;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
PackmanMove();
}
private void PackmanMove()
{
player_pos = transform.position; //プレイヤーの位置を取得
player_pos.x = Mathf.Clamp(player_pos.x, -3f, 3f); //x位置が常に範囲内か監視
if (Input.GetKey(KeyCode.LeftArrow))
{
player_pos.x = player_pos.x - moving_distance;
}
if (Input.GetKey(KeyCode.RightArrow))
{
player_pos.x = player_pos.x + moving_distance;
}
transform.position = new Vector2(player_pos.x, player_pos.y);
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UniRx;
using System;
public class tomatoDestroy : MonoBehaviour
{
private static Subject<Unit> m_OnEat = new Subject<Unit>();
public static IObservable<Unit> OnEat { get { return m_OnEat; } }
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
if (!GetComponent<Renderer>().isVisible)
{
Destroy(this.gameObject);
}
}
void OnTriggerEnter2D(Collider2D other)
{
if (other.tag == "Player")
{
Destroy(this.gameObject);
}
m_OnEat.OnNext(Unit.Default);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.