Commit 6d02950c authored by Markus Schlösser's avatar Markus Schlösser
Browse files

Added movement to player controller

parent a75c69fa
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Tilemaps;
public class Charackter_Controller : MonoBehaviour
{
private bool isMoving;
private enum BlockType
{
Block,
Convex,
Concave
}
private enum PlayerHead
{
Up,
Right,
Down,
Left
}
public Tilemap tilemapVar;
private PlayerHead headPos=PlayerHead.Up;
[SerializeField] private BlockType blockRight;
public InputProviderPlayer InputProvider;
private TileBase nextTileOn,nextTileIn;
// Use this for initialization
void Start () {
InputProvider = GetComponent<InputProviderPlayer>();
}
// Update is called once per frame
void Update()
{
if (isMoving) return;
StartCoroutine(Wait());
Vector2 movement = InputProvider.MoveDirection;
var x = movement.x;
var y = movement.y;
if (isHorizontal())
{
nextTileOn= GetNextTileOn(Vector3Int.right *movement.x);
nextTile = CheckNextTile(Vector2.right* movement.x);
}
else
{
nextBlock= CheckNextBlock(Vector2.up *movement.y);
nextTile = CheckNextTile()
}
if (nextIsSomething() == true)
{
if (nextIsACube() == true)
{
MovePlayerCube();
}
else
{
if(nextIsKonkanv()==true)
MovePlayerconcav();
else
MovePlayerconvex();
}
}
else
{
return ;
}
}
private IEnumerator Wait()
{
isMoving = true;
yield return new WaitForSeconds(5.0f);
isMoving = false;
}
private TileBase GetNextTileOn(Vector3Int direction)
{
Vector3Int tilePos = Vector3Int.RoundToInt(transform.position + direction);
switch (headPos)
{
case PlayerHead.Up:
tilePos -= Vector3Int.up;
break;
case PlayerHead.Down:
tilePos+=Vector3Int.up ;
break;
case PlayerHead.Left:
tilePos += Vector3Int.right;
break;
case PlayerHead.Right:
tilePos -= Vector3Int.right;
break;
default:
print("Headpos error");
break;
}
return tilemapVar.GetTile(tilePos));
}
private TileBase GetNextTileIn(Vector3Int direction)
{
Vector3Int tilePos = Vector3Int.RoundToInt( transform.position + direction);
return tilemapVar.GetTile(tilePos);
}
private bool isHorizontal()
{
return (headPos == PlayerHead.Up || headPos == PlayerHead.Down);
}
}
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Tilemaps;
public class PlayerActor : MonoBehaviour {
private const int MAX_STEPS = 128;
private bool isMoving;
private enum MovementDirection {
Left = -1,
Right = 1
}
private struct RouteSnapshot {
public Vector3 position;
public Orientation orientation;
}
/// <summary>
/// Custom Modulo that does not return negative values
/// </summary>
/// <param name="i">Input value</param>
/// <param name="m">Divisior</param>
/// <returns>i % m</returns>
private static int Mod(int i, int m) {
int tmp = i % m;
return tmp < 0 ? tmp + m : tmp;
}
[SerializeField] private Tilemap tilemap;
[SerializeField] private Orientation orientation = Orientation.Up;
[SerializeField] public InputProviderPlayer InputProvider;
private void Start() {
InputProvider = GetComponent<InputProviderPlayer>();
}
private void Update() {
if (isMoving) return;
transform.position = tilemap.GetCellCenterWorld(tilemap.WorldToCell(transform.position));
float direction = InputProvider.MoveDirection.x;
if (direction < 0) {
StartCoroutine(Move(MovementDirection.Left));
} else if (direction > 0) {
StartCoroutine(Move(MovementDirection.Right));
}
}
private Vector3Int GetDirectionVector(Orientation o) {
switch (o) {
case Orientation.Up: return Vector3Int.up;
case Orientation.Left: return Vector3Int.left;
case Orientation.Right: return Vector3Int.right;
case Orientation.Down: return Vector3Int.down;
default:
throw new ArgumentOutOfRangeException();
}
}
private IEnumerator Move(MovementDirection direction) {
isMoving = true;
List<RouteSnapshot> route = new List<RouteSnapshot>();
Orientation o = orientation;
Vector3Int celllPos = tilemap.WorldToCell(transform.position);
bool isCompleteRoute = false;
bool isValidRoute = true;
int numSteps = 0;
while (!isCompleteRoute && isValidRoute) {
if (numSteps++ > MAX_STEPS) {
isValidRoute = false;
break;
}
Vector3Int directionVec = GetDirectionVector((Orientation) Mod((int) o + (int) direction, 4));
Vector3Int downVec = GetDirectionVector((Orientation) Mod((int) o + 2, 4));
Orientation enterFace = (Orientation) Mod((int) o - (int) direction, 4);
Debug.Log(enterFace);
Vector3Int adjacentCellPos = celllPos + directionVec;
Vector3Int adjacentDownCellPos = adjacentCellPos + downVec;
RotatedTile adjacentTile = tilemap.GetTile<RotatedTile>(adjacentCellPos);
RotatedTile adjacentDownTile = tilemap.GetTile<RotatedTile>(adjacentDownCellPos);
Debug.DrawLine(tilemap.GetCellCenterWorld(celllPos), tilemap.GetCellCenterWorld(adjacentCellPos), adjacentTile == null ? Color.red : Color.green);
Debug.DrawLine(tilemap.GetCellCenterWorld(celllPos), tilemap.GetCellCenterWorld(adjacentDownCellPos), adjacentDownTile == null ? Color.red : Color.green);
if (adjacentTile != null) {
switch (direction) {
case MovementDirection.Left:
if (adjacentTile[enterFace].enter == OnEnterAction.MoveConcaveRight) {
// TODO: Add animation
celllPos += directionVec;
o = (Orientation) Mod((int) o + 1, 4); // turn right
} else {
isValidRoute = false;
}
break;
case MovementDirection.Right:
if (adjacentTile[enterFace].enter == OnEnterAction.MoveConcaveLeft) {
// TODO: Add animation
celllPos += directionVec;
o = (Orientation) Mod((int) o - 1, 4); // turn left
} else {
isValidRoute = false;
}
break;
default:
throw new ArgumentOutOfRangeException(nameof(direction), direction, null);
}
} else if (adjacentDownTile != null) {
switch (direction) {
case MovementDirection.Left:
switch (adjacentDownTile[o].left) {
case OnWalkOverAction.Invalid:
isValidRoute = false;
break;
case OnWalkOverAction.MoveToCenter:
// TODO: Add animation
celllPos += directionVec;
isCompleteRoute = true;
break;
case OnWalkOverAction.MoveConvex:
// TODO: Add animation
celllPos += directionVec + directionVec + downVec;
o = (Orientation) Mod((int) o - 1, 4); // turn left
break;
default:
throw new ArgumentOutOfRangeException();
}
break;
case MovementDirection.Right:
switch (adjacentDownTile[o].right) {
case OnWalkOverAction.Invalid:
isValidRoute = false;
break;
case OnWalkOverAction.MoveToCenter:
// TODO: Add animation
celllPos += directionVec;
isCompleteRoute = true;
break;
case OnWalkOverAction.MoveConvex:
// TODO: Add animation
celllPos += directionVec + directionVec + downVec;
o = (Orientation) Mod((int) o + 1, 4); // turn right
break;
default:
throw new ArgumentOutOfRangeException();
}
break;
default:
throw new ArgumentOutOfRangeException(nameof(direction), direction, null);
}
} else {
isValidRoute = false;
}
if (isValidRoute) {
route.Add(new RouteSnapshot {
position = tilemap.GetCellCenterWorld(celllPos),
orientation = o
});
}
}
if (isValidRoute) {
foreach (RouteSnapshot v in route) {
transform.position = v.position;
transform.rotation = Quaternion.Euler(0, 0, (int)v.orientation * -90.0f);
orientation = v.orientation;
yield return new WaitForSeconds(0.25f);
}
}
isMoving = false;
}
}
......@@ -153,6 +153,78 @@ Tilemap:
m_ObjectToInstantiate: {fileID: 0}
m_TileFlags: 2
m_ColliderType: 2
- first: {x: 1, y: -3, z: 0}
second:
m_TileIndex: 7
m_TileSpriteIndex: 2
m_TileMatrixIndex: 0
m_TileColorIndex: 0
m_ObjectToInstantiate: {fileID: 0}
m_TileFlags: 2
m_ColliderType: 2
- first: {x: 2, y: -3, z: 0}
second:
m_TileIndex: 2
m_TileSpriteIndex: 2
m_TileMatrixIndex: 1
m_TileColorIndex: 0
m_ObjectToInstantiate: {fileID: 0}
m_TileFlags: 2
m_ColliderType: 2
- first: {x: 3, y: -3, z: 0}
second:
m_TileIndex: 5
m_TileSpriteIndex: 2
m_TileMatrixIndex: 2
m_TileColorIndex: 0
m_ObjectToInstantiate: {fileID: 0}
m_TileFlags: 2
m_ColliderType: 2
- first: {x: 4, y: -3, z: 0}
second:
m_TileIndex: 6
m_TileSpriteIndex: 2
m_TileMatrixIndex: 3
m_TileColorIndex: 0
m_ObjectToInstantiate: {fileID: 0}
m_TileFlags: 2
m_ColliderType: 2
- first: {x: 1, y: -2, z: 0}
second:
m_TileIndex: 1
m_TileSpriteIndex: 1
m_TileMatrixIndex: 0
m_TileColorIndex: 0
m_ObjectToInstantiate: {fileID: 0}
m_TileFlags: 2
m_ColliderType: 2
- first: {x: 2, y: -2, z: 0}
second:
m_TileIndex: 0
m_TileSpriteIndex: 1
m_TileMatrixIndex: 1
m_TileColorIndex: 0
m_ObjectToInstantiate: {fileID: 0}
m_TileFlags: 2
m_ColliderType: 2
- first: {x: 3, y: -2, z: 0}
second:
m_TileIndex: 3
m_TileSpriteIndex: 1
m_TileMatrixIndex: 2
m_TileColorIndex: 0
m_ObjectToInstantiate: {fileID: 0}
m_TileFlags: 2
m_ColliderType: 2
- first: {x: 4, y: -2, z: 0}
second:
m_TileIndex: 4
m_TileSpriteIndex: 1
m_TileMatrixIndex: 3
m_TileColorIndex: 0
m_ObjectToInstantiate: {fileID: 0}
m_TileFlags: 2
m_ColliderType: 2
- first: {x: -1, y: -1, z: 0}
second:
m_TileIndex: 2
......@@ -227,33 +299,33 @@ Tilemap:
m_ColliderType: 2
m_AnimatedTiles: {}
m_TileAssetArray:
- m_RefCount: 1
- m_RefCount: 2
m_Data: {fileID: 11400000, guid: 7cd616f6669f6034f8dc5fb8e1977157, type: 2}
- m_RefCount: 1
- m_RefCount: 2
m_Data: {fileID: 11400000, guid: 84b2e218d4d30144dbde79f0499c7097, type: 2}
- m_RefCount: 1
- m_RefCount: 2
m_Data: {fileID: 11400000, guid: f11ad8ee25ba757418fe29f03491430b, type: 2}
- m_RefCount: 1
- m_RefCount: 2
m_Data: {fileID: 11400000, guid: af9e9c5c7e4d807479465329ae5ab04e, type: 2}
- m_RefCount: 1
- m_RefCount: 2
m_Data: {fileID: 11400000, guid: abd8b20869637f8499648c976eab026d, type: 2}
- m_RefCount: 1
- m_RefCount: 2
m_Data: {fileID: 11400000, guid: 7b3c67dce1d8789468d91512f953bbe5, type: 2}
- m_RefCount: 1
- m_RefCount: 2
m_Data: {fileID: 11400000, guid: b675fe1d5bfe47b4785171d7159e6227, type: 2}
- m_RefCount: 1
- m_RefCount: 2
m_Data: {fileID: 11400000, guid: 88bce14208020a2469df1c47b7ec53c5, type: 2}
- m_RefCount: 1
m_Data: {fileID: 11400000, guid: 20a9ff2cdea8c4144b7bcd136a52986d, type: 2}
m_TileSpriteArray:
- m_RefCount: 1
m_Data: {fileID: 21300000, guid: b71d3df6a1ca4a44bad4d0877bc53e0e, type: 3}
- m_RefCount: 4
- m_RefCount: 8
m_Data: {fileID: 21300000, guid: d48532af0b85b9a4093f85a675f60009, type: 3}
- m_RefCount: 4
- m_RefCount: 8
m_Data: {fileID: 21300000, guid: 788213b0d137d4e4b96e99bab374c4b9, type: 3}
m_TileMatrixArray:
- m_RefCount: 3
- m_RefCount: 5
m_Data:
e00: 1
e01: 0
......@@ -271,7 +343,7 @@ Tilemap:
e31: 0
e32: 0
e33: 1
- m_RefCount: 2
- m_RefCount: 4
m_Data:
e00: 0.000000059604645
e01: -0.99999994
......@@ -289,7 +361,7 @@ Tilemap:
e31: 0
e32: 0
e33: 1
- m_RefCount: 2
- m_RefCount: 4
m_Data:
e00: -1
e01: 0.00000008742278
......@@ -307,7 +379,7 @@ Tilemap:
e31: 0
e32: 0
e33: 1
- m_RefCount: 2
- m_RefCount: 4
m_Data:
e00: 0.000000059604645
e01: 0.99999994
......@@ -326,12 +398,12 @@ Tilemap:
e32: 0
e33: 1
m_TileColorArray:
- m_RefCount: 9
- m_RefCount: 17
m_Data: {r: 1, g: 1, b: 1, a: 1}
m_AnimationFrameRate: 1
m_Color: {r: 1, g: 1, b: 1, a: 1}
m_Origin: {x: -1, y: -3, z: 0}
m_Size: {x: 5, y: 4, z: 1}
m_Size: {x: 11, y: 4, z: 1}
m_TileAnchor: {x: 0.5, y: 0.5, z: 0}
m_TileOrientation: 0
m_TileOrientationMatrix:
......
......@@ -13,3 +13,23 @@ MonoBehaviour:
m_EditorClassIdentifier:
sprite: {fileID: 21300000, guid: b71d3df6a1ca4a44bad4d0877bc53e0e, type: 3}
rotation: 0
top:
left: 1
right: 1
enter: 0
land: 0
left:
left: 1
right: 1
enter: 0
land: 0
right:
left: 1
right: 1
enter: 0
land: 0
bottom:
left: 1
right: 1
enter: 0
land: 0
......@@ -11,5 +11,25 @@ MonoBehaviour:
m_Script: {fileID: 11500000, guid: 85c106dd6ad68a043a95570cbdd83ca7, type: 3}
m_Name: SlopeConcave0
m_EditorClassIdentifier:
sprite: {fileID: 21300000, guid: d48532af0b85b9a4093f85a675f60009, type: 3}
sprite: {fileID: 21300000, guid: 788213b0d137d4e4b96e99bab374c4b9, type: 3}
rotation: 0
top:
left: 1
right: 1
enter: 0
land: 0
left:
left: 1
right: 1
enter: 0
land: 0
right:
left: 0
right: 0
enter: 1
land: 2
bottom:
left: 0
right: 0
enter: 2
land: 1
......@@ -11,5 +11,25 @@ MonoBehaviour:
m_Script: {fileID: 11500000, guid: 85c106dd6ad68a043a95570cbdd83ca7, type: 3}
m_Name: SlopeConcave180
m_EditorClassIdentifier:
sprite: {fileID: 21300000, guid: d48532af0b85b9a4093f85a675f60009, type: 3}
sprite: {fileID: 21300000, guid: 788213b0d137d4e4b96e99bab374c4b9, type: 3}
rotation: 2
top:
left: 0
right: 0
enter: 2
land: 1
left: