WIP Power system

This commit is contained in:
Stedd 2022-09-18 19:55:54 +02:00
parent 13be4256ce
commit cea045ae2c
12 changed files with 283 additions and 27 deletions

View File

@ -1,4 +1,5 @@
using AsteroidGame.Entities.Structures.Scripts;
using AsteroidGame.Interfaces;
using UnityEngine;
namespace AsteroidGame.Entities.Structures.PowerPlant

View File

@ -54,6 +54,10 @@ MonoBehaviour:
cost: 0
buildPlacementBlocked: 0
buildTimer: 0
isPowerGenerator: 1
isPowerConsumer: 0
maxPower: 100
currentPower: 0
--- !u!1 &1863277996181035512
GameObject:
m_ObjectHideFlags: 0

View File

@ -1,10 +1,11 @@
using System;
using AsteroidGame.Interfaces;
using UnityEngine;
using UnityEngine.Serialization;
namespace AsteroidGame.Entities.Structures.Scripts
{
public class StructureBase : EntityBase, IBuildable
public class StructureBase : EntityBase, IBuildable, IPowerSystem
{
[Header("BuildParameters")]
[SerializeField]
@ -13,18 +14,33 @@ namespace AsteroidGame.Entities.Structures.Scripts
[SerializeField] private bool buildPlacementBlocked;
[SerializeField] protected float buildTimer;
[Header("Power")]
[SerializeField] protected bool isPowerGenerator;
[SerializeField] protected bool isPowerConsumer;
[SerializeField] protected int maxPower;
[SerializeField] protected int currentPower;
#region Private
#endregion
#region Publics
public bool IsPowerGenerator => isPowerGenerator;
public bool IsPowerConsumer => isPowerConsumer;
public bool BuildPlacementBlocked => buildPlacementBlocked;
public string Name { get; set; }
public int Cost { get; set; }
public int Cost => cost;
#endregion
private void OnEnable()
{
if (!isPowerConsumer && !isPowerGenerator)
{
Debug.Log("Power consumer/generator not set!");
}
}
private void OnTriggerStay(Collider other)
{
if(other.name == "BuildCollider")
@ -32,7 +48,7 @@ namespace AsteroidGame.Entities.Structures.Scripts
buildPlacementBlocked = true;
}
}
private void OnTriggerExit(Collider other)
{
if(other.name == "BuildCollider")
@ -40,5 +56,35 @@ namespace AsteroidGame.Entities.Structures.Scripts
buildPlacementBlocked = false;
}
}
#region Setters
public void SetMaxPower(int newValue)
{
maxPower = newValue;
}
#endregion
#region Getters
public int GetMaxPower()
{
return maxPower;
}
public int GetCurrentPower()
{
return currentPower;
}
public float GetPowerFactor()
{
// ReSharper disable once PossibleLossOfFraction
return currentPower / maxPower;
}
#endregion
}
}

View File

@ -1,5 +1,6 @@
using AsteroidGame.Entities.Enemies.Scripts;
using AsteroidGame.Entities.Structures.Scripts;
using AsteroidGame.Interfaces;
using UnityEngine;
namespace AsteroidGame.Entities.Structures.Tower

View File

@ -750,6 +750,10 @@ MonoBehaviour:
cost: 20
buildPlacementBlocked: 0
buildTimer: 1
isPowerGenerator: 0
isPowerConsumer: 1
maxPower: 10
currentPower: 0
weaponRange: 40
damage: 1
fireRate: 1

View File

@ -1,6 +1,7 @@
using System;
using System.Collections.Generic;
using AsteroidGame.Entities.Structures.Scripts;
using UnityEditor;
using UnityEngine;
using UnityEngine.InputSystem;
@ -17,14 +18,15 @@ namespace AsteroidGame.Handlers
[SerializeField] private bool isBuilding;
[SerializeField] private int buildingSelector;
[Header("Prefabs")]
[SerializeField] private List<StructureBase> buildings = new();
[Header("Structures")]
[SerializeField] private StructureBaseScriptableObject availableStructuresObject;
#region Private
private List<StructureBase> _availableStructures = new();
[SerializeField] private Color colorCurrent;
private Camera _camera;
[SerializeField] private List<StructureBase> activeBuildings;
[SerializeField] private List<StructureBase> activeStructures;
private Vector3 _tempVec;
private Plane _buildPlane;
private StructureBase _tempStructure;
@ -39,7 +41,8 @@ namespace AsteroidGame.Handlers
base.OnEnable();
_camera = Camera.main;
_buildPlane = new Plane(Vector3.up, Vector3.zero);
activeBuildings.Clear();
_availableStructures = availableStructuresObject.structureList;
activeStructures.Clear();
}
private void Update()
@ -100,7 +103,7 @@ namespace AsteroidGame.Handlers
private void SpawnGhostStructure()
{
_ghostStructure = Instantiate(buildings[buildingSelector], GetPlanePoint(), Quaternion.identity, transform);
_ghostStructure = Instantiate(_availableStructures[buildingSelector], GetPlanePoint(), Quaternion.identity, transform);
_ghostStructure.name = "GhostStructure";
var rb = _ghostStructure.gameObject.AddComponent<Rigidbody>();
@ -123,10 +126,12 @@ namespace AsteroidGame.Handlers
private void SpawnStructure()
{
_tempStructure = Instantiate(buildings[buildingSelector], GetPlanePoint(), Quaternion.identity, transform);
activeBuildings.Add(_tempStructure);
_tempStructure = Instantiate(_availableStructures[buildingSelector], GetPlanePoint(), Quaternion.identity, transform);
activeStructures.Add(_tempStructure);
}
#region Getters
private Vector3 GetPlanePoint()
{
Ray ray = _camera.ScreenPointToRay(Mouse.current.position.ReadValue());
@ -138,6 +143,18 @@ namespace AsteroidGame.Handlers
print("BuildPlaneNotHit");
return Vector3.zero;
}
public List<StructureBase> GetAvailableStructures()
{
return _availableStructures;
}
public List<StructureBase> GetActiveStructures()
{
return activeStructures;
}
#endregion
#region Setters

View File

@ -7,7 +7,8 @@
"GUID:f008ecc6829887e478aeb5eb004eb01b",
"GUID:f26d68a0bdefa1043b120b820f55e190",
"GUID:5041af1ee0cf75e4a9a52f5f23a0bfae",
"GUID:bc7863ca0989b494d84426bfd28432fa"
"GUID:bc7863ca0989b494d84426bfd28432fa",
"GUID:17a5862fcd6383b4b97bad4dcb1e2e5d"
],
"includePlatforms": [],
"excludePlatforms": [],

View File

@ -0,0 +1,75 @@
using System;
using System.Collections.Generic;
using AsteroidGame.Entities.Structures.Scripts;
using AsteroidGame.Interfaces;
using UnityEngine;
using UnityEngine.Pool;
namespace AsteroidGame.Handlers
{
public class PowerHandler : HandlerBase
{
[Header("State")] [SerializeField] private int powerConsumption;
[SerializeField] private int powerCapacity;
[SerializeField] private float powerFactor;
[Header("Connections")] [SerializeField]
private BuildingHandler buildingHandler;
// [SerializeField] private BuildingHandler buildingHandler;
#region Private
[SerializeField] private List<StructureBase> activeStructures = new();
#endregion
protected override void OnEnable()
{
base.OnEnable();
buildingHandler = FindObjectOfType<BuildingHandler>();
activeStructures = buildingHandler.GetActiveStructures();
}
private void Update()
{
powerConsumption = 0;
powerCapacity = 0;
foreach (var structure in activeStructures)
{
if (structure.IsPowerConsumer)
{
powerConsumption += structure.GetMaxPower();
}
if (structure.IsPowerGenerator)
{
powerCapacity += structure.GetMaxPower();
}
}
if (powerCapacity > 0)
{
powerFactor = (float)powerConsumption / (float)powerCapacity;
}
else
{
powerFactor = 0;
}
}
public int GetMaxPower()
{
throw new System.NotImplementedException();
}
public int GetCurrentPower()
{
throw new System.NotImplementedException();
}
public float GetPowerFactor()
{
throw new System.NotImplementedException();
}
}
}

View File

@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: bf720c524a2a9624099d0e4ba3d78108
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@ -0,0 +1,17 @@
namespace AsteroidGame.Interfaces
{
public interface IPowerSystem
{
public bool IsPowerGenerator { get; }
public bool IsPowerConsumer { get; }
public void SetMaxPower(int newValue);
public int GetMaxPower();
public int GetCurrentPower();
public float GetPowerFactor();
}
}

View File

@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: c3e9b410a74a04f4ab85e908d2a684a8
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@ -153,20 +153,59 @@ Transform:
m_Children:
- {fileID: 1047643964}
- {fileID: 1158682046}
- {fileID: 357927158}
m_Father: {fileID: 0}
m_RootOrder: 1
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
--- !u!1 &357927157
GameObject:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
serializedVersion: 6
m_Component:
- component: {fileID: 357927158}
- component: {fileID: 357927159}
m_Layer: 0
m_Name: PowerHandler
m_TagString: Untagged
m_Icon: {fileID: 0}
m_NavMeshLayer: 0
m_StaticEditorFlags: 0
m_IsActive: 1
--- !u!4 &357927158
Transform:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 357927157}
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: 38176946}
m_RootOrder: 2
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
--- !u!114 &309657227 stripped
--- !u!114 &357927159
MonoBehaviour:
m_CorrespondingSourceObject: {fileID: 5166195223278443568, guid: 57a75520298c47140a928041b05d7f3c, type: 3}
m_PrefabInstance: {fileID: 8451896670512076735}
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 0}
m_GameObject: {fileID: 357927157}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: b5b8b24a2cbe7294c90fb34afeca78e7, type: 3}
m_Script: {fileID: 11500000, guid: bf720c524a2a9624099d0e4ba3d78108, type: 3}
m_Name:
m_EditorClassIdentifier:
powerConsumption: 0
powerCapacity: 0
powerFactor: 0
buildingHandler: {fileID: 1158682047}
activeStructures: []
--- !u!1 &1047643963
GameObject:
m_ObjectHideFlags: 0
@ -267,11 +306,9 @@ MonoBehaviour:
ghostStructureMaterial: {fileID: 2100000, guid: dc919a35edbf85647939132e73b39642, type: 2}
isBuilding: 0
buildingSelector: 0
buildings:
- {fileID: 8787361557661825162, guid: 534f8d15e0c83c646887bebfda2bdfd6, type: 3}
- {fileID: 309657227}
availableStructuresObject: {fileID: 11400000, guid: f789f54c47873664284d6e8544724693, type: 2}
colorCurrent: {r: 0, g: 0, b: 0, a: 0}
activeBuildings: []
activeStructures: []
--- !u!1001 &1191794244
PrefabInstance:
m_ObjectHideFlags: 0
@ -391,6 +428,37 @@ Transform:
m_CorrespondingSourceObject: {fileID: 2692714622321691895, guid: 4af571b983b23f94f8d5ca4dbda27de5, type: 3}
m_PrefabInstance: {fileID: 1715656625}
m_PrefabAsset: {fileID: 0}
--- !u!1 &1819888405
GameObject:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
serializedVersion: 6
m_Component:
- component: {fileID: 1819888406}
m_Layer: 0
m_Name: GameObject
m_TagString: Untagged
m_Icon: {fileID: 0}
m_NavMeshLayer: 0
m_StaticEditorFlags: 0
m_IsActive: 1
--- !u!4 &1819888406
Transform:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 1819888405}
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: 0}
m_RootOrder: 6
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
--- !u!1 &2047541735
GameObject:
m_ObjectHideFlags: 0
@ -625,7 +693,7 @@ PrefabInstance:
objectReference: {fileID: 0}
- target: {fileID: 8324879816836607384, guid: 534f8d15e0c83c646887bebfda2bdfd6, type: 3}
propertyPath: m_RootOrder
value: 1
value: 4
objectReference: {fileID: 0}
- target: {fileID: 8324879816836607384, guid: 534f8d15e0c83c646887bebfda2bdfd6, type: 3}
propertyPath: m_LocalPosition.x
@ -703,7 +771,7 @@ PrefabInstance:
objectReference: {fileID: 0}
- target: {fileID: 7393565776164556299, guid: f1217267fdedfd74485c70ca348e1fcb, type: 3}
propertyPath: m_RootOrder
value: 5
value: 2
objectReference: {fileID: 0}
- target: {fileID: 7393565776164556299, guid: f1217267fdedfd74485c70ca348e1fcb, type: 3}
propertyPath: m_AnchorMax.x
@ -846,11 +914,11 @@ PrefabInstance:
m_Modifications:
- target: {fileID: 493861824998956378, guid: 57a75520298c47140a928041b05d7f3c, type: 3}
propertyPath: m_RootOrder
value: 4
value: 5
objectReference: {fileID: 0}
- target: {fileID: 493861824998956378, guid: 57a75520298c47140a928041b05d7f3c, type: 3}
propertyPath: m_LocalPosition.x
value: -4.71
value: -5.24
objectReference: {fileID: 0}
- target: {fileID: 493861824998956378, guid: 57a75520298c47140a928041b05d7f3c, type: 3}
propertyPath: m_LocalPosition.y
@ -858,7 +926,7 @@ PrefabInstance:
objectReference: {fileID: 0}
- target: {fileID: 493861824998956378, guid: 57a75520298c47140a928041b05d7f3c, type: 3}
propertyPath: m_LocalPosition.z
value: 1.66
value: 3.16
objectReference: {fileID: 0}
- target: {fileID: 493861824998956378, guid: 57a75520298c47140a928041b05d7f3c, type: 3}
propertyPath: m_LocalRotation.w