Commit 314dae63 authored by 15박보승's avatar 15박보승

Implementing player control (camera control, mouse drag control)

parent 7d136bc8
This diff is collapsed.
fileFormatVersion: 2
guid: a2fec545d4e0d6440b73f21070aef44d
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:
......@@ -890,6 +890,7 @@ GameObject:
- component: {fileID: 1627406712}
- component: {fileID: 1627406711}
- component: {fileID: 1627406710}
- component: {fileID: 1627406713}
m_Layer: 0
m_Name: Main Camera
m_TagString: MainCamera
......@@ -962,6 +963,20 @@ Transform:
m_Father: {fileID: 0}
m_RootOrder: 0
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
--- !u!114 &1627406713
MonoBehaviour:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 1627406709}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: edaf8521323363f4a97a0d674404227a, type: 3}
m_Name:
m_EditorClassIdentifier:
camera: {fileID: 1627406711}
moveSpeed: 5
--- !u!1 &1824588112
GameObject:
m_ObjectHideFlags: 0
......
fileFormatVersion: 2
guid: 37152d58a2d0da54fbfe8542a0f00595
folderAsset: yes
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:
......@@ -3,7 +3,7 @@ using System.Collections.Generic;
using UnityEngine;
using BS;
public class Actor : MonoBehaviour
public abstract class Actor : MonoBehaviour
{
protected NodalPathfinding2DAgent agent = null;
......@@ -40,4 +40,11 @@ public class Actor : MonoBehaviour
{
agent = GetComponent<NodalPathfinding2DAgent>();
}
public void MoveTo(Vector2 destination)
{
agent.MoveTo(destination);
}
public abstract void OnSelected();
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class DummyCharacter : PlayableCharacter
{
protected override void AimingControl()
{
throw new System.NotImplementedException();
}
protected override void DefaultControl()
{
throw new System.NotImplementedException();
}
}
fileFormatVersion: 2
guid: c0b042379f4eae54e9e5fade272f0763
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
......@@ -93,4 +93,9 @@ public class Enemy : Actor
eyesightMesh.SetVertices(vertices);
eyesightMesh.SetTriangles(indices, 0);
}
public override void OnSelected()
{
throw new System.NotImplementedException();
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using BS;
enum CharacterControlMode
{
DEFAULT,
AIMING,
}
[RequireComponent(typeof(NodalPathfinding2DAgent))]
[RequireComponent(typeof(LineRenderer))]
public abstract class PlayableCharacter : Actor
{
public float shotRange = 5.0f;
public LayerMask shotBlockMask;
protected LineRenderer lr;
private float r = 0.0f;
public bool isSelected = false;
protected override void Start()
{
base.Start();
lr = GetComponent<LineRenderer>();
}
protected virtual void Update()
{
if (isSelected)
{
r = Mathf.Min(shotRange, r + shotRange * Time.deltaTime);
Vector3[] vertices = new Vector3[361];
for (int i = 0; i < 360; i++)
{
Vector2 direction = (Quaternion.Euler(0, 0, i) * Vector2.right).normalized;
RaycastHit2D hit = Physics2D.Raycast(transform.position, direction, r, shotBlockMask);
if (hit.collider != null)
vertices[i] = hit.point;
else
vertices[i] = transform.position + new Vector3(direction.x, direction.y) * r;
}
vertices[360] = vertices[0];
lr.SetPositions(vertices);
}
}
public override void OnSelected()
{
isSelected = true;
r = 0;
}
protected abstract void DefaultControl();
protected abstract void AimingControl();
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CameraController : MonoBehaviour
{
public GameObject target = null;
public bool isLocked = false;
// Update is called once per frame
void Update()
{
if (isLocked)
{
}
}
}
fileFormatVersion: 2
guid: b95ae6b0dc7c13347bec65034167ee27
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
fileFormatVersion: 2
guid: d3edd8655cdd5b24ea258d42c5321d61
folderAsset: yes
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public static class Vector2Extension
{
public static Vector2 Abs(this Vector2 vec2)
{
return new Vector2(Mathf.Abs(vec2.x), Mathf.Abs(vec2.y));
}
}
fileFormatVersion: 2
guid: 763a912657b50244d860e48c1eb7746a
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class SingletonBehaviour<T> : MonoBehaviour where T : MonoBehaviour
{
private static T _inst = null;
public static T inst
{
get
{
if (_inst == null)
{
T[] objects = GameObject.FindObjectsOfType<T>();
if (objects.Length > 1)
Debug.LogError("Multiple singleton exists!");
if (objects.Length > 0)
_inst = objects[0];
if (_inst == null)
{
GameObject obj = new GameObject(typeof(T).Name);
_inst = obj.AddComponent<T>();
}
}
return _inst;
}
}
public void SetStatic()
{
GameObject.DontDestroyOnLoad(inst.gameObject);
}
}
fileFormatVersion: 2
guid: cb161611e682d1a4bb9c9f209422405f
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class IngameUIManager : SingletonBehaviour<IngameUIManager>
{
public RectTransform dragUI;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
}
public void EnableDragUI()
{
dragUI.gameObject.SetActive(true);
dragUI.sizeDelta = Vector2.zero;
}
public void DisableDragUI()
{
dragUI.gameObject.SetActive(false);
}
public void UpdateDragUI(Vector2 startPoint, Vector2 endPoint)
{
startPoint = Camera.main.WorldToViewportPoint(startPoint);
endPoint = Camera.main.WorldToViewportPoint(endPoint);
Debug.Log("Start : " + startPoint + ", End : " + endPoint);
dragUI.anchorMin = dragUI.anchorMax = (startPoint + endPoint) / 2;
Vector2 size = endPoint - startPoint;
RectTransform rect = GetComponent<RectTransform>();
size.x = Mathf.Abs(rect.sizeDelta.x * size.x);
size.y = Mathf.Abs(rect.sizeDelta.y * size.y);
dragUI.sizeDelta = size;
}
}
fileFormatVersion: 2
guid: 45de64ddb773a3d458c38a048ddac13b
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using BS;
enum CharacterControlMode
{
DEFAULT,
AIMING,
}
[RequireComponent(typeof(NodalPathfinding2DAgent))]
public abstract class PlayableCharacter : MonoBehaviour
{
private NodalPathfinding2DAgent agent;
protected virtual void Start()
{
agent = GetComponent<NodalPathfinding2DAgent>();
}
protected abstract void DefaultControl();
protected abstract void AimingControl();
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerController : MonoBehaviour
{
[SerializeField]
private new Camera camera;
public float moveSpeed = 5.0f;
private Vector3 clickPos;
private Vector3 dragPos;
private Vector3 releasePos;
private void Awake()
{
}
private void Update()
{
MouseControl();
}
public void MouseControl()
{
if (Input.GetMouseButtonDown(0))
{
clickPos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
IngameUIManager.inst.EnableDragUI();
}
if (Input.GetMouseButton(0))
{
dragPos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
IngameUIManager.inst.UpdateDragUI(clickPos, dragPos);
}
if (Input.GetMouseButtonUp(0))
{
releasePos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
IngameUIManager.inst.DisableDragUI();
Vector2 size = clickPos - releasePos;
size = size.Abs();
foreach(var cast in Physics2D.OverlapBoxAll((clickPos + releasePos) / 2, size, 0))
{
Actor actor = cast.GetComponent<Actor>();
if (actor != null)
{
actor.OnSelected();
}
}
}
}
public void CameraControl()
{
float horizontal = 0;
float vertical = 0;
Vector2 mousePosition = Input.mousePosition;
if (Screen.width - Input.mousePosition.x < Screen.width / 10)
{
horizontal = 1;
}
else if (Input.mousePosition.x < Screen.width / 10)
{
horizontal = -1;
}
if (Screen.height - Input.mousePosition.y < Screen.height / 10)
{
vertical = 1;
}
else if (Input.mousePosition.y < Screen.height / 10)
{
vertical = -1;
}
camera.transform.position += new Vector3(horizontal, vertical) * moveSpeed * Time.deltaTime;
//camera.transform.position += new Vector3(Input.GetAxis("Horizontal"), Input.GetAxis("Vertical"), 0) * moveSpeed * Time.deltaTime;
}
public void OnMove(Vector2 direction)
{
Debug.Log("A");
transform.position += new Vector3(direction.x, 0, direction.y).normalized * moveSpeed * Time.deltaTime;
}
}
fileFormatVersion: 2
guid: edaf8521323363f4a97a0d674404227a
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment