finished portal fundimentals

This commit is contained in:
2025-03-09 20:43:38 +02:00
parent 960991b17d
commit b62e851c0e
1229 changed files with 46770 additions and 7301 deletions

View File

@@ -0,0 +1,50 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CameraMovement : MonoBehaviour
{
[SerializeField] private Transform target;
[SerializeField] private float speed;
private MapInfo map;
private Camera cam;
void Start()
{
map = FindObjectOfType<MapInfo>();
cam = GetComponentInChildren<Camera>();
if (cam == null)
enabled = false;
}
// LateUpdate is called once at the end of frame
void LateUpdate()
{
if(target == null)
return;
Vector3 targetPositon = target.position;
if (map != null)
{
targetPositon = map.ClampPosition(targetPositon,
new Vector2(cam.orthographicSize * cam.aspect, cam.orthographicSize));
}
transform.position = Vector3.MoveTowards(transform.position, targetPositon, speed * Time.deltaTime);
}
public void SetTarget(Transform newTarget)
{
target = newTarget;
}
public void JumpToTarget()
{
if(target == null)
return;
transform.position = target.position;
}
}

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 1490e1d8991fb6546860b2b1859f6001
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

24
Assets/Scripts/Command.cs Normal file
View File

@@ -0,0 +1,24 @@
//Command.cs
using UnityEngine;
public abstract class Command
{
public abstract KeyCode Key { get; set; }
public abstract string Description { get; }
/*
Todo:
1. Add an abstract Execute method with an argument for receiver.
For your consideration:
What would make for a good Execute receiver parameter?
Should we try to limit the receiver types?
How precisely can we limit the receiver's type?
Could we move the receiver into command data instead?
What are the benefits of either approach? Which works best here?
*/
/*
2. Create a MoveCommand class, which executes Walk method in TileBasedMovement component.
Execute method of this command should call the Walk(Vector3 direction) method of GridMovement.
*/
}

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: c372f0cb4dbc3db4c991d69f9481efa4
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

8
Assets/Scripts/GUI.meta Normal file
View File

@@ -0,0 +1,8 @@
fileFormatVersion: 2
guid: 9f91583480983b6449cdd7996dc3edca
folderAsset: yes
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,34 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class ControlCard : MonoBehaviour
{
public Command MyCommand;
[SerializeField] private Text description;
[SerializeField] private Text key;
private RebindMenu rebindMenu;
public void Populate(Command command, RebindMenu rebindMenu)
{
this.rebindMenu = rebindMenu;
MyCommand = command;
Refresh();
}
public void Rebind()
{
rebindMenu.Open(this);
}
public void Refresh()
{
description.text = MyCommand.Description;
key.text = MyCommand.Key.ToString();
}
}

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 8940b9c7b5c3e474fb41c133c6d5921b
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,48 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ControlsMenu : MonoBehaviour
{
[Header("Widgets")]
[SerializeField] private GameObject mainPanel;
[SerializeField] private GameObject scrollContent;
[SerializeField] private RebindMenu rebindMenu;
[Header("Prefabs")]
[SerializeField] private ControlCard controlCardPrefab;
private InputHandler inputHandler;
// Start is called before the first frame update
void Start()
{
inputHandler = FindObjectOfType<InputHandler>();
if (inputHandler == null)
{
gameObject.SetActive(false);
return;
}
foreach (Command command in inputHandler.Keymap)
{
ControlCard card = Instantiate(controlCardPrefab, scrollContent.transform);
card.Populate(command, rebindMenu);
}
Close();
}
public void Open()
{
mainPanel.SetActive(true);
inputHandler.enabled = false;
}
public void Close()
{
mainPanel.SetActive(false);
rebindMenu.Close();
inputHandler.enabled = true;
}
}

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 2a01ec157fbb2184ba9fe507120720d7
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,37 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class RebindMenu : MonoBehaviour
{
[SerializeField] private Text instruction;
private ControlCard controlCard;
public void Open(ControlCard keybindCard)
{
controlCard = keybindCard;
instruction.text = $"Enter new key for\n" +
$"{controlCard.MyCommand.Description}";
gameObject.SetActive(true);
}
public void Close()
{
gameObject.SetActive(false);
}
//On GUI provides a convenient method to get keycodes through Event data
void OnGUI()
{
if(!(Event.current.isKey && Input.anyKeyDown))
return;
controlCard.MyCommand.Key = Event.current.keyCode;
controlCard.Refresh();
Close();
}
}

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: bd6df0aacf9fc0649996299244764e42
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,92 @@
//GridMovement.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class GridMovement : MonoBehaviour
{
[SerializeField] private float moveTime = 0.3f;
[SerializeField] private AnimationCurve spacing;
private bool isMoving;
private Animator animator;
private MapInfo map;
void Start()
{
animator = GetComponent<Animator>();
animator.SetFloat("y", -1);
map = FindObjectOfType<MapInfo>();
}
private void Update()
{
if (Input.GetKeyDown(KeyCode.W))
{
Walk(Vector3.up);
}
else if (Input.GetKeyDown(KeyCode.S))
{
Walk(Vector3.down);
}
else if (Input.GetKeyDown(KeyCode.A))
{
Walk(Vector3.left);
}
else if (Input.GetKeyDown(KeyCode.D))
{
Walk(Vector3.right);
}
}
public bool Walk(Vector3 direction)
{
if (isMoving)
return false;
if (!IsPositionWalkable(transform.position + direction))
return false;
StartCoroutine(WalkCoroutine(direction, moveTime));
return true;
}
private bool IsPositionWalkable(Vector3 pos)
{
if (Physics2D.OverlapPoint(pos) != null)
return false;
if (map != null)
{
return map.IsPositionInBounds(pos);
}
return true;
}
private IEnumerator WalkCoroutine(Vector3 direction, float duration)
{
Vector3 from = transform.position;
Vector3 to = from + direction;
animator.SetFloat("x", direction.x);
animator.SetFloat("y", direction.y);
animator.SetFloat("speed", 1f);
if (duration < float.Epsilon)
{
transform.position = to;
yield break;
}
isMoving = true;
float aggregate = 0;
while (aggregate < 1f)
{
aggregate += Time.deltaTime / duration;
transform.position = Vector3.Lerp(from, to, spacing.Evaluate(aggregate));
yield return null;
}
isMoving = false;
animator.SetFloat("speed", 0f);
}
}

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: bdd6baffed6b79b48bb8cf0161756493
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,31 @@
//InputHandler.cs
using UnityEngine;
using System.Collections.Generic;
using System.Linq;
public class InputHandler : MonoBehaviour
{
// SerializeField let's us assign a value in the Unity editor for private variables.
[SerializeField]
// The character currently being commanded
private GridMovement currentActor;
// A list of all characters in the scene
private List<GridMovement> allActors;
// Variables for binding commands to input and executing commands
public List<Command> Keymap = new List<Command>(); // keycode to command mapping
// Use this for initialization
void Awake()
{
// TODO: Add movement commands to the list
}
void Update()
{
// TODO: Loop through all commands and execute them on correct actor via correct key press.
}
}

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 024cba9d4ec5ad94f9c1a71c69531745
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

28
Assets/Scripts/MapInfo.cs Normal file
View File

@@ -0,0 +1,28 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class MapInfo : MonoBehaviour
{
[Header("Map Bounds")]
public Vector2Int MaxCoordinate;
public Vector2Int MinCoordinate;
public bool IsPositionInBounds(Vector3 pos)
{
return pos.x <= MaxCoordinate.x &&
pos.x >= MinCoordinate.x &&
pos.y <= MaxCoordinate.y &&
pos.y >= MinCoordinate.y;
}
public Vector3 ClampPosition(Vector3 pos, Vector2 border)
{
return new Vector3(
Mathf.Clamp(pos.x, MinCoordinate.x + border.x - 1, MaxCoordinate.x - border.x + 1),
Mathf.Clamp(pos.y, MinCoordinate.y + border.y - 1, MaxCoordinate.y - border.y + 1),
pos.z);
}
}

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: b1d98edaef7b5584b901e896b9884397
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant: