nirasan's tech blog

趣味や仕事の覚え書きです。Linux, Perl, PHP, Ruby, Javascript, Android, Cocos2d-x, Unity などに興味があります。

2D Roguelike を試してみて使いまわせそうなイディオム


  • Unity のチュートリアル 2D RogueLike が気になったので、試してみて使えそうなイディオムをいくつかメモ。
  • ゲーム自体は、矢印で移動、移動する毎にfood減少、アイテムでfood増加、敵の攻撃でfood減少、階段で次の階へ、アイテム・壁・敵は階ごとにランダム、という単純な感じ


  • GameManager.cs をシングルトンとして扱う
		public static GameManager instance = null;				//Static instance of GameManager which allows it to be accessed by any other script.

		//Awake is always called before any Start functions
		void Awake()
			//Check if instance already exists
			if (instance == null)
				//if not, set instance to this
				instance = this;
			//If instance already exists and it's not this:
			else if (instance != this)
				//Then destroy this. This enforces our singleton pattern, meaning there can only ever be one instance of a GameManager.
			//Sets this to not be destroyed when reloading scene


  • 配置する位置の候補を List にしておき...
		//Clears our list gridPositions and prepares it to generate a new board.
		void InitialiseList ()
			//Clear our list gridPositions.
			gridPositions.Clear ();
			//Loop through x axis (columns).
			for(int x = 1; x < columns-1; x++)
				//Within each column, loop through y axis (rows).
				for(int y = 1; y < rows-1; y++)
					//At each index add a new Vector3 to our list with the x and y coordinates of that position.
					gridPositions.Add (new Vector3(x, y, 0f));
  • 候補からランダムに選出して List から除外する
		//RandomPosition returns a random position from our list gridPositions.
		Vector3 RandomPosition ()
			//Declare an integer randomIndex, set it's value to a random number between 0 and the count of items in our List gridPositions.
			int randomIndex = Random.Range (0, gridPositions.Count);
			//Declare a variable of type Vector3 called randomPosition, set it's value to the entry at randomIndex from our List gridPositions.
			Vector3 randomPosition = gridPositions[randomIndex];
			//Remove the entry at randomIndex from the list so that it can't be re-used.
			gridPositions.RemoveAt (randomIndex);
			//Return the randomly selected Vector3 position.
			return randomPosition;


  • Physics2D.Linecast で2点間に引いた線と衝突するものを調べることで移動可能かどうか確認する
  • Physics2D.Linecast 実行中は動かしたいオブジェクト自体の Collider を disable にする
		//Move returns true if it is able to move and false if not. 
		//Move takes parameters for x direction, y direction and a RaycastHit2D to check collision.
		protected bool Move (int xDir, int yDir, out RaycastHit2D hit)
			//Store start position to move from, based on objects current transform position.
			Vector2 start = transform.position;
			// Calculate end position based on the direction parameters passed in when calling Move.
			Vector2 end = start + new Vector2 (xDir, yDir);
			//Disable the boxCollider so that linecast doesn't hit this object's own collider.
			boxCollider.enabled = false;
			//Cast a line from start point to end point checking collision on blockingLayer.
			hit = Physics2D.Linecast (start, end, blockingLayer);
			//Re-enable boxCollider after linecast
			boxCollider.enabled = true;
			//Check if anything was hit
			if(hit.transform == null)
				//If nothing was hit, start SmoothMovement co-routine passing in the Vector2 end as destination
				StartCoroutine (SmoothMovement (end));
				//Return true to say that Move was successful
				return true;
			//If something was hit, return false, Move was unsuccesful.
			return false;
  • 移動できなかった場合に衝突したものを取得する例
		//The virtual keyword means AttemptMove can be overridden by inheriting classes using the override keyword.
		//AttemptMove takes a generic parameter T to specify the type of component we expect our unit to interact with if blocked (Player for Enemies, Wall for Player).
		protected virtual void AttemptMove <T> (int xDir, int yDir)
			where T : Component
			//Hit will store whatever our linecast hits when Move is called.
			RaycastHit2D hit;
			//Set canMove to true if Move was successful, false if failed.
			bool canMove = Move (xDir, yDir, out hit);
			//Check if nothing was hit by linecast
			if(hit.transform == null)
				//If nothing was hit, return and don't execute further code.
			//Get a component reference to the component of type T attached to the object that was hit
			T hitComponent = hit.transform.GetComponent <T> ();
			//If canMove is false and hitComponent is not equal to null, meaning MovingObject is blocked and has hit something it can interact with.
			if(!canMove && hitComponent != null)
				//Call the OnCantMove function and pass it hitComponent as a parameter.
				OnCantMove (hitComponent);

Position の Tween

  • プレイヤーや敵など移動するオブジェクトの親クラス MovingObject.cs でスムーズに位置を変更する
  • Vector3 同士の減算で差のベクトルを出し、Vector3.sqrMagnitude で距離の累乗を算出し、とても小さい数 float.Epsilon より小さければ Vector3 同士が同一であると見なして移動終了とする
		//Co-routine for moving units from one space to next, takes a parameter end to specify where to move to.
		protected IEnumerator SmoothMovement (Vector3 end)
			//Calculate the remaining distance to move based on the square magnitude of the difference between current position and end parameter. 
			//Square magnitude is used instead of magnitude because it's computationally cheaper.
			float sqrRemainingDistance = (transform.position - end).sqrMagnitude;
			//While that distance is greater than a very small amount (Epsilon, almost zero):
			while(sqrRemainingDistance > float.Epsilon)
				//Find a new position proportionally closer to the end, based on the moveTime
				Vector3 newPostion = Vector3.MoveTowards(rb2D.position, end, inverseMoveTime * Time.deltaTime);
				//Call MovePosition on attached Rigidbody2D and move it to the calculated position.
				rb2D.MovePosition (newPostion);
				//Recalculate the remaining distance after moving.
				sqrRemainingDistance = (transform.position - end).sqrMagnitude;
				//Return and loop until sqrRemainingDistance is close enough to zero to end the function
				yield return null;