Added placing buildings

This commit is contained in:
2026-03-31 15:58:43 +01:00
parent da83bb8dd5
commit e3b7716d7d
5 changed files with 153 additions and 3 deletions

View File

@@ -10,6 +10,8 @@ GameObject:
m_Component:
- component: {fileID: 546461529234046531}
- component: {fileID: 3321531399837876958}
- component: {fileID: 512124502862012575}
- component: {fileID: 2938597077917375806}
m_Layer: 0
m_Name: FanBuilding
m_TagString: Untagged
@@ -17,14 +19,13 @@ GameObject:
m_NavMeshLayer: 0
m_StaticEditorFlags: 0
m_IsActive: 1
--- !u!4 &546461529234046531
Transform:
--- !u!224 &546461529234046531
RectTransform:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 8624048914473773482}
serializedVersion: 2
m_LocalRotation: {x: 0, y: 0, z: 0, w: 1}
m_LocalPosition: {x: 0, y: 0, z: 0}
m_LocalScale: {x: 1, y: 1, z: 1}
@@ -32,6 +33,11 @@ Transform:
m_Children: []
m_Father: {fileID: 0}
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
m_AnchorMin: {x: 0.5, y: 0.5}
m_AnchorMax: {x: 0.5, y: 0.5}
m_AnchoredPosition: {x: 0, y: 0}
m_SizeDelta: {x: 100, y: 100}
m_Pivot: {x: 0.5, y: 0.5}
--- !u!114 &3321531399837876958
MonoBehaviour:
m_ObjectHideFlags: 0
@@ -46,3 +52,41 @@ MonoBehaviour:
m_EditorClassIdentifier:
BuildingTexture: {fileID: 2800000, guid: 1c2431a53994c424c82389d1cc422df8, type: 3}
<SizeOnGrid>k__BackingField: {x: 1, y: 3}
--- !u!222 &512124502862012575
CanvasRenderer:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 8624048914473773482}
m_CullTransparentMesh: 1
--- !u!114 &2938597077917375806
MonoBehaviour:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 8624048914473773482}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: fe87c0e1cc204ed48ad3b37840f39efc, type: 3}
m_Name:
m_EditorClassIdentifier:
m_Material: {fileID: 0}
m_Color: {r: 1, g: 1, b: 1, a: 1}
m_RaycastTarget: 1
m_RaycastPadding: {x: 0, y: 0, z: 0, w: 0}
m_Maskable: 1
m_OnCullStateChanged:
m_PersistentCalls:
m_Calls: []
m_Sprite: {fileID: 21300000, guid: 1c2431a53994c424c82389d1cc422df8, type: 3}
m_Type: 0
m_PreserveAspect: 0
m_FillCenter: 1
m_FillMethod: 4
m_FillAmount: 1
m_FillClockwise: 1
m_FillOrigin: 0
m_UseSpriteMesh: 0
m_PixelsPerUnitMultiplier: 1

View File

@@ -1306,6 +1306,7 @@ RectTransform:
- {fileID: 96288173}
- {fileID: 1330313875}
- {fileID: 1194686691}
- {fileID: 2005593404}
m_Father: {fileID: 0}
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
m_AnchorMin: {x: 0, y: 0}
@@ -1640,6 +1641,7 @@ MonoBehaviour:
RectTransform: {fileID: 1330313875}
BuildingPreview: {fileID: 1194686692}
CursorImage: {fileID: 1330313876}
BuildingManager: {fileID: 0}
BuildingPrefab: {fileID: 8624048914473773482, guid: 1b5c88b3f8d08674a9b15155d6ba9a36,
type: 3}
CurrentMouseClickStrength: 0
@@ -2008,6 +2010,54 @@ RectTransform:
m_AnchoredPosition: {x: 0, y: 1500}
m_SizeDelta: {x: 100, y: 100}
m_Pivot: {x: 0.5, y: 0.5}
--- !u!1 &2005593403
GameObject:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
serializedVersion: 6
m_Component:
- component: {fileID: 2005593404}
- component: {fileID: 2005593405}
m_Layer: 5
m_Name: BuildingManager
m_TagString: Untagged
m_Icon: {fileID: 0}
m_NavMeshLayer: 0
m_StaticEditorFlags: 0
m_IsActive: 1
--- !u!224 &2005593404
RectTransform:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 2005593403}
m_LocalRotation: {x: 0, y: 0, z: 0, w: 1}
m_LocalPosition: {x: 0, y: 0, z: 0}
m_LocalScale: {x: 1, y: 1, z: 1}
m_ConstrainProportionsScale: 0
m_Children: []
m_Father: {fileID: 1165634414}
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
m_AnchorMin: {x: 0.5, y: 0.5}
m_AnchorMax: {x: 0.5, y: 0.5}
m_AnchoredPosition: {x: 0, y: 0}
m_SizeDelta: {x: 100, y: 100}
m_Pivot: {x: 0.5, y: 0.5}
--- !u!114 &2005593405
MonoBehaviour:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 2005593403}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: 9b26cbb6d9a64e44ad6f1b2022ed62fe, type: 3}
m_Name:
m_EditorClassIdentifier:
--- !u!1 &2026916297
GameObject:
m_ObjectHideFlags: 0

View File

@@ -0,0 +1,46 @@
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
namespace Fruitomation.Game
{
public class BuildingManager : MonoBehaviour
{
private class BuildingInfo
{
public BuildingInfo(BuildingBase b, RectTransform rt)
{
Building = b;
Rect = rt;
}
public BuildingBase Building { get; }
public RectTransform Rect { get; }
}
private bool[,] InhabitedCells { get; } = new bool[96, 49];
private List<BuildingInfo> Buildings { get; } = new();
public bool AddBuildingAt(Vector2Int position, GameObject prefab, bool updateCellMap = false)
{
GameObject go = Instantiate(prefab, transform);
BuildingInfo info = new
(
go.GetComponent<BuildingBase>(),
go.GetComponent<RectTransform>()
);
Vector2Int p0 = position * 40;
Vector2Int p1 = p0 + new Vector2Int(20, 100);
Vector2 off0 = info.Building.SizeOnGrid - Vector2.one;
Vector2 off1 = off0 * 20;
info.Rect.sizeDelta = info.Building.SizeOnGrid * 40;
info.Rect.anchoredPosition = p1 + off1;
Buildings.Add(info);
return true;
}
}
}

View File

@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 9b26cbb6d9a64e44ad6f1b2022ed62fe
timeCreated: 1774967593

View File

@@ -19,6 +19,7 @@ namespace Fruitomation.UI
[SerializeField] private RectTransform RectTransform;
[SerializeField] private Image BuildingPreview;
[SerializeField] private Image CursorImage;
[SerializeField] private BuildingManager BuildingManager;
[Header("Dev")]
[SerializeField] private GameObject BuildingPrefab;
@@ -156,6 +157,12 @@ namespace Fruitomation.UI
Vector2 o1 = o0 * 20;
Vector2 p4 = p3 + o1;
BuildingPreview.rectTransform.anchoredPosition = p4;
//
if (Input.GetMouseButtonDown(0))
{
BuildingManager.AddBuildingAt(p1, BuildingPrefab);
}
}
}
}