Created
October 16, 2017 20:00
-
-
Save tklee1975/cafe71045326d3d058917d4a70be3c6c to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public class TankFactory : MonoBehaviour { | |
| public TankMaster tankMasterPrefab; | |
| public GameObject[] tankModelList; | |
| //public Dictionary<int, GameObject> tankModelDict = new Dictionary<int, GameObject>(); | |
| // Use this for initialization | |
| public GameObject CreateModel(int tankID) { | |
| // Get the Model Prefab | |
| GameObject tankModel = tankModelList[tankID]; | |
| if(tankModel == null) { | |
| return null; | |
| } | |
| // instantiate | |
| GameObject newModel = GameObject.Instantiate(tankMasterPrefab.gameObject); | |
| //aimObject.transform.localPosition = GetAimArrowTransformPosition(); | |
| TankMaster tank = newModel.GetComponent<TankMaster>(); | |
| tank.SetupModel(tankModel); | |
| return newModel; | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| using System.Collections; | |
| using System.Collections.Generic; | |
| using UnityEngine; | |
| public class TankMaster : Tank { | |
| protected Transform mModelParentTransform; | |
| // Use this for initialization | |
| void Awake () { | |
| mModelParentTransform = transform.Find("ModelParent"); | |
| // Define the mTankTouchBounds | |
| BoxCollider2D box = GetComponent<BoxCollider2D>(); | |
| mTankTouchBounds = box.bounds; | |
| // | |
| SetupAimArrow(); | |
| } | |
| void Start () { | |
| } | |
| public void SetupModel(GameObject modelPrefab) | |
| { | |
| // Clear the reference model | |
| for(int i=0; i<mModelParentTransform.childCount; i++) { | |
| Transform tf = mModelParentTransform.GetChild(i); | |
| GameObject.Destroy(tf.gameObject); | |
| } | |
| // | |
| GameObject modelObject = GameObject.Instantiate(modelPrefab, mModelParentTransform, false); | |
| modelObject.transform.localPosition = Vector3.zero; | |
| mTankTransform = modelObject.transform; | |
| mTurretTransform = mTankTransform.Find("Turret"); | |
| mLaunchPositionTransform = mTurretTransform.Find("LaunchPosition"); | |
| } | |
| // Link to Other Prefabs | |
| public GameObject bulletPrefab; | |
| // Constants | |
| const float kLaunchMin = 30; | |
| const float kLaunchMax = 100; | |
| const float kLaunchPowerMin = 10; | |
| const float kLaunchPowerMax = 30; | |
| // Internal Structure | |
| protected AimArrow mArrow; | |
| protected Bounds mTankTouchBounds; | |
| // | |
| protected Vector2 mPressPosition; | |
| protected bool mHasPressed; | |
| protected bool mMoveCancelled = false; | |
| protected float mPower; | |
| // Internal Structure | |
| protected Transform mTankTransform; | |
| protected Transform mTurretTransform; | |
| protected Transform mLaunchPositionTransform; | |
| protected Vector2 mTurretPosition; | |
| protected Vector3 GetAimArrowTransformPosition() { | |
| return mTurretTransform.localPosition; | |
| } | |
| virtual protected void SetupAimArrow() | |
| { | |
| mArrow = GetComponentInChildren<AimArrow>(); | |
| mArrow.SetVisible(false); | |
| } | |
| // Update is called once per frame | |
| void Update () { | |
| UpdateStateWaitForInput(); | |
| } | |
| private bool IsTouchingMe(Vector3 touchPoint) { | |
| //Vector3 localPoint = touchPoint - | |
| Vector3 touchPosition = Camera.main.ScreenToWorldPoint(touchPoint); | |
| touchPosition.z = transform.position.z; | |
| Bounds bounds = mTankTouchBounds; | |
| bounds.center = transform.position; | |
| bool isTouching = bounds.Contains(touchPosition); | |
| Debug.Log("isTouching: touchPosition=" + touchPosition + " bound=" + bounds + " isTouching=" + isTouching); | |
| return isTouching; | |
| } | |
| public float GetLaunchPower(Vector2 mouseDiff) { | |
| float magnitude = mouseDiff.magnitude; | |
| // Get the value | |
| float diffRange = magnitude - kLaunchMin; | |
| float power; | |
| if(diffRange <= 0) { | |
| power = 0; | |
| } else { | |
| float scale = diffRange / (kLaunchMax - kLaunchMin); | |
| if(scale > 1) { | |
| scale = 1; | |
| } | |
| power = kLaunchPowerMin + (kLaunchPowerMax - kLaunchPowerMin) * scale; | |
| //power = kLaunchPowerMax; | |
| } | |
| Debug.Log("MouseDiff: magitude=" + magnitude + " power=" + power); | |
| return power; | |
| } | |
| public float GetTurretAngle() { | |
| float convertAngle = mTankTransform.localScale.x >= 0 ? 0 : 180; | |
| return mTurretTransform.eulerAngles.z + convertAngle; | |
| } | |
| public float GetAngle( Vector2 a, Vector2 b ) { | |
| var an = a.normalized; | |
| var bn = b.normalized; | |
| var x = a.x - b.x; | |
| var y = b.y - a.y; | |
| return Mathf.Atan2(-y, x) * Mathf.Rad2Deg; | |
| } | |
| float GetPowerScale(float power) { | |
| return power / kLaunchPowerMax; | |
| } | |
| void HandleRotateByMouse() | |
| { | |
| // Get Mouse Position | |
| Vector3 touchPosition = Input.mousePosition; | |
| Vector2 pos = new Vector2(touchPosition.x, touchPosition.y); | |
| // | |
| Vector2 diff = pos - mPressPosition; | |
| float launchPower = GetLaunchPower(diff); | |
| mArrow.SetArrowScale(GetPowerScale(launchPower)); | |
| mPower = launchPower * 10; | |
| mMoveCancelled = (launchPower <= 0) ? true : false; | |
| //float angle = Vector2.Angle(mPressPosition, pos); | |
| mArrow.Angle = GetAngle(mPressPosition, pos); | |
| SetTurretAngle(mArrow.Angle); | |
| } | |
| private Vector2 GetReferencePosition() { | |
| Vector3 turretPos = mTurretTransform.position; | |
| Vector3 screenPos = Camera.main.WorldToScreenPoint(turretPos); | |
| return new Vector2(screenPos.x, screenPos.y); | |
| } | |
| private void SetTurretAngle(float angle) | |
| { | |
| float convertAngle = mTankTransform.localScale.x >= 0 ? 0 : 180; | |
| Vector3 euler = mTurretTransform.eulerAngles; | |
| euler.z = angle + convertAngle; | |
| mTurretTransform.eulerAngles = euler; | |
| } | |
| private void UpdateStateWaitForInput() | |
| { | |
| if(Input.GetMouseButtonDown(0)){ | |
| //Vector2 pressPos = new Vector2(Input.mousePosition.x, Input.mousePosition.y); | |
| if(IsTouchingMe(Input.mousePosition)) { | |
| mPressPosition = GetReferencePosition(); | |
| mHasPressed = true; | |
| mMoveCancelled = false; | |
| } else { | |
| mHasPressed = false; | |
| } | |
| } | |
| if(mHasPressed) { | |
| mArrow.SetVisible(true); | |
| if(Input.GetMouseButton(0)) { | |
| HandleRotateByMouse(); | |
| } | |
| if(Input.GetMouseButtonUp(0)){ | |
| if(! mMoveCancelled) { | |
| LaunchBullet(); | |
| } | |
| mArrow.SetVisible(false); | |
| mMoveCancelled = false; | |
| mHasPressed = false; | |
| } | |
| } | |
| } | |
| void LaunchBullet() | |
| { | |
| Transform parent = transform.parent; | |
| GameObject bulletObject = GameObject.Instantiate(bulletPrefab, parent, true); | |
| bulletObject.transform.position = mLaunchPositionTransform.transform.position; | |
| Bullet bullet = bulletObject.GetComponent<Bullet>(); | |
| bullet.Launch(mPower, GetTurretAngle()); | |
| //testBullet.Launch(power, testTank.GetTurretAngle()); | |
| } | |
| } | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment