mirror of
https://github.com/DerTyp7/fps-citybuild-unity.git
synced 2026-07-31 14:59:02 +02:00
Building Rework
This commit is contained in:
39
Assets/Buildings/Scripts/Building.cs
Normal file
39
Assets/Buildings/Scripts/Building.cs
Normal file
@@ -0,0 +1,39 @@
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
|
||||
public abstract class Building : MonoBehaviour
|
||||
{
|
||||
public string title = "New Building";
|
||||
public string description = "A cool new building";
|
||||
[SerializeField] private GameObject buildingPrefab;
|
||||
[SerializeField] private GameObject blueprintPrefab;
|
||||
|
||||
|
||||
public abstract void OnStartUp();
|
||||
public enum BuildingType
|
||||
{
|
||||
Housing,
|
||||
Storage,
|
||||
Decoration
|
||||
}
|
||||
|
||||
public BuildingType buildingType;
|
||||
|
||||
private void Start()
|
||||
{
|
||||
GameObject blueprint = Instantiate<GameObject>(blueprintPrefab);
|
||||
blueprint.transform.parent = gameObject.transform;
|
||||
|
||||
OnStartUp();
|
||||
}
|
||||
|
||||
public void Place(Transform t)
|
||||
{
|
||||
GameObject building = Instantiate<GameObject>(buildingPrefab);
|
||||
building.transform.position = t.position;
|
||||
building.transform.rotation = t.rotation;
|
||||
building.transform.parent = gameObject.transform;
|
||||
}
|
||||
|
||||
}
|
||||
11
Assets/Buildings/Scripts/Building.cs.meta
Normal file
11
Assets/Buildings/Scripts/Building.cs.meta
Normal file
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 5c2ebc4f7d4eb064b814863e80d9bab0
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
142
Assets/Buildings/Scripts/BuildingBlueprint.cs
Normal file
142
Assets/Buildings/Scripts/BuildingBlueprint.cs
Normal file
@@ -0,0 +1,142 @@
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
|
||||
public class BuildingBlueprint : MonoBehaviour
|
||||
{
|
||||
|
||||
public Material collisionMat;
|
||||
public Material blueprintMat;
|
||||
|
||||
|
||||
private GameObject terrain;
|
||||
private Canvas hud;
|
||||
private bool isColliding;
|
||||
|
||||
Ray ray;
|
||||
|
||||
private void Start()
|
||||
{
|
||||
hud = GameObject.Find("HUD").GetComponent<Canvas>(); //Get HUD Canvas
|
||||
terrain = GameObject.FindGameObjectWithTag("Terrain"); //Get Terrain
|
||||
|
||||
hud.enabled = false; //Hide HUD
|
||||
}
|
||||
|
||||
public void Update()
|
||||
{
|
||||
FollowMouse();
|
||||
Rotate();
|
||||
|
||||
//COLLISION
|
||||
if (isColliding)
|
||||
{
|
||||
Debug.Log("Collision");
|
||||
MeshRenderer[] mr = gameObject.GetComponentsInChildren<MeshRenderer>();
|
||||
foreach (MeshRenderer r in mr)
|
||||
{
|
||||
r.material = collisionMat;
|
||||
}
|
||||
|
||||
}
|
||||
else
|
||||
{
|
||||
MeshRenderer[] mr = gameObject.GetComponentsInChildren<MeshRenderer>();
|
||||
foreach (MeshRenderer r in mr)
|
||||
{
|
||||
r.material = blueprintMat;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
//PLACE
|
||||
if (Input.GetMouseButtonDown(0) && !isColliding)
|
||||
{
|
||||
gameObject.transform.parent.gameObject.GetComponent<Building>().Place(transform);
|
||||
hud.enabled = true;
|
||||
Destroy(gameObject);
|
||||
|
||||
}
|
||||
|
||||
if (Input.GetButtonDown("Build"))
|
||||
{
|
||||
hud.enabled = true;
|
||||
Destroy(gameObject);
|
||||
}
|
||||
}
|
||||
|
||||
void FollowMouse()
|
||||
{
|
||||
ray = Camera.main.ScreenPointToRay(Input.mousePosition);
|
||||
RaycastHit hitData;
|
||||
if (terrain.GetComponent<Collider>().Raycast(ray, out hitData, Mathf.Infinity))
|
||||
{
|
||||
transform.position = hitData.point;
|
||||
}
|
||||
}
|
||||
void Rotate()
|
||||
{
|
||||
if (Input.GetButtonDown("Rotate"))
|
||||
{
|
||||
Debug.Log("Rotate+");
|
||||
if (Input.GetKey(KeyCode.LeftShift))
|
||||
{
|
||||
transform.Rotate(0, 5, 0);
|
||||
}
|
||||
else if (Input.GetKey(KeyCode.LeftControl))
|
||||
{
|
||||
transform.Rotate(0, 45, 0);
|
||||
}
|
||||
else
|
||||
{
|
||||
transform.Rotate(0, 22.5f, 0);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
if (Input.GetButtonDown("CounterRotate"))
|
||||
{
|
||||
Debug.Log("Rotate-");
|
||||
if (Input.GetKey(KeyCode.LeftShift))
|
||||
{
|
||||
transform.Rotate(0, -5, 0);
|
||||
}
|
||||
else if (Input.GetKey(KeyCode.LeftControl))
|
||||
{
|
||||
transform.Rotate(0, -45, 0);
|
||||
}
|
||||
else
|
||||
{
|
||||
transform.Rotate(0, -22.5f, 0);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
//Collision
|
||||
public void OnTriggerEnter(Collider other)
|
||||
{
|
||||
if(!(other.transform.tag == "Terrain"))
|
||||
{
|
||||
isColliding = true;
|
||||
}
|
||||
|
||||
Debug.Log("Colliding True");
|
||||
}
|
||||
public void OnTriggerStay(Collider other)
|
||||
{
|
||||
if (!(other.transform.tag == "Terrain"))
|
||||
{
|
||||
isColliding = true;
|
||||
}
|
||||
Debug.Log("Colliding True");
|
||||
}
|
||||
public void OnTriggerExit(Collider other)
|
||||
{
|
||||
if (!(other.transform.tag == "Terrain"))
|
||||
{
|
||||
isColliding = false;
|
||||
}
|
||||
Debug.Log("Colliding False");
|
||||
}
|
||||
}
|
||||
14
Assets/Buildings/Scripts/BuildingBlueprint.cs.meta
Normal file
14
Assets/Buildings/Scripts/BuildingBlueprint.cs.meta
Normal file
@@ -0,0 +1,14 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 76f9b3b57e22ab047b8f95bcb552289a
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences:
|
||||
- constructionPrefab: {instanceID: 0}
|
||||
- collisionMat: {fileID: 2100000, guid: a1e8fb1ea637c0e45bed70dd7d1feaab, type: 2}
|
||||
- blueprintMat: {fileID: 2100000, guid: 78d3985cb7b88204b930cd05567c0c61, type: 2}
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
26
Assets/Buildings/Scripts/BuildingConstruction.cs
Normal file
26
Assets/Buildings/Scripts/BuildingConstruction.cs
Normal file
@@ -0,0 +1,26 @@
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
|
||||
public abstract class BuildingConstruction : MonoBehaviour
|
||||
{
|
||||
|
||||
public GameObject building;
|
||||
|
||||
public abstract bool CheckForResources();
|
||||
public abstract void Init();
|
||||
|
||||
private void Start()
|
||||
{
|
||||
Init();
|
||||
}
|
||||
|
||||
private void Update()
|
||||
{
|
||||
if (CheckForResources())
|
||||
{
|
||||
Instantiate(building, gameObject.transform.position, Quaternion.identity);
|
||||
Destroy(this.gameObject);
|
||||
}
|
||||
}
|
||||
}
|
||||
11
Assets/Buildings/Scripts/BuildingConstruction.cs.meta
Normal file
11
Assets/Buildings/Scripts/BuildingConstruction.cs.meta
Normal file
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 891ae650cdb2a6a438a2fd1bbb520db2
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
26
Assets/Buildings/Scripts/BuildingPlacement.cs
Normal file
26
Assets/Buildings/Scripts/BuildingPlacement.cs
Normal file
@@ -0,0 +1,26 @@
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
|
||||
public class BuildingPlacement : MonoBehaviour
|
||||
{
|
||||
[SerializeField] private GameObject terrain;
|
||||
[SerializeField] private GameObject prefab;
|
||||
|
||||
Ray ray;
|
||||
|
||||
void Update()
|
||||
{
|
||||
// Build Button Handler
|
||||
if (Input.GetButtonDown("Build"))
|
||||
{ // Wenn man den Button 'B'
|
||||
ray = Camera.main.ScreenPointToRay(Input.mousePosition);
|
||||
RaycastHit hitData;
|
||||
if (terrain.GetComponent<Collider>().Raycast(ray, out hitData, Mathf.Infinity))
|
||||
{
|
||||
Instantiate(prefab, hitData.point, Quaternion.identity);
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
11
Assets/Buildings/Scripts/BuildingPlacement.cs.meta
Normal file
11
Assets/Buildings/Scripts/BuildingPlacement.cs.meta
Normal file
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 2fe01a752f991734aa307cc7b7665015
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
8
Assets/Buildings/Scripts/Buildings.meta
Normal file
8
Assets/Buildings/Scripts/Buildings.meta
Normal file
@@ -0,0 +1,8 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 67b1c241d94696c409ad579573961232
|
||||
folderAsset: yes
|
||||
DefaultImporter:
|
||||
externalObjects: {}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
8
Assets/Buildings/Scripts/Buildings/House.meta
Normal file
8
Assets/Buildings/Scripts/Buildings/House.meta
Normal file
@@ -0,0 +1,8 @@
|
||||
fileFormatVersion: 2
|
||||
guid: e0c8f7012e4cce54bb00c3edeac83c65
|
||||
folderAsset: yes
|
||||
DefaultImporter:
|
||||
externalObjects: {}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,14 @@
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
|
||||
public class HouseBuildingScript : Building
|
||||
{
|
||||
public override void OnStartUp()
|
||||
{
|
||||
title = "House";
|
||||
description = "A place to live in";
|
||||
buildingType = BuildingType.Housing;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 255558bfba6648641822c24b4555e7db
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,26 @@
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
|
||||
public class HouseConstruction : BuildingConstruction
|
||||
{
|
||||
[Header("Needed Resources")]
|
||||
[SerializeField] private int neededWood = 10;
|
||||
|
||||
[Header("Having Resources")]
|
||||
[SerializeField] private int havingWood = 0;
|
||||
|
||||
public override void Init()
|
||||
{
|
||||
}
|
||||
|
||||
public override bool CheckForResources()
|
||||
{
|
||||
if (havingWood == neededWood)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: b49d2e0b5f1b960469b9dc34355fcc5f
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
8
Assets/Buildings/Scripts/Buildings/Warehouse.meta
Normal file
8
Assets/Buildings/Scripts/Buildings/Warehouse.meta
Normal file
@@ -0,0 +1,8 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 03d0b1e434e28de4f9f37f90abf6d095
|
||||
folderAsset: yes
|
||||
DefaultImporter:
|
||||
externalObjects: {}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,15 @@
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
|
||||
public class WarehouseBuilding : StorageBuilding
|
||||
{
|
||||
|
||||
private void Start()
|
||||
{
|
||||
title = "Warehouse";
|
||||
description = "A place to store your resources";
|
||||
inventorySpace = 500;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 8a48bc1459390b4459fbf54ad16b50f9
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,25 @@
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
|
||||
public class WarehouseConstruction : BuildingConstruction
|
||||
{
|
||||
[Header("Needed Resources")]
|
||||
[SerializeField] private int neededWood = 10;
|
||||
|
||||
[Header("Having Resources")]
|
||||
[SerializeField] private int havingWood = 0;
|
||||
|
||||
public override void Init()
|
||||
{
|
||||
}
|
||||
|
||||
public override bool CheckForResources()
|
||||
{
|
||||
if (havingWood == neededWood)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: c290e17b16e818841aabd5083a3475db
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
25
Assets/Buildings/Scripts/InfoSign.cs
Normal file
25
Assets/Buildings/Scripts/InfoSign.cs
Normal file
@@ -0,0 +1,25 @@
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
|
||||
public class InfoSign : Interactable
|
||||
{
|
||||
private Building parentBuilding;
|
||||
|
||||
private void Start()
|
||||
{
|
||||
parentBuilding = this.transform.parent.gameObject.GetComponent<Building>();
|
||||
}
|
||||
|
||||
|
||||
public override string GetDescription()
|
||||
{
|
||||
return "Press [E] to get <color=blue>info</color>.";
|
||||
}
|
||||
|
||||
public override void Interact()
|
||||
{
|
||||
Debug.Log("interact");
|
||||
}
|
||||
}
|
||||
|
||||
11
Assets/Buildings/Scripts/InfoSign.cs.meta
Normal file
11
Assets/Buildings/Scripts/InfoSign.cs.meta
Normal file
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 0b83755523bca294c8ebaacb9bfd6f02
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
8
Assets/Buildings/Scripts/Types.meta
Normal file
8
Assets/Buildings/Scripts/Types.meta
Normal file
@@ -0,0 +1,8 @@
|
||||
fileFormatVersion: 2
|
||||
guid: efcc2aca90f2a2b4eb81b54c475f3df4
|
||||
folderAsset: yes
|
||||
DefaultImporter:
|
||||
externalObjects: {}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
108
Assets/Buildings/Scripts/Types/StorageBuilding.cs
Normal file
108
Assets/Buildings/Scripts/Types/StorageBuilding.cs
Normal file
@@ -0,0 +1,108 @@
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
|
||||
public class StorageBuilding : Building
|
||||
{
|
||||
[SerializeField] private List<Item> inventory = new List<Item>();
|
||||
public int inventorySpace;
|
||||
|
||||
public override void OnStartUp()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
public void Awake()
|
||||
{
|
||||
buildingType = BuildingType.Storage;
|
||||
|
||||
}
|
||||
public void Add(Item item)
|
||||
{
|
||||
if(GetFreeSpace() >= item.count)
|
||||
{
|
||||
bool added = false;
|
||||
//Check if the Item can get stacked
|
||||
foreach (Item i in inventory)
|
||||
{
|
||||
if (i.uuid == item.uuid)
|
||||
{
|
||||
i.count += item.count;
|
||||
added = true;
|
||||
return;
|
||||
}
|
||||
added = false;
|
||||
|
||||
|
||||
}
|
||||
|
||||
//If foreach does not work just ADD (List is empty)
|
||||
if (!added)
|
||||
{
|
||||
inventory.Add(item);
|
||||
}
|
||||
|
||||
}
|
||||
else
|
||||
{
|
||||
Debug.Log("Inventory Full");
|
||||
}
|
||||
|
||||
//TODO mach wenn nicht ganz voll, dass dann so viele items added werden wie platz ist
|
||||
//Sonst wird bei 20 Holz KOMPLETT nein gesagt weil/obowhl 19 Space noch da ist
|
||||
}
|
||||
|
||||
public void Remove(Item item)
|
||||
{
|
||||
//Check if the Item can get stacked
|
||||
foreach (Item i in inventory)
|
||||
{
|
||||
if (i.uuid == item.uuid)
|
||||
{
|
||||
if(i.count > item.count)
|
||||
{
|
||||
i.count -= item.count;
|
||||
}else if(i.count <= item.count)
|
||||
{
|
||||
//!!!Muss eventuell später anders gehandelt werden!!!
|
||||
inventory.Remove(i); //Wenn du mehr entfernst als im Inventar ist, dann wird das Item einfach komplett removed
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public int GetCountOfItem(Item item)
|
||||
{
|
||||
int count = 0;
|
||||
foreach(Item i in inventory)
|
||||
{
|
||||
if(i.uuid == item.uuid)
|
||||
{
|
||||
count += i.count;
|
||||
}
|
||||
}
|
||||
return count;
|
||||
|
||||
}
|
||||
|
||||
public int GetUsedSpace()
|
||||
{
|
||||
int usedSpace = 0;
|
||||
|
||||
foreach(Item item in inventory)
|
||||
{
|
||||
usedSpace += item.count;
|
||||
}
|
||||
|
||||
return usedSpace;
|
||||
}
|
||||
|
||||
public int GetFreeSpace()
|
||||
{
|
||||
return inventorySpace - GetUsedSpace();
|
||||
}
|
||||
public List<Item> Getinventory()
|
||||
{
|
||||
return inventory;
|
||||
}
|
||||
}
|
||||
11
Assets/Buildings/Scripts/Types/StorageBuilding.cs.meta
Normal file
11
Assets/Buildings/Scripts/Types/StorageBuilding.cs.meta
Normal file
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 9cd3101a861849b4f97e2f4f499d7d46
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
Reference in New Issue
Block a user