code ソーシャル

Unityアプリ作成Ver.2(4)

スポンサーリンク

だいぶアプリっぽくなってきました。

ちなみに、最近読んだ本のおすすめは、劉 慈欣の「三体」で、中国語から英訳され、さらに和訳されたベストセラー本です。

興味をもったのは、SFでありながら中国人と政治の関わりについても描かれているところで、中国の方にとって政治とは死活問題で、その背景には、昔から長きに渡り続いた匈奴との戦いや元による侵略統治などがあるのではないかと思いを巡らせました。

まだ和訳版は完結しておらず、三部作の二部まで刊行されています。

 

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

using DG.Tweening;

public class PlayerManager : MonoBehaviour
{
public GameObject gameManager;

public LayerMask blockLayer;
private Rigidbody2D rbody;
private Animator animator;

private const float MOVE_SPEED = 3;
private float moveSpeed;

private float jumpPower = 400;
private bool goJump = false;
private bool canJump = false;
private bool usingButtons = false;

public enum MOVE_DIR
{
STOP,
LEFT,
RIGHT,
};

private MOVE_DIR moveDirection = MOVE_DIR.STOP;

public AudioClip jumpSE;
public AudioClip getSE;
public AudioClip stampSE;

private AudioSource audioSource;

// Start is called before the first frame update
void Start()
{
audioSource = gameManager.GetComponent<AudioSource>();
rbody = GetComponent<Rigidbody2D>();
animator = GetComponent<Animator>();
}

void Update()
{
canJump =
Physics2D.Linecast(transform.position - (transform.right * 0.3f),
transform.position - (transform.up * 0.1f), blockLayer) ||
Physics2D.Linecast(transform.position + (transform.right * 0.3f),
transform.position - (transform.up * 0.1f), blockLayer);

animator.SetBool("onGround", canJump);

if (!usingButtons)
{
float x = Input.GetAxisRaw("Horizontal");

if (x == 0)
{
moveDirection = MOVE_DIR.STOP;
}
else
{
if (x < 0)
{
moveDirection = MOVE_DIR.LEFT;
}
else
{
moveDirection = MOVE_DIR.RIGHT;
}
}
if (Input.GetKeyDown("space"))
{
PushJumpButton();
}
}

}

// Update is called once per frame
void FixedUpdate()
{
switch (moveDirection)
{
case MOVE_DIR.STOP:
moveSpeed = 0;
break;
case MOVE_DIR.LEFT:
moveSpeed = MOVE_SPEED * -1;
transform.localScale = new Vector2(-1, 1);
break;
case MOVE_DIR.RIGHT:
moveSpeed = MOVE_SPEED;
transform.localScale = new Vector2(1, 1);
break;
}
rbody.velocity = new Vector2(moveSpeed, rbody.velocity.y);

if (goJump)
{
audioSource.PlayOneShot(jumpSE);
rbody.AddForce(Vector2.up * jumpPower);
goJump = false;
}
}

public void PushLeftButton()
{
moveDirection = MOVE_DIR.LEFT;
usingButtons = true;
}
public void PushRightButton()
{
moveDirection = MOVE_DIR.RIGHT;
usingButtons = true;
}
public void ReleaseMoveButton()
{
moveDirection = MOVE_DIR.STOP;
usingButtons = true;
}

public void PushJumpButton()
{
if (canJump)
{
goJump = true;
}
}
void OnTriggerEnter2D(Collider2D col)
{
if (gameManager.GetComponent<GameManager>().gameMode != GameManager.GAME_MODE.PLAY)
{
return;
}

if (col.gameObject.tag == "Trap")
{
gameManager.GetComponent<GameManager>().GameOver();
DestroyPlayer();
}
if (col.gameObject.tag == "Goal")
{
gameManager.GetComponent<GameManager>().GameClear();
}
if (col.gameObject.tag == "Enemy")
{
if (transform.position.y > col.gameObject.transform.position.y + 0.4f)
{
audioSource.PlayOneShot(stampSE);
rbody.velocity = new Vector2(rbody.velocity.x, 0);
rbody.AddForce(Vector2.up * jumpPower);
col.gameObject.GetComponent<EnemyManager>().DestroyEnemy();
}
else
{
gameManager.GetComponent<GameManager>().GameOver();
DestroyPlayer();
}
}
if (col.gameObject.tag == "Orb")
{
audioSource.PlayOneShot(getSE);
col.gameObject.GetComponent<OrbManager>().GetOrb();
}

}
void DestroyPlayer()
{
gameManager.GetComponent<GameManager>().gameMode = GameManager.GAME_MODE.GAMEOVER;

CircleCollider2D circleCollider = GetComponent<CircleCollider2D>();
BoxCollider2D boxCollider = GetComponent<BoxCollider2D>();
Destroy(circleCollider);
Destroy(boxCollider);

Sequence animSet = DOTween.Sequence();
animSet.Append(transform.DOLocalMoveY(1.0f, 0.2f).SetRelative());
animSet.Append(transform.DOLocalMoveY(-10.0f, 1.0f).SetRelative());

Destroy(this.gameObject, 1.2f);
}

}

 

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

using UnityEngine.UI;

public class GameManager : MonoBehaviour
{
private const int MAX_SCORE = 999999;

public GameObject textGameOver;
public GameObject textClear;
public GameObject buttons;
public GameObject textScoreNumber;

public enum GAME_MODE
{
PLAY,
CLEAR,
GAMEOVER,

};
public GAME_MODE gameMode = GAME_MODE.PLAY;

private int score = 0;
private int displayScore = 0;

public AudioClip clearSE;
public AudioClip gameoverSE;

private AudioSource audioSource;

// Start is called before the first frame update
void Start()
{
audioSource = this.gameObject.GetComponent<AudioSource>();
RefreshScore();
}

// Update is called once per frame
void Update()
{
if(score > displayScore)
{
displayScore += 10;

if(displayScore > score)
{
displayScore = score;
}

RefreshScore();
}
}

public void GameOver()
{
audioSource.PlayOneShot(gameoverSE);
textGameOver.SetActive(true);
buttons.SetActive(false);
}
public void GameClear()
{
audioSource.PlayOneShot(clearSE);
gameMode = GAME_MODE.CLEAR;
textClear.SetActive(true);
buttons.SetActive(false);
}
public void AddScore(int val)
{
score += val;
if(score > MAX_SCORE)
{
score = MAX_SCORE;
}

}
void RefreshScore()
{
textScoreNumber.GetComponent<Text>().text = displayScore.ToString();
}
}

 

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

using DG.Tweening;

public class EnemyManager : MonoBehaviour
{
private const int ENEMY_POINT = 50;
private GameObject gameManager;

public LayerMask blockLayer;
private Rigidbody2D rbody;
private float moveSpeed = 1;
public enum MOVE_DIR
{
LEFT,
RIGHT,
};
private MOVE_DIR moveDirection = MOVE_DIR.LEFT;

// Start is called before the first frame update
void Start()
{
rbody = GetComponent<Rigidbody2D>();

gameManager = GameObject.Find("GameManager");
}

// Update is called once per frame
void Update()
{

}
private void FixedUpdate()
{
bool isBlock;

switch (moveDirection)
{
case MOVE_DIR.LEFT:
rbody.velocity = new Vector2(moveSpeed * -1, rbody.velocity.y);
transform.localScale = new Vector2(1, 1);

isBlock = Physics2D.Linecast(
new Vector2(transform.position.x, transform.position.y + 0.5f),
new Vector2(transform.position.x - 0.3f, transform.position.y + 0.5f),
blockLayer);

if (isBlock)
{
moveDirection = MOVE_DIR.RIGHT;
}
break;
case MOVE_DIR.RIGHT:
rbody.velocity = new Vector2(moveSpeed, rbody.velocity.y);
transform.localScale = new Vector2(-1, 1);

isBlock = Physics2D.Linecast(
new Vector2(transform.position.x, transform.position.y + 0.5f),
new Vector2(transform.position.x + 0.3f, transform.position.y + 0.5f),
blockLayer);
if (isBlock)
{
moveDirection = MOVE_DIR.LEFT;
}
break;
}
}

public void DestroyEnemy()
{
gameManager.GetComponent<GameManager>().AddScore(ENEMY_POINT);

rbody.velocity = new Vector2(0, 0);

CircleCollider2D circleCollider = GetComponent<CircleCollider2D>();
BoxCollider2D boxCollider = GetComponent<BoxCollider2D>();
Destroy(circleCollider);
Destroy(boxCollider);

Sequence animSet = DOTween.Sequence();
animSet.Append(transform.DOLocalMoveY(0.5f, 0.2f).SetRelative());
animSet.Append(transform.DOLocalMoveY(-10.0f, 1.0f).SetRelative());

Destroy(this.gameObject,1.2f);
}
}

 

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

using DG.Tweening;

public class OrbManager : MonoBehaviour
{
private const int ORB_POINT = 100;

private GameObject gameManager;

// Start is called before the first frame update
void Start()
{
gameManager = GameObject.Find("GameManager");
}

// Update is called once per frame
void Update()
{

}
public void GetOrb()
{
gameManager.GetComponent<GameManager>().AddScore(ORB_POINT);

CircleCollider2D circleCollider = GetComponent<CircleCollider2D>();
Destroy(circleCollider);

transform.DOScale(2.5f, 0.3f);
SpriteRenderer spriteRenderer = transform.GetComponent<SpriteRenderer>();
DOTween.ToAlpha(() => spriteRenderer.color, a => spriteRenderer.color = a, 0.0f, 0.3f);

Destroy(this.gameObject,0.5f);
}
}

人気の記事

1

皆さん、ついに、エアラインでも、サブスクリプションが始まったのはご存じですか? まだ実験段階ですが、ANAが、定額全国住み放題サービスを提供する「ADDress」と組んで、国内線を4回まで定額利用可能 ...

2

無料でネットショップを開けるアプリとして多くの人に驚きを与えたBASE株式会社が、2019年10月25日東証マザーズに上場しました。2020年2月時点で90万店を超えるショップを抱えるまでに成長してい ...

3

2011年にサービスを開始してから圧倒的な成長率を誇るインテリア通販サイト 【FLYMEe/フライミー】を皆さんご存じでしょうか。 「自分のイメージするインテリア、本当に欲しいインテリアがどこにあるの ...

4

ついに、noteの月間アクティブユーザー数が4400万人(2020年3月時点)に到達しました。 そもそも、「note」とは、クリエイターが、文章やマンガ、写真、音声を投稿することができ、ユーザーはその ...

5

ボードゲームカフェが1日2回転で儲かるという記事をみつけたので興味を持ち、調べてみました。 まずは、需要がどれくらいあるのか、市場のようすからみていきましょう。 世界最大のボードゲーム市場はドイツで、 ...

-code, ソーシャル
-,

Copyright© BUSINESS HUNTER , 2023 All Rights Reserved Powered by AFFINGER5.