using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class MouseController : MonoBehaviour
{
public GameObject cursorCircle;
Vector3 lastFramePosition;
Vector3 dragStartPosition;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
Vector3 currFramePosition = Camera.main.ScreenToWorldPoint(Input.mousePosition);
currFramePosition.z = 0;
Debug.Log(currFramePosition.x);
//cursorCircle update
Tile tileUnderMouse = GetTileAtWorldCoord(currFramePosition);
if(tileUnderMouse != null) {
cursorCircle.SetActive(true);
Vector3 cursorPosition = new Vector3(tileUnderMouse.X, tileUnderMouse.Y, 0);
cursorCircle.transform.position = cursorPosition;
}
else
{
cursorCircle.SetActive(false);
}
//Drag start
if( Input.GetMouseButtonDown(0) )
{
dragStartPosition = currFramePosition;
}
//Drag end
if ( Input.GetMouseButtonUp(0) )
{
int start_x = Mathf.FloorToInt(dragStartPosition.x);
int end_x = Mathf.FloorToInt(currFramePosition.x);
if(end_x < start_x)
{
int temp = end_x;
end_x = start_x;
start_x = temp;
}
int start_y = Mathf.FloorToInt(dragStartPosition.y);
int end_y = Mathf.FloorToInt(currFramePosition.y);
if (end_y < start_y)
{
int temp = end_y;
end_y = start_y;
start_y = temp;
}
for (int x = start_x; x <= end_x; x++)
{
for (int y = start_y ; y <= end_y; y++)
{
Tile t = WorldController.Instance.World.GetTileAt(x, y);
if(t != null)
{
t.Type = Tile.TileType.Floor;
}
}
}
}
//Screen dragging
if ( Input.GetMouseButton(1) || Input.GetMouseButton(2) )
{
Vector3 diff = lastFramePosition - currFramePosition;
Camera.main.transform.Translate(diff);
}
lastFramePosition = Camera.main.ScreenToWorldPoint(Input.mousePosition);
lastFramePosition.z = 0;
}
Tile GetTileAtWorldCoord(Vector3 coord)
{
int x = Mathf.FloorToInt(coord.x);
int y = Mathf.FloorToInt(coord.y);
GameObject.FindObjectOfType<WorldController>();
return WorldController.Instance.World.GetTileAt(x, y);
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class WorldController : MonoBehaviour
{
public static WorldController Instance { get; protected set; }
public Sprite floorSprite;
public World World { get; protected set; }
// Start is called before the first frame update
void Start()
{
if(Instance != null)
{
Debug.Log("There shouldn't be two world controller.");
}
Instance = this;
//World with empty tiles
World = new World();
// Create GameObject for each tile
for (int x = 0; x < World.Width; x++)
{
for (int y = 0; y < World.Height; y++)
{
Tile tile_data = World.GetTileAt(x, y);
GameObject tile_go = new GameObject();
tile_go.name = "Tile_" + x + "_" + y;
tile_go.transform.position = new Vector3( tile_data.X, tile_data.Y, 0 );
tile_go.transform.SetParent(this.transform, true);
//Sprite renderer
tile_go.AddComponent<SpriteRenderer>();
tile_data.RegisterTileTypeChangedCallback( (tile) => { OnTileTypeChanged(tile, tile_go); } );
}
}
World.RandomizeTiles();
}
// Update is called once per frame
void Update()
{
}
void OnTileTypeChanged(Tile tile_data, GameObject tile_go)
{
if (tile_data.Type == Tile.TileType.Floor)
{
tile_go.GetComponent<SpriteRenderer>().sprite = floorSprite;
}
else if (tile_data.Type == Tile.TileType.Empty)
{
tile_go.GetComponent<SpriteRenderer>().sprite = null;
}
else
{
Debug.LogError("OnTileTypeChanged - Unknown tile type.");
}
}
}
These 2 different classes but integrated together after i add this code to the WorldController nothing is working i had some infinity loops but i solved them
//Drag start
if( Input.GetMouseButtonDown(0) )
{
dragStartPosition = currFramePosition;
}
//Drag end
if ( Input.GetMouseButtonUp(0) )
{
int start_x = Mathf.FloorToInt(dragStartPosition.x);
int end_x = Mathf.FloorToInt(currFramePosition.x);
if(end_x < start_x)
{
int temp = end_x;
end_x = start_x;
start_x = temp;
}
int start_y = Mathf.FloorToInt(dragStartPosition.y);
int end_y = Mathf.FloorToInt(currFramePosition.y);
if (end_y < start_y)
{
int temp = end_y;
end_y = start_y;
start_y = temp;
}
for (int x = start_x; x <= end_x; x++)
{
for (int y = start_y ; y <= end_y; y++)
{
Tile t = WorldController.Instance.World.GetTileAt(x, y);
if(t != null)
{
t.Type = Tile.TileType.Floor;
}
}
}
}
Now nothing works screen dragging or anything when i put these codes to the empty object.I dont know why i had a infinite loop and closed the unity because of that after that i added the scripts the the empty gameobjects again but now not working please help