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.
 
 
 

253 lines
8.3 KiB

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Map : MonoBehaviour
{
public GameObject[] Tiles;
public List<GameObject> Landmarks;
public int TileSize = 100;
public Vector2 MapDimensions;
//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()
{
GenerateTiles();
CreateDummys();
Random.State state = Random.state;
Random.InitState((int)System.DateTime.Now.Ticks);
Multiplayer.PlayersManager.Instance.LocalPlayer.transform.position = new Vector3(Random.Range(minX, maxX), 5, Random.Range(minX, maxX));
Random.state = state;
}
//Create dummy players for the world wrapping
void CreateDummys()
{
foreach (PlayerData playerData in Multiplayer.PlayersManager.Instance.Players.Values)
{
playerData.Player.CreateDummies(this);
}
}
public void GenerateTiles()
{
float HalfSize = TileSize / 2;
Vector3 startPoint = new Vector3(-(MapDimensions.x * HalfSize), 0.0f, -(MapDimensions.y * HalfSize));
GameObject Map = new GameObject("Map Tiles");
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],Map.transform);
Tile.transform.position = position;
Tile.transform.Rotate(Vector3.up, RandomRotation);
Tile.name = "Tile [" + x + "," + z + "]";
PopulateTile(Tile.GetComponent<Tile>());
if (x == 0)
{
Vector3 newPos = position;
newPos.x += TileSize * MapDimensions.x;
GameObject MirrorTile = Instantiate(Tile, Map.transform);
MirrorTile.transform.position = newPos;
MirrorTile.transform.rotation = Tile.transform.rotation;
MirrorTile.name = Tile.name + "(Mirror)";
}
if (z == 0)
{
Vector3 newPos = position;
newPos.z += TileSize * MapDimensions.y;
GameObject MirrorTile = Instantiate(Tile, Map.transform);
MirrorTile.transform.position = newPos;
MirrorTile.transform.rotation = Tile.transform.rotation;
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(Tile, Map.transform);
MirrorTile.transform.position = newPos;
MirrorTile.transform.rotation = Tile.transform.rotation;
MirrorTile.name = Tile.name + "(Mirror)";
}
if (x == MapDimensions.x - 1)
{
Vector3 newPos = position;
newPos.x -= TileSize * MapDimensions.x;
GameObject MirrorTile = Instantiate(Tile, Map.transform);
MirrorTile.transform.position = newPos;
MirrorTile.transform.rotation = Tile.transform.rotation;
MirrorTile.name = Tile.name + "(Mirror)";
}
if (z == MapDimensions.y - 1)
{
Vector3 newPos = position;
newPos.z -= TileSize * MapDimensions.y;
GameObject MirrorTile = Instantiate(Tile, Map.transform);
MirrorTile.transform.position = newPos;
MirrorTile.transform.rotation = Tile.transform.rotation;
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(Tile, Map.transform);
MirrorTile.transform.position = newPos;
MirrorTile.transform.rotation = Tile.transform.rotation;
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(Tile, Map.transform);
MirrorTile.transform.position = newPos;
MirrorTile.transform.rotation = Tile.transform.rotation;
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(Tile, Map.transform);
MirrorTile.transform.position = newPos;
MirrorTile.transform.rotation = Tile.transform.rotation;
MirrorTile.name = Tile.name + "(Mirror)";
}
}
}
StaticBatchingUtility.Combine(Map);
}
//Populate the tile with landmarks
void PopulateTile(Tile tile)
{
foreach (Transform LP in tile.LandMarkLocations)
{
if (Landmarks.Count > 0)
{
int rand = Random.Range(0, Landmarks.Count);
Instantiate(Landmarks[rand], LP.transform.position, Landmarks[rand].transform.rotation, tile.transform);
Landmarks.Remove(Landmarks[rand]);
}
}
}
#endregion
}
//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);
}
*/