게임 플레이 추가 작업까지 진행된 소스
using UnityEngine;
using System.Collections;
public class FarmerTouchControl : MonoBehaviour {
public Camera mainCamera;
public GameObject fireObj;
public Transform firePoint;
Vector3 fireDirection;
public float fireSpeed = 3;
bool enableAttack = true;
Vector3 lastInputPosition;
Vector3 tempVector3;
Vector2 tempVector2 = new Vector2();
GameObject tempObj;
Animator animator;
ShotObj shotObjScript;
GameObjectPool objPool;
Vector3 spawnPos = new Vector3(0, 50, 0);
void Awake()
{
animator = GetComponent<Animator>();
}
void OnEnable()
{
InitGameObjectPool();
}
void Update()
{
if( Input.GetMouseButton(0) )
{
lastInputPosition = Input.mousePosition;
if( enableAttack )
{
animator.SetTrigger("fire");
}
}
}
void FireTrigger()
{
Fire(lastInputPosition);
}
void FireEnd()
{
enableAttack = true;
}
void Fire(Vector3 inputPosition)
{
tempVector3 = mainCamera.ScreenToWorldPoint(inputPosition);
tempVector3.z = 0;
fireDirection = tempVector3 - firePoint.position;
fireDirection = fireDirection.normalized;
shotObjScript = null;
if( !objPool.NextGameObject(out tempObj) )
{
tempObj = Instantiate(
fireObj,
spawnPos,
Quaternion.Euler(Vector3.up * 90)
) as GameObject;
tempObj.name = tempObj.name + objPool.lastIndex;
objPool.AddGameObject(tempObj);
shotObjScript = tempObj.GetComponent<ShotObj>();
shotObjScript.InitReturnPosition(spawnPos);
}
if(shotObjScript == null)
{
shotObjScript = tempObj.GetComponent<ShotObj>();
}
tempObj.transform.position = firePoint.position;
tempVector2.Set(fireDirection.x, fireDirection.y);
tempVector2 = tempVector2 * fireSpeed;
tempObj.rigidbody2D.velocity = tempVector2;
shotObjScript.InitShotObj(1);
shotObjScript.TurnOnTrigger();
}
void InitGameObjectPool()
{
spawnPos += GameData.Instance.gamePlayManager
.gameObjectPoolPosition.transform.position;
objPool = new GameObjectPool(
spawnPos.x,
tempObj);
for(int i=0;i<20;++i)
{
shotObjScript = null;
GameObject makeObj =
Instantiate(
fireObj,
spawnPos,
Quaternion.Euler(Vector3.up * 90)
) as GameObject;
makeObj.name = makeObj.name + i;
objPool.AddGameObject(makeObj);
shotObjScript = makeObj.GetComponent<ShotObj>();
shotObjScript.InitReturnPosition(spawnPos);
}
}
}