Skip to content

Commit

Permalink
Merge pull request samOtero#13 from samOtero/feat/create-tower-at-start
Browse files Browse the repository at this point in the history
feat: create towers at start add isBattling
  • Loading branch information
samOtero authored Jun 24, 2022
2 parents 18df6b6 + 56c4d1a commit f40507b
Show file tree
Hide file tree
Showing 8 changed files with 118 additions and 36 deletions.
47 changes: 45 additions & 2 deletions Assets/Scenes/SampleScene.unity
Original file line number Diff line number Diff line change
Expand Up @@ -744,7 +744,6 @@ MonoBehaviour:
m_EditorClassIdentifier:
PauseStatus: {fileID: 11400000, guid: a264ff026ea714e449ebd2932560c752, type: 2}
selectedTowerSpot: {fileID: 11400000, guid: 34412af143bd12a4288cccd10753a8fd, type: 2}
TowerTemplate: {fileID: 4656894821810783984, guid: 2ff6b753a129588428dc854d277c4939, type: 3}
StartTowerDrag: {fileID: 11400000, guid: 290c37b82dbd1364cacbd0d7cbb6ee27, type: 2}
EndTowerDrag: {fileID: 11400000, guid: 1d8a2629240e2e04cb241ae16ca48e87, type: 2}
DoTowerDrag: {fileID: 11400000, guid: d94db0c03fcafa345a9e9ef93caed2f7, type: 2}
Expand All @@ -755,7 +754,6 @@ MonoBehaviour:
currentParty: {fileID: 11400000, guid: 16f87b819b7da344b897b3fc9f583818, type: 2}
isDragging: 0
partyPosition: 0
needToCreate: 0
UnitGfxContainer: {fileID: 204961320}
--- !u!1 &565914756
GameObject:
Expand Down Expand Up @@ -1451,6 +1449,51 @@ CanvasRenderer:
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 1128518796}
m_CullTransparentMesh: 1
--- !u!1 &1294913646
GameObject:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
serializedVersion: 6
m_Component:
- component: {fileID: 1294913648}
- component: {fileID: 1294913647}
m_Layer: 0
m_Name: PartyManager
m_TagString: Untagged
m_Icon: {fileID: -964228994112308473, guid: 0000000000000000d000000000000000, type: 0}
m_NavMeshLayer: 0
m_StaticEditorFlags: 0
m_IsActive: 1
--- !u!114 &1294913647
MonoBehaviour:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 1294913646}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: 0ffa1052ba4fa9c4ea6faad795c3db28, type: 3}
m_Name:
m_EditorClassIdentifier:
TowerTemplate: {fileID: 4656894821810783984, guid: 2ff6b753a129588428dc854d277c4939, type: 3}
currentParty: {fileID: 11400000, guid: 16f87b819b7da344b897b3fc9f583818, type: 2}
--- !u!4 &1294913648
Transform:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 1294913646}
m_LocalRotation: {x: 0, y: 0, z: 0, w: 1}
m_LocalPosition: {x: -62.44, y: 12.07, z: -4.56}
m_LocalScale: {x: 1, y: 1, z: 1}
m_Children: []
m_Father: {fileID: 0}
m_RootOrder: 16
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
--- !u!1001 &1394603788
PrefabInstance:
m_ObjectHideFlags: 0
Expand Down
48 changes: 48 additions & 0 deletions Assets/Scripts/Level/PartyManager.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PartyManager : MonoBehaviour
{
public GameObject TowerTemplate;
public UnitParty currentParty;
// Start is called before the first frame update
void Start()
{
init();
}

private void init() {
Debug.Log("initializing party...");

// Loop through all the party spots and create a unit for each one
for(var i=0; i < currentParty.party.Count; i++) {
var container = currentParty.party[i];
if (container.unit) Destroy(container.unit);
container.unit = CreateTower(container.profile, i);
container.hasBeenCreated = true;
}

Debug.Log("party initialized");
}

// Create unit from a profile
private Unit CreateTower(UnitProfile profile, int partyPosition) {
var unitTemplate = TowerTemplate; //Would get which template we need from the profile, for now we just have only one
var newUnit = Instantiate(unitTemplate, transform);
var unitGfxName = UnitProfile.GetUnitGfxName(profile.unitID);
newUnit.name = "Tower_"+unitGfxName;

// Get graphic resource
var graphicResourceName = "unitGfx/"+unitGfxName;
var unitGfx = Object.Instantiate(Resources.Load(graphicResourceName), newUnit.transform) as GameObject;
unitGfx.name = "unitGfx";

//Set unit script
var unitScript = newUnit.GetComponent<Unit>();
unitScript.doInit(profile);
unitScript.partyPos = partyPosition;

return unitScript;
}
}
11 changes: 11 additions & 0 deletions Assets/Scripts/Level/PartyManager.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

32 changes: 0 additions & 32 deletions Assets/Scripts/UI/DragTower.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ public class DragTower : MonoBehaviour
{
public IntVariable PauseStatus;
public TowerSpotVariable selectedTowerSpot;
public GameObject TowerTemplate;
public IntEvent StartTowerDrag;
public BasicEvent EndTowerDrag;
public BasicEvent DoTowerDrag;
Expand All @@ -17,7 +16,6 @@ public class DragTower : MonoBehaviour
public UnitParty currentParty;
public bool isDragging;
public int partyPosition;
public bool needToCreate;
public GameObject UnitGfxContainer;
// Start is called before the first frame update
void Start()
Expand All @@ -37,7 +35,6 @@ private int onStartTowerDrag(int pos) {
if (currentParty.party != null && partyPosition < currentParty.party.Count) {
draggingUnit = currentParty.party[partyPosition].unit;
draggingUnitProfile = currentParty.party[partyPosition].profile;
needToCreate = !currentParty.party[partyPosition].hasBeenCreated;
rectTransform.anchoredPosition = Input.mousePosition / canvasScaleFactor;
UIUtil.setUnitGfx(draggingUnitProfile.unitID, UnitGfxContainer.transform); // Set drag Unit Graphic
PauseStatus.Value++;
Expand All @@ -51,39 +48,10 @@ private int onDragTower() {
return 1;
}

// Create unit from a profile
// TODO: Move this to it's own class
private Unit CreateTower(UnitProfile profile) {
var unitTemplate = TowerTemplate; //Would get which template we need from the profile, for now we just have only one
var newUnit = Instantiate(unitTemplate);
var unitGfxName = UnitProfile.GetUnitGfxName(profile.unitID);
newUnit.name = "Tower_"+unitGfxName;

// Get graphic resource
var graphicResourceName = "unitGfx/"+unitGfxName;
var unitGfx = Object.Instantiate(Resources.Load(graphicResourceName), newUnit.transform) as GameObject;
unitGfx.name = "unitGfx";

//Set unit script
var unitScript = newUnit.GetComponent<Unit>();
unitScript.doInit(profile);
unitScript.partyPos = partyPosition;

return unitScript;
}

private int onEndTowerDrag() {

// If we are dragging on top of a tower spot then add the unit to that spot
if (selectedTowerSpot.Value != null) {
// If we haven't instatiated a tower yet then create one
if (needToCreate) {
draggingUnit = CreateTower(draggingUnitProfile);
//Store the unit in the party reference
currentParty.party[partyPosition].unit = draggingUnit;
currentParty.party[partyPosition].hasBeenCreated = true;
needToCreate = false;
}
selectedTowerSpot.Value.AddUnit(draggingUnit);
}

Expand Down
1 change: 1 addition & 0 deletions Assets/Scripts/UI/TowerSpot.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ public void AddUnit(Unit newUnit) {
var newPos = transform.position;
newPos.y = 1.0f;
myUnit.positionRef.transform.position = newPos;
myUnit.setIsBattling(true); // When added to a spot the unit is now battling!
}

private void OnMouseEnter() {
Expand Down
3 changes: 2 additions & 1 deletion Assets/Scripts/Units/AttackingUnit.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ void Update()
}
if (attack1 != null) {
attack1.runCooldown(Time.deltaTime);
attack1.doAttack();
// Only try to attack if we are battling
if (myUnit.isBattling) attack1.doAttack();
}
}

Expand Down
11 changes: 10 additions & 1 deletion Assets/Scripts/Units/Unit.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ public class Unit : MonoBehaviour, ITargetable, IHasLife
public bool isEnemy;
public bool isAlive;
public bool isHidding;
public bool isBattling;
public int totalLife;
public int currentLife;

Expand Down Expand Up @@ -42,6 +43,9 @@ public void doInit(UnitProfile profile) {
totalLife = profile.baseHP;
attackSelected = profile.attackSelected;

// Unit is not battling yet
isBattling = false;

this.profile = profile;
// Add unit to it's respective list
if (isEnemy && EnemyList) EnemyList.Add(this);
Expand Down Expand Up @@ -113,6 +117,11 @@ public void doHide() {
positionRef.transform.position = new Vector3(1000.0f, 500.0f, 0);
}

// This is to be called when a unit is put into the battle field or removed
public void setIsBattling(bool isBattling) {
this.isBattling = isBattling;
}

public void doFade(float newFade) {
if (newFade >= 1) {
MaterialUtil.ToOpaqueMode(graphicMaterial);
Expand Down Expand Up @@ -176,7 +185,7 @@ public float getRotationFromDirection(Direction direction) {
}

public bool isTargetable() {
return isAlive && !isHidding;
return isAlive && !isHidding && isBattling;
}

public Vector3 getLocation() {
Expand Down
1 change: 1 addition & 0 deletions Assets/Scripts/Waves/LevelWave.cs
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ private void spawnEnemy(Unit whichUnit, Waypoint path) {
follower.reset(path);
}
whichUnit.Reset();
whichUnit.setIsBattling(true); // When enemy is spawned it is now battling!
}

public int UnitLeftLevelEvent(Unit whichUnit) {
Expand Down

0 comments on commit f40507b

Please sign in to comment.