Ajoutez des fichiers projet.
This commit is contained in:
45
Assets/Controllers/MouseController.cs
Normal file
45
Assets/Controllers/MouseController.cs
Normal file
@@ -0,0 +1,45 @@
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
using Unity.VisualScripting;
|
||||
|
||||
public class MouseController : MonoBehaviour
|
||||
{
|
||||
Vector3 lastFramePosition;
|
||||
public Camera InterestingCamera;
|
||||
|
||||
// Start is called before the first frame update
|
||||
void Start()
|
||||
{
|
||||
if(InterestingCamera == null)
|
||||
InterestingCamera = Camera.main;
|
||||
}
|
||||
|
||||
// Update is called once per frame
|
||||
void Update()
|
||||
{
|
||||
HandleCamera();
|
||||
HandleWorldInteraction();
|
||||
}
|
||||
|
||||
void HandleCamera()
|
||||
{
|
||||
var currentFramePosition = InterestingCamera.ScreenToWorldPoint(Input.mousePosition);
|
||||
|
||||
if (Input.GetMouseButton(1))
|
||||
{
|
||||
var diff = lastFramePosition - currentFramePosition;
|
||||
InterestingCamera.transform.Translate(diff);
|
||||
}
|
||||
|
||||
lastFramePosition = InterestingCamera.ScreenToWorldPoint(Input.mousePosition);
|
||||
}
|
||||
|
||||
void HandleWorldInteraction()
|
||||
{
|
||||
if (Input.GetMouseButtonDown(0))
|
||||
{
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
11
Assets/Controllers/MouseController.cs.meta
Normal file
11
Assets/Controllers/MouseController.cs.meta
Normal file
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: d760f8aeec6ce3b4286ce8621cca1f7c
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
167
Assets/Controllers/StageController.cs
Normal file
167
Assets/Controllers/StageController.cs
Normal file
@@ -0,0 +1,167 @@
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using Unity.VisualScripting;
|
||||
using UnityEngine;
|
||||
|
||||
public class StageController : MonoBehaviour
|
||||
{
|
||||
public Train Train;
|
||||
public int FocusedWagon = 0;
|
||||
public int FocusedLevel = 0;
|
||||
|
||||
Stage Stage { get; set; }
|
||||
|
||||
public Sprite FloorSprite;
|
||||
public Sprite WallSprite;
|
||||
|
||||
GameObject[,] AllTilesGameObjects;
|
||||
GameObject[,] HorizontalWallsGameObjects;
|
||||
GameObject[,] VerticalWallsGameObjects;
|
||||
|
||||
public bool ShouldRandomize = false;
|
||||
|
||||
// Start is called before the first frame update
|
||||
void Start()
|
||||
{
|
||||
if (Train == null)
|
||||
{
|
||||
Train = new Train();
|
||||
Train.Generate();
|
||||
}
|
||||
FocusCurrentStage();
|
||||
|
||||
AllTilesGameObjects = new GameObject[Stage.Width, Stage.Height];
|
||||
HorizontalWallsGameObjects = new GameObject[Stage.Width, Stage.Height+1];
|
||||
VerticalWallsGameObjects = new GameObject[Stage.Width+1, Stage.Height];
|
||||
|
||||
//Tiles creation
|
||||
for(int x=0; x<Stage.Width; x++)
|
||||
for(int y=0; y<Stage.Height; y++)
|
||||
{
|
||||
var tile = Stage[x, y];
|
||||
|
||||
GameObject tile_go = new GameObject();
|
||||
AllTilesGameObjects[x, y] = tile_go;
|
||||
tile_go.name = "tile_" + x + "_" + y;
|
||||
tile_go.transform.position = new Vector3(x, y, 0);
|
||||
tile_go.transform.SetParent(this.transform, true);
|
||||
|
||||
tile_go.AddComponent<SpriteRenderer>();
|
||||
OnTileUpdated(tile, tile_go);
|
||||
}
|
||||
|
||||
Stage.OnTileChanged = (Tile tile) =>
|
||||
{
|
||||
var go = AllTilesGameObjects[tile.X, tile.Y];
|
||||
OnTileUpdated(tile, go);
|
||||
};
|
||||
|
||||
|
||||
// wall creation
|
||||
for (int x = 0; x < Stage.Width; x++)
|
||||
for (int y = 0; y <= Stage.Height; y++)
|
||||
{
|
||||
var wall = Stage[WallOrientation.Horizontal, x, y];
|
||||
|
||||
GameObject wallh_go = new GameObject();
|
||||
HorizontalWallsGameObjects[x, y] = wallh_go;
|
||||
wallh_go.name = "hwall_" + x + "_" + y;
|
||||
wallh_go.transform.position = new Vector3(-0f+x, -0.5f+y, -1);
|
||||
wallh_go.transform.localScale = new Vector3(1f, 0.1f, 1);
|
||||
wallh_go.transform.SetParent(this.transform, true);
|
||||
|
||||
wallh_go.AddComponent<SpriteRenderer>();
|
||||
OnWallUpdated(wall, wallh_go);
|
||||
}
|
||||
for (int x = 0; x <= Stage.Width; x++)
|
||||
for (int y = 0; y < Stage.Height; y++)
|
||||
{
|
||||
var wall = Stage[WallOrientation.Vertical, x, y];
|
||||
|
||||
GameObject wallv_go = new GameObject();
|
||||
VerticalWallsGameObjects[x, y] = wallv_go;
|
||||
wallv_go.name = "vwall_" + x + "_" + y;
|
||||
wallv_go.transform.position = new Vector3(-0.5f + x, 0f + y, -1);
|
||||
wallv_go.transform.localScale = new Vector3(0.1f, 1f, 1);
|
||||
wallv_go.transform.SetParent(this.transform, true);
|
||||
|
||||
wallv_go.AddComponent<SpriteRenderer>();
|
||||
OnWallUpdated(wall, wallv_go);
|
||||
}
|
||||
|
||||
Stage.OnWallChanged = (Wall wall) =>
|
||||
{
|
||||
if(wall.Orientation == WallOrientation.Horizontal)
|
||||
{
|
||||
var go = HorizontalWallsGameObjects[wall.X, wall.Y];
|
||||
OnWallUpdated(wall, go);
|
||||
}
|
||||
|
||||
if (wall.Orientation == WallOrientation.Vertical)
|
||||
{
|
||||
var go = VerticalWallsGameObjects[wall.X, wall.Y];
|
||||
OnWallUpdated(wall, go);
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
float randomizeTileTimer = 2f;
|
||||
|
||||
// Update is called once per frame
|
||||
void Update()
|
||||
{
|
||||
if (Stage == null)
|
||||
return;
|
||||
|
||||
randomizeTileTimer -= Time.deltaTime;
|
||||
|
||||
if(randomizeTileTimer < 0f)
|
||||
{
|
||||
if(ShouldRandomize)
|
||||
Stage.Randomize();
|
||||
randomizeTileTimer += 2f;
|
||||
}
|
||||
}
|
||||
|
||||
public void FocusCurrentStage()
|
||||
{
|
||||
Stage = Train.GetWagon(FocusedWagon).GetDeck(FocusedLevel);
|
||||
}
|
||||
|
||||
private void OnTileUpdated(Tile tile, GameObject go)
|
||||
{
|
||||
Debug.Log($"Updating tile {tile.X},{tile.Y}.");
|
||||
if (tile.Type == TileType.Empty)
|
||||
{
|
||||
go.GetComponent<SpriteRenderer>().sprite = null;
|
||||
return;
|
||||
}
|
||||
|
||||
if (tile.Type == TileType.Floor)
|
||||
{
|
||||
go.GetComponent<SpriteRenderer>().sprite = FloorSprite;
|
||||
return;
|
||||
}
|
||||
|
||||
throw new System.NotImplementedException($"Tile type {tile.Type} not draw implemented");
|
||||
}
|
||||
|
||||
private void OnWallUpdated(Wall wall, GameObject go)
|
||||
{
|
||||
Debug.Log($"Updating wall {wall.X},{wall.Y}.");
|
||||
|
||||
if (wall.Type == WallType.Empty)
|
||||
{
|
||||
go.GetComponent<SpriteRenderer>().sprite = null;
|
||||
return;
|
||||
}
|
||||
|
||||
if (wall.Type == WallType.Full)
|
||||
{
|
||||
go.GetComponent<SpriteRenderer>().sprite = WallSprite;
|
||||
return;
|
||||
}
|
||||
|
||||
throw new System.NotImplementedException($"Wall type {wall.Type} not draw implemented");
|
||||
}
|
||||
}
|
||||
11
Assets/Controllers/StageController.cs.meta
Normal file
11
Assets/Controllers/StageController.cs.meta
Normal file
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: cef411a9d202a3b4b9b3e14f4b90ac58
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
Reference in New Issue
Block a user