You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

292 lines
9.6 KiB

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Map : MonoBehaviour
{
GameObject LocalPlayer;
List<GameObject> Players = new List<GameObject>();
public GameObject[] Tiles;
public List<GameObject> Landmarks;
public int TileSize = 100;
public Vector2 MapDimensions;
public List<GameObject> Veggies;
//Minimum and maximum values for the map
public float minX { get { return (-MapDimensions.x * (TileSize / 2)); } }
public float maxX { get { return (MapDimensions.x * (TileSize / 2)); } }
public float minZ { get { return (-MapDimensions.y * (TileSize / 2)); } }
public float maxZ { get { return (MapDimensions.y * (TileSize / 2)); } }
#region StartupFunctions
private void Start()
{
LocalPlayer = Multiplayer.PlayersManager.Instance.LocalPlayer;
Players.Add(LocalPlayer);
foreach (GameObject curPlayer in Multiplayer.PlayersManager.Instance.RemotePlayers.Values)
{
Players.Add(curPlayer);
}
GenerateTiles();
CreateDummys();
}
//Create dummy players for the world wrapping
void CreateDummys()
{
foreach (GameObject Player in Players)
{
Player PS = Player.GetComponent<Player>();
Transform PT = Player.transform;
Transform model = PT.Find("Model");
PS.dummies.Add(Instantiate(model, new Vector3(PT.position.x + minX, PT.position.y, PT.position.z), transform.rotation, PT).gameObject);
PS.dummies.Add(Instantiate(model, new Vector3(PT.position.x + maxX, PT.position.y, PT.position.z), transform.rotation, PT).gameObject);
PS.dummies.Add(Instantiate(model, new Vector3(PT.position.x, PT.position.y, PT.position.z + minZ), transform.rotation, PT).gameObject);
PS.dummies.Add(Instantiate(model, new Vector3(PT.position.x, PT.position.y, PT.position.z + maxZ), transform.rotation, PT).gameObject);
foreach (GameObject dummie in PS.dummies)
{
foreach (Collider col in dummie.GetComponentsInChildren<Collider>())
{
col.enabled = false;
}
}
}
}
public void GenerateTiles()
{
float HalfSize = TileSize / 2;
Vector3 startPoint = new Vector3(-(MapDimensions.x * HalfSize), 0.0f, -(MapDimensions.y * HalfSize));
for (int x = 0; x < MapDimensions.x; x++)
{
for (int z = 0; z < MapDimensions.y; z++)
{
int randIndex = Random.Range(0, Tiles.Length);
float RandomRotation = Random.Range(0, 4) * 90;
Vector3 position = startPoint + new Vector3(TileSize * x + HalfSize, 0.0f, TileSize * z + HalfSize);
Debug.Log(position);
GameObject Tile = Instantiate(Tiles[randIndex]);
Tile.transform.position = position;
Tile.transform.Rotate(Vector3.up, RandomRotation);
Tile.name = "Tile [" + x + "," + z + "]";
if (x == 0)
{
Vector3 newPos = position;
newPos.x += TileSize * MapDimensions.x;
GameObject MirrorTile = Instantiate(Tiles[randIndex]);
MirrorTile.transform.position = newPos;
MirrorTile.transform.Rotate(Vector3.up, RandomRotation);
MirrorTile.name = Tile.name + "(Mirror)";
}
if (z == 0)
{
Vector3 newPos = position;
newPos.z += TileSize * MapDimensions.y;
GameObject MirrorTile = Instantiate(Tiles[randIndex]);
MirrorTile.transform.position = newPos;
MirrorTile.transform.Rotate(Vector3.up, RandomRotation);
MirrorTile.name = Tile.name + "(Mirror)";
}
if (x == 0 && z == 0)
{
Vector3 newPos = position;
newPos.x += TileSize * MapDimensions.x;
newPos.z += TileSize * MapDimensions.y;
GameObject MirrorTile = Instantiate(Tiles[randIndex]);
MirrorTile.transform.position = newPos;
MirrorTile.transform.Rotate(Vector3.up, RandomRotation);
MirrorTile.name = Tile.name + "(Mirror)";
}
if (x == MapDimensions.x - 1)
{
Vector3 newPos = position;
newPos.x -= TileSize * MapDimensions.x;
GameObject MirrorTile = Instantiate(Tiles[randIndex]);
MirrorTile.transform.position = newPos;
MirrorTile.transform.Rotate(Vector3.up, RandomRotation);
MirrorTile.name = Tile.name + "(Mirror)";
}
if (z == MapDimensions.y - 1)
{
Vector3 newPos = position;
newPos.z -= TileSize * MapDimensions.y;
GameObject MirrorTile = Instantiate(Tiles[randIndex]);
MirrorTile.transform.position = newPos;
MirrorTile.transform.Rotate(Vector3.up, RandomRotation);
MirrorTile.name = Tile.name + "(Mirror)";
}
if (x == MapDimensions.x - 1 && z == MapDimensions.y - 1)
{
Vector3 newPos = position;
newPos.x -= TileSize * MapDimensions.x;
newPos.z -= TileSize * MapDimensions.y;
GameObject MirrorTile = Instantiate(Tiles[randIndex]);
MirrorTile.transform.position = newPos;
MirrorTile.transform.Rotate(Vector3.up, RandomRotation);
MirrorTile.name = Tile.name + "(Mirror)";
}
if (x == 0 && z == MapDimensions.y - 1)
{
Vector3 newPos = position;
newPos.x += TileSize * MapDimensions.x;
newPos.z -= TileSize * MapDimensions.y;
GameObject MirrorTile = Instantiate(Tiles[randIndex]);
MirrorTile.transform.position = newPos;
MirrorTile.transform.Rotate(Vector3.up, RandomRotation);
MirrorTile.name = Tile.name + "(Mirror)";
}
if (x == MapDimensions.x - 1 && z == 0)
{
Vector3 newPos = position;
newPos.x -= TileSize * MapDimensions.x;
newPos.z += TileSize * MapDimensions.y;
GameObject MirrorTile = Instantiate(Tiles[randIndex]);
MirrorTile.transform.position = newPos;
MirrorTile.transform.Rotate(Vector3.up, RandomRotation);
MirrorTile.name = Tile.name + "(Mirror)";
}
}
}
}
//Populate the tile with landmarks
void PopulateTile(Tile tile)
{
foreach (GameObject LP in tile.LandmarkPoints)
{
if (Landmarks.Count > 0)
{
int rand = Random.Range(0, Landmarks.Count);
Instantiate(Landmarks[rand], LP.transform.position, Quaternion.identity, tile.transform.parent);
}
}
}
#endregion
void Update()
{
Teleport();
}
void Teleport()
{
Transform PT = LocalPlayer.transform;
if (PT.position.x > maxX)
{
PT.position = new Vector3(minX, PT.position.y, PT.position.z);
}
else if (PT.position.x < minX)
{
PT.position = new Vector3(maxX, PT.position.y, PT.position.z);
}
if (PT.position.z > maxZ)
{
PT.position = new Vector3(PT.position.x, PT.position.y, minZ);
}
else if (PT.position.z < minZ)
{
PT.position = new Vector3(PT.position.x, PT.position.y, maxZ);
}
}
}
//EXTRA STUFF, PLEASE IGNORE
/*
* //IN UPDATE
HandleCameraMovement(TopCam, true);
HandleCameraMovement(BotCam, true);
HandleCameraMovement(LeftCam, false);
HandleCameraMovement(RightCam, false);
HandleCamSizing();
*/
/*
/// <summary>
///
/// </summary>
/// <param name="Cam"> The camera to move</param>
/// <param name="Xmove">Do we move in the X axis or the Z?</param>
void HandleCameraMovement(Camera Cam, bool Xmove)
{
if (Xmove == true)
{
Cam.transform.position = new Vector3(LocalPlayer.transform.position.x, Cam.transform.position.y, Cam.transform.position.z);
}
else
{
Cam.transform.position = new Vector3(Cam.transform.position.x, Cam.transform.position.y, LocalPlayer.transform.position.z);
}
}
void HandleCamSizing()
{
Transform PT = LocalPlayer.transform;
if (PT.position.x > maxX - camWidth)
{
float dif = ((maxX - PT.position.x) / camWidth) / 2;
SetCam(dif, 0, RightCam);
}
else if (PT.position.x < minX + camWidth)
{
float dif = ((minX - PT.position.x) / camWidth) / 2;
SetCam(dif, 0, LeftCam);
}
else
{
SetCam(1, 1, RightCam);
SetCam(1, 1, LeftCam);
}
if (PT.position.z > maxZ - camHeight)
{
float dif = ((maxZ - PT.position.z) / camHeight) / 2;
SetCam(0, dif, TopCam);
}
else if (PT.position.x < + camHeight)
{
float dif = ((minZ - PT.position.z) / camHeight) / 2;
SetCam(0, dif, BotCam);
}
else
{
SetCam(1, 1, RightCam);
SetCam(1, 1, LeftCam);
}
}
void SetCam(float x, float y, Camera cam)
{
cam.rect = new Rect(x, y, 1, 1);
}
*/