mirror of
https://github.com/zoriya/Prototype-Magnifique.git
synced 2026-06-01 10:06:18 +00:00
Creating map generator
This commit is contained in:
@@ -2,7 +2,11 @@
|
||||
|
||||
public class MapGenerator : MonoBehaviour
|
||||
{
|
||||
[SerializeField] private Transform mapContainer;
|
||||
|
||||
[SerializeField] private MapTile[] tiles;
|
||||
[SerializeField] private Texture2D[] maps;
|
||||
|
||||
private Texture2D map;
|
||||
|
||||
private void Start()
|
||||
@@ -12,17 +16,83 @@ public class MapGenerator : MonoBehaviour
|
||||
else
|
||||
map = maps[Random.Range(0, maps.Length)];
|
||||
|
||||
for(int x = 0; x < map.width; x++)
|
||||
PlaceCamera();
|
||||
|
||||
for (int x = 0; x < map.width; x++)
|
||||
{
|
||||
for(int y = 0; y < map.height; y++)
|
||||
for (int y = 0; y < map.height; y++)
|
||||
{
|
||||
CreateTile(x, y);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void PlaceCamera()
|
||||
{
|
||||
Camera cam = GameObject.Find("Main Camera").GetComponent<Camera>();
|
||||
Vector3 topLeft = new Vector3(0, map.height, 0);
|
||||
Vector3 bottomRight = new Vector3(map.width, 0, 0);
|
||||
|
||||
cam.transform.position = new Vector3(map.width / 2, map.height / 2, -Vector3.Distance(topLeft, bottomRight) / 2.5f);
|
||||
}
|
||||
|
||||
private void CreateTile(int x, int y)
|
||||
{
|
||||
//Color pixelColor = map.GetPixel(x, y);
|
||||
Color pixelColor = map.GetPixel(x, y);
|
||||
MapTile tile = null;
|
||||
|
||||
foreach(MapTile foo in tiles)
|
||||
{
|
||||
if(foo.color == pixelColor)
|
||||
{
|
||||
tile = foo;
|
||||
}
|
||||
}
|
||||
|
||||
if (tile == null)
|
||||
return;
|
||||
|
||||
bool topPosition = (y + 1 < map.height) ? map.GetPixel(x, y + 1) == pixelColor : false;
|
||||
//bool underPosition = (y - 1 > 0) ? map.GetPixel(x, y - 1) == pixelColor : false;
|
||||
bool leftPosition = (x - 1 > 0) ? map.GetPixel(x - 1, y) == pixelColor : false;
|
||||
bool rightPosition = (x + 1 < map.width) ? map.GetPixel(x + 1, y) == pixelColor : false;
|
||||
|
||||
GameObject prefab = null;
|
||||
|
||||
if (topPosition)
|
||||
{
|
||||
if (!leftPosition)
|
||||
{
|
||||
prefab = tile.leftUnder;
|
||||
|
||||
if (!rightPosition)
|
||||
prefab = tile.bothUnder;
|
||||
}
|
||||
else if (!rightPosition)
|
||||
{
|
||||
prefab = tile.rightUnder;
|
||||
}
|
||||
else
|
||||
{
|
||||
prefab = tile.underPrefeb;
|
||||
}
|
||||
}
|
||||
else if (!leftPosition)
|
||||
{
|
||||
prefab = tile.leftPrefab;
|
||||
|
||||
if (!rightPosition)
|
||||
prefab = tile.bothTop;
|
||||
}
|
||||
else if (!rightPosition)
|
||||
{
|
||||
prefab = tile.rightPrefab;
|
||||
}
|
||||
else
|
||||
{
|
||||
prefab = tile.middlePrefab;
|
||||
}
|
||||
|
||||
Instantiate(prefab, new Vector3(x, y, 0), Quaternion.identity, mapContainer);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,15 @@
|
||||
using UnityEngine;
|
||||
|
||||
[System.Serializable]
|
||||
public class MapTile
|
||||
{
|
||||
public Color color;
|
||||
public GameObject leftPrefab;
|
||||
public GameObject middlePrefab;
|
||||
public GameObject rightPrefab;
|
||||
public GameObject underPrefeb;
|
||||
public GameObject leftUnder;
|
||||
public GameObject rightUnder;
|
||||
public GameObject bothUnder;
|
||||
public GameObject bothTop;
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 77e4551656e34f643a2534f5f2b370dc
|
||||
timeCreated: 1521034821
|
||||
licenseType: Free
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -23,6 +23,12 @@ public class PlayerMovement : MonoBehaviour
|
||||
[SerializeField] private int smallJumpPush = 5;
|
||||
[SerializeField] private float pushSpeed = 0.2f;
|
||||
|
||||
[Space]
|
||||
[SerializeField] private float hookRange = 15;
|
||||
[SerializeField] private GameObject hookObject;
|
||||
[SerializeField] private float playerHookSpeed = 15;
|
||||
private GameObject hook;
|
||||
|
||||
[Space]
|
||||
[SerializeField] private float dashSpeed = 15;
|
||||
[SerializeField] private GameObject smallProjectile;
|
||||
@@ -78,8 +84,6 @@ public class PlayerMovement : MonoBehaviour
|
||||
|
||||
private bool IsSliding()
|
||||
{
|
||||
CapsuleCollider col = GetComponent<CapsuleCollider>();
|
||||
|
||||
List<RaycastHit> raycastHits = Physics.BoxCastAll(rb.position - new Vector3(0, 0.6f, 0), new Vector3(0.55f, 0, 1), Vector3.up, Quaternion.identity, 1.2f).ToList();
|
||||
List<RaycastHit> walls = new List<RaycastHit>();
|
||||
|
||||
@@ -191,7 +195,7 @@ public class PlayerMovement : MonoBehaviour
|
||||
movingPlateform = null;
|
||||
|
||||
//Reload dash
|
||||
if (isGrounded || isSliding || isPushing)
|
||||
if (isGrounded || isPushing)
|
||||
canDash = true;
|
||||
|
||||
if (-1 < velocity && velocity < 1)
|
||||
@@ -215,7 +219,6 @@ public class PlayerMovement : MonoBehaviour
|
||||
if(dashTime > 0)
|
||||
{
|
||||
rb.velocity = dashVelocity;
|
||||
//dashVelocity.x -= dashVelocity.x / 10;
|
||||
dashVelocity.y -= dashVelocity.y / 10;
|
||||
dashTime--;
|
||||
|
||||
@@ -281,6 +284,35 @@ public class PlayerMovement : MonoBehaviour
|
||||
rb.AddForce(new Vector3(0, Mathf.Abs(rb.velocity.y) + gravity / 2, 0), ForceMode.Acceleration);
|
||||
}
|
||||
|
||||
//Hook
|
||||
if (Input.GetKeyUp(HookKey))
|
||||
{
|
||||
Destroy(hook);
|
||||
}
|
||||
if (Input.GetKeyDown(HookKey))
|
||||
{
|
||||
if (hook != null)
|
||||
Destroy(hook);
|
||||
|
||||
Vector3 direction = NormaliseMovement(Input.GetAxis(Horizontal), Input.GetAxis(Vertical));
|
||||
|
||||
RaycastHit hit;
|
||||
if (Physics.Raycast(rb.position, direction, out hit, hookRange))
|
||||
{
|
||||
hook = Instantiate(hookObject, rb.position, Quaternion.identity);
|
||||
hook.GetComponent<LineRenderer>().SetPositions(new Vector3[] { rb.position, hit.point });
|
||||
|
||||
if (hit.collider.tag == "Player")
|
||||
{
|
||||
hit.collider.GetComponent<PlayerMovement>().Hooked(direction, rb.position);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
print("Mised");
|
||||
}
|
||||
}
|
||||
|
||||
//Dash and small attack
|
||||
if (canDash && Input.GetKeyDown(DashKey))
|
||||
{
|
||||
@@ -301,12 +333,20 @@ public class PlayerMovement : MonoBehaviour
|
||||
Vector3 input = new Vector3(x, y, 0);
|
||||
input.Normalize();
|
||||
|
||||
if (input.x == 0 && input.x == 0)
|
||||
if (input.x == 0 && input.y == 0)
|
||||
input.x = 1;
|
||||
|
||||
return input;
|
||||
}
|
||||
|
||||
public void Hooked(Vector3 direction, Vector3 attackPosition)
|
||||
{
|
||||
float force = Vector3.Distance(rb.position, attackPosition) * -1;
|
||||
|
||||
rb.velocity = direction * playerHookSpeed * force;
|
||||
velocity = direction.x * playerHookSpeed * force;
|
||||
}
|
||||
|
||||
public void SmallProjectileHit(PlayerMovement victim)
|
||||
{
|
||||
canDash = true;
|
||||
|
||||
Reference in New Issue
Block a user