public interface IDamageTest
{
public void Damage(int damage);
public void Death();
public void DecrementLife();
public void DecrementDed();
}
//playerに
using UnityEngine;
public class Player : MonoBehaviour
{
public GameObject player;
Rigidbody2D rbody;
float axisH = 0.0f;
// float axisV = 0.0f;
public float speed = 0.02f;
public float workspeed = 7.0f;
public FixedJoystick joystick; //InspectorからJoystickを取得
private Animator anim;
public BulletShot shotPrefab; // 弾のプレハブ
public float angle;
public Transform firePoint;
private Vector2 vecGravity;
void Start()
{
rbody = this.GetComponent();
anim = GetComponent();
}
void Update()
{
// axisH = Input.GetAxisRaw("Horizontal");
axisH = joystick.Horizontal;
// axisV = joystick.Vertical;
if (axisH == 0.0f)
{
anim.SetBool("del", false);
rbody.gravityScale = 1;
}
//向きの調整
if (axisH > 0.0f)
{
anim.SetBool("del", true);
transform.eulerAngles = new Vector3(0, 0, 0);
// back.transform.Translate(-0.001f, 0, 0);
// Debug.Log("右移動");
}
else if (axisH < 0.0f)
{
anim.SetBool("del", true);
transform.eulerAngles = new Vector3(0, 180, 0);
// back.transform.Translate(0.001f, 0, 0);
// Debug.Log("左移動");
}
}
public void StartPos()
{
ShootGo(transform);
}
public void ShootGo(Transform transform)
{
var pos = player.transform.localPosition; // プレイヤーの位置
var rot = player.transform.localRotation; // プレイヤーの向き
if (rot == Quaternion.Euler(0f, 0f, 0f))
{
var shot = Instantiate(shotPrefab, firePoint.position, rot);
shot.Init(angle, speed);
}
else if (rot == Quaternion.Euler(0f, -180f, 0f))
{
var shot = Instantiate(shotPrefab, firePoint.position, rot);
shot.Init(angle, -speed);
}
}
private void OnTriggerEnter2D(Collider2D coll)
{
var damagetest = coll.gameObject.GetComponent();
if (coll.gameObject.CompareTag("Fall"))
{
damagetest.Death();
}
}
void FixedUpdate()
{
//速度を更新する
rbody.linearVelocity = new Vector2(axisH * workspeed, rbody.linearVelocity.y);
}
}
////////////////////
using UnityEngine;
public class PlayerJump : MonoBehaviour
{
private Rigidbody2D rb;
public float gravityScale = 200;
public float jumpHeight = 2;
private int count = 2;
private int jumpCount = 0;
void Start()
{
rb = GetComponent();
}
public void Jump()
{
if (this.jumpCount < count)
{
float jumpForce = Mathf.Sqrt(jumpHeight * -1.9f * (Physics2D.gravity.y * rb.gravityScale));
rb.AddForce(new Vector2(0, jumpForce), ForceMode2D.Impulse);
jumpCount++;
}
}
void Update()
{
}
private void OnCollisionEnter2D(Collision2D other)
{
if (other.gameObject.CompareTag("Ground"))
{
jumpCount = 0;
}
}
}
////////////////
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.SceneManagement;
public class PlayerDed : MonoBehaviour,IDamageTest
{
private Vector3 objectPoint;
public int maxLife = 5;
public Image[] lifeImages;
private int currentLife;
void Start()
{
currentLife = maxLife;
}
private void UpdateLifeUI()
{
for (int i = 0; i < lifeImages.Length; i++)
{
if (i < currentLife)
{
lifeImages[i].enabled = true;
}
else
{
lifeImages[i].enabled = false;
}
}
}
public void DecrementLife()
{
currentLife--;
UpdateLifeUI();
gameObject.GetComponent().color=new Color32(255,0,0,230);
Invoke("back",0.2f);
if (currentLife <= 0)
{
gameObject.SetActive(false);
Invoke("GameRestart", 0.8f);
}
}
void back()
{
gameObject.GetComponent().color=new Color32(255,255,255,255);
}
public void GameRestart()
{
// gameObject.SetActive(false);
GameManager.Instance.GameRestart();
}
public void DecrementDed()
{
currentLife++;
UpdateLifeUI();
if (currentLife >= 0)
{
gameObject.SetActive(true);
}
}
public void Damage(int damage)
{
// Hp -= damage;
// Debug.Log("add: " + damage + "hp: " + currentLife);
}
public void Death()
{
// gameObject.SetActive(false);
// Debug.Log("Enemyを倒した");
}
private void OnTriggerEnter2D(Collider2D coll)
{
if (coll.gameObject.CompareTag("Crystal"))
{
DecrementDed();
coll.gameObject.SetActive(false);
}
if (coll.gameObject.CompareTag("Finish"))
{
SceneManager.LoadScene("NextOneScene");
}
if (coll.gameObject.CompareTag("Poll"))
{
SceneManager.LoadScene("MainScene");
}
}
}
/////////////////
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.SceneManagement;
public class ScoreController : MonoBehaviour
{
Rigidbody2D rb;
int gemCount = 0;
public Text Count;
public float waitTime = 1f;
void Start()
{
rb = GetComponent();
Count.text = "Score:" + gemCount.ToString();
}
private void FixedUpdate()
{
// rb.velocity = direction * Vector3.right * 3;
}
void OnTriggerEnter2D(Collider2D coll)
{
if (coll.gameObject.CompareTag("Move"))
{
GetComponent().material.color = Color.yellow;
Destroy(coll.gameObject);
// Destroy(gameObject);
UpCount();
if(gemCount > 30)
{
SceneManager.sceneLoaded += KeepScore;
SceneManager.LoadScene("NextOneScene");
}
}
}
void KeepScore(Scene next, LoadSceneMode mode)
{
var sm = GameObject.Find("Player").GetComponent();
// GameObject bullet = GameObject.FindWithTag("Bullet");
// var sm = bullet.GetComponent();
sm.gemCount = gemCount;
SceneManager.sceneLoaded -= KeepScore;
}
void UpCount()
{
gemCount += 10;
Count.text = "Score:" + gemCount.ToString();
}
}
//playerここまで
///////////////////////
//bulletに
using UnityEngine;
public class BulletShot : MonoBehaviour
{
private Vector3 m_velocity; // 速度
private void Update()
{
// 移動する
transform.localPosition += m_velocity;
}
// 弾を発射する時に初期化するための関数
public void Init(float angle, float speed)
{
// 弾の発射角度をベクトルに変換する
var direction = GetDirection(angle);
// 発射角度と速さから速度を求める
m_velocity = direction * speed;
// 弾が進行方向を向くようにする
var angles = transform.localEulerAngles;
angles.z = angle - 180;
transform.localEulerAngles = angles;
// 2 秒後に削除する
Destroy(gameObject, 2);
}
// 指定された角度( 0 ~ 360 )をベクトルに変換して返す
public static Vector3 GetDirection(float angle)
{
return new Vector3
(
Mathf.Cos(angle * Mathf.Deg2Rad),
Mathf.Sin(angle * Mathf.Deg2Rad),
0
);
}
private void OnTriggerEnter2D(Collider2D coll)
{
var damagetest = coll.gameObject.GetComponent();
if (coll.gameObject.CompareTag("Dead"))
{
Destroy(gameObject);
damagetest.Death();
}
}
}