Skip to content

Instantly share code, notes, and snippets.

@tuti107
Created April 10, 2016 18:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tuti107/d6ff9931ddc8caafdc4be5fd11882fec to your computer and use it in GitHub Desktop.
Save tuti107/d6ff9931ddc8caafdc4be5fd11882fec to your computer and use it in GitHub Desktop.
using UnityEngine;
using UnityEngine.UI;
using System.Collections;
using UnityEngine.SceneManagement;
public class Continue : MonoBehaviour {
public Image blackImage;
private Color blackImageColor;
private float pushedTime = 0f;
private Vector3 origPos;
// Use this for initialization
void Start () {
origPos = transform.position;
blackImageColor = blackImage.color;
}
// Update is called once per frame
void Update () {
if (pushedTime > 0f) {
float t = Time.time - pushedTime;
if (t < 0.5f) {
blackImageColor.a = 0f;
}
else if (t >= 0.5f && t <= 1.5f) {
blackImageColor.a = t - 0.5f;
} else if (t > 1.5f) {
blackImageColor.a = 1f;
pushedTime = 0f;
SceneManager.LoadScene("gameScene");
}
blackImage.color = blackImageColor;
}
}
public void Push() {
if (pushedTime == 0f) {
pushedTime = Time.time;
Vector3 newPos = new Vector3 (origPos.x, origPos.y-0.1f, origPos.z);
iTween.MoveTo(gameObject, iTween.Hash("position", newPos, "oncomplete", "PushAnimCompleted", "time", 0.5f));
}
}
public void PushAnimCompleted() {
iTween.MoveTo(gameObject, iTween.Hash("position", origPos, "time", 0.5f));
}
}
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class Gate : MonoBehaviour {
public int max = 3;
public float duration = 30f;
public float initial = 0f;
public float durationDelta = 3f;
public float durationMin = 10f;
public Player player;
public GameObject enemyPrefab;
public ParticleSystem particeSystem;
private float lastCreated;
private List<GameObject> enemies = new List<GameObject>();
// Use this for initialization
void Start () {
lastCreated = initial - duration + Time.time;
}
// Update is called once per frame
void Update () {
// --- 4/9 add start ---
if (player.isGameover) {
return;
}
// --- 4/9 add end ---
if ((Time.time - lastCreated > duration) && enemies.Count < max) {
lastCreated = Time.time;
duration -= durationDelta;
if (duration < durationMin)
duration = durationMin;
GameObject obj = GameObject.Instantiate (enemyPrefab);
obj.transform.parent = transform;
obj.transform.position = transform.position;
Skeleton skeleton = obj.GetComponent<Skeleton> ();
skeleton.gate = this;
skeleton.player = player;
particeSystem.Play ();
}
}
public void Remove(GameObject obj) {
enemies.Remove (obj);
GameObject.Destroy (obj);
}
}
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
public class Player : MonoBehaviour {
public Image damageImage;
private Color damageImageColor;
private float damagedTime = 0f;
// --- 4/9 add start ---
public Image gameOverImage;
public TextMesh gameOverText;
private Color gameOverColor;
private Color gameOverTextColor;
private float gameoverTime = 0f;
public bool isGameover = false;
public GameObject cont;
// --- 4/9 add end ---
// Use this for initialization
void Start () {
damageImageColor = damageImage.color;
ShowHP ();
// --- 4/9 add start ---
gameOverColor = gameOverImage.color;
gameOverTextColor = gameOverText.color;
cont.SetActive (false);
// --- 4/9 add end ---
}
// Update is called once per frame
void Update () {
if (damagedTime > 0f) {
float t = Time.time - damagedTime;
if (t <= 1f) {
damageImageColor.a = 1f - t;
}
else {
damagedTime = 0f;
damageImageColor.a = 0f;
}
damageImage.color = damageImageColor;
}
// --- 4/9 add start ---
if (gameoverTime > 0f) {
float t = Time.time - gameoverTime;
if (t <= 2f) {
gameOverColor.a = t / 2f;
} else if (t > 2f && t <= 3) {
isGameover = true;
cont.SetActive (true);
gameOverColor.a = 3f - t;
gameOverTextColor.a = (t - 2f);
} else {
gameOverColor.a = 0f;
gameOverTextColor.a = 1f;
}
gameOverImage.color = gameOverColor;
gameOverText.color = gameOverTextColor;
}
// --- 4/9 add end ---
}
public void Damage() {
if (damagedTime == 0f) {
damagedTime = Time.time;
// --- 4/9 add start ---
HP-=0.5f;
if (HP <= 0 && gameoverTime == 0f) {
gameoverTime = Time.time;
}
// --- 4/9 add end ---
ShowHP ();
}
}
// --- 4/9 add start ---
public GameObject lifePartsPrefab;
public GameObject life;
public float HP = 3;
private void ShowHP() {
if (life.transform.childCount == 0) {
for (int i = 0; i < HP; i++) {
GameObject lifeParts = Instantiate (lifePartsPrefab);
lifeParts.transform.parent = life.transform;
lifeParts.transform.localPosition = new Vector3 (i * 0.25f, 0, 0);
lifeParts.transform.name = (i+1).ToString ();
}
}
for (int i = 0; i < life.transform.childCount; i++) {
GameObject obj = life.transform.GetChild (i).gameObject;
float l = float.Parse (obj.name);
SpriteRenderer sprite = obj.GetComponent<SpriteRenderer> ();
if (l <= HP) {
sprite.color = Color.red;
} else {
float col = HP - (l - 1);
if (col < 0)
col = 0;
sprite.color = new Color (col, 0, 0);
}
}
}
// --- 4/9 add end ---
}
using UnityEngine;
using System.Collections;
public class Skeleton : MonoBehaviour {
// --- 3/27 modify start ---
//public GameObject player;
public Player player;
// --- 3/27 modify end ---
private Vector3 nextDirection = Vector3.zero;
private float nextDistance;
private Vector3 startPoint;
private float speed = 0.5f;
// --- 3/27 add start ---
private int HP = 3;
private bool damagingInvalid = false;
private bool damagedPlayer = false;
public ParticleSystem particle;
// --- 3/27 add end ---
// --- 4/2 add start ---
public Gate gate;
// --- 4/2 add end ---
private Animator anim;
// Use this for initialization
void Start () {
anim = GetComponent<Animator>();
// --- 4/2 add start ---
ChangeState ("Appear");
// --- 4/2 add end ---
}
// Update is called once per frame
void Update () {
// --- 4/9 add start ---
if (player.isGameover) {
gate.Remove (gameObject);
}
// --- 4/9 add end ---
if (IsStateChanging ())
return;
AnimatorStateInfo info = anim.GetCurrentAnimatorStateInfo (0);
// --- 4/2 add start ---
if (info.IsName ("Appear")) {
float t = info.normalizedTime;
if (t >= 1f) {
ChangeState ("Idle");
} else {
transform.position = new Vector3 (transform.position.x, -1.4f + t * 1.6f, transform.position.z);
}
}
else if (info.IsName ("Idle")) {
// --- 4/2 add end ---
// if (info.IsName ("Idle")) { // remove 4/2
// --- 3/27 add start ---
damagingInvalid = false;
damagedPlayer = false;
// --- 3/27 add end ---
if (player != null) {
if (nextDirection == Vector3.zero) {
float dist = Vector3.Distance (player.transform.position, transform.position);
float randAngle = dist < 3.0f ? 0f : (Random.value - 0.5f) * dist * 8;
nextDirection = Quaternion.LookRotation (player.transform.position - transform.position).eulerAngles;
nextDirection.y += randAngle;
if (nextDirection.y < 0)
nextDirection.y += 360;
nextDirection.x = 0;
nextDirection.z = 0;
nextDistance = dist < 3.0f ? dist : dist / 4 + Random.value * (dist / 4);
startPoint = transform.position;
} else {
transform.rotation = Quaternion.Slerp (transform.rotation, Quaternion.Euler (nextDirection), Time.deltaTime);
if (Mathf.Abs (Mathf.DeltaAngle (transform.rotation.eulerAngles.y, nextDirection.y)) <= 1.0f) {
ChangeState ("Walk");
}
}
}
} else if (info.IsName ("Walk")) {
float playerDist = Vector3.Distance (player.transform.position, transform.position);
if (playerDist <= 2) {
ChangeState ("Attack");
} else {
transform.position += transform.TransformDirection (Vector3.forward) * Time.deltaTime * speed;
float dist = Vector3.Distance (startPoint, transform.position);
if (dist >= nextDistance) {
nextDirection = Vector3.zero;
ChangeState ("Idle");
}
}
// 3/27 add start
} else if (info.IsName ("Attack")) {
float t = info.normalizedTime;
if (t > 0.35f && !damagedPlayer) {
damagedPlayer = true;
player.Damage ();
}
} else if (info.IsName ("Damage")) {
float t = info.normalizedTime;
if (HP <= 0f && t > 0.5f) {
ChangeState ("Dead");
} else {
transform.position += transform.TransformDirection (Vector3.back) * Time.deltaTime;
}
} else if (info.IsName ("Dead")) {
float t = info.normalizedTime;
if (t >= 2f) {
// --- 4/2 add start ---
if (gate != null) {
gate.Remove (gameObject);
}
// --- 4/2 add end ---
GameObject.Destroy (gameObject);
}
}
// 3/27 add end
}
private string changingStateName = null;
private void ChangeState(string name) {
changingStateName = name;
anim.SetTrigger (name);
}
private bool IsStateChanging() {
AnimatorStateInfo next = anim.GetNextAnimatorStateInfo (0);
bool ret = changingStateName != null && next.IsName (changingStateName);
if (changingStateName != null && !ret) {
changingStateName = null;
}
return ret;
}
// --- 3/27 add start ---
public void Damage() {
if (!damagingInvalid) {
HP -= 1;
damagingInvalid = true;
ChangeState ("Damage");
particle.Play ();
}
}
// --- 3/27 add end ---
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment