diff --git a/Assets/Base.blend b/Assets/Base.blend index 2deda5c106..9c6e2a99d4 100644 Binary files a/Assets/Base.blend and b/Assets/Base.blend differ diff --git a/Assets/Base.prefab b/Assets/Base.prefab index c6605a4231..01de23aa09 100644 Binary files a/Assets/Base.prefab and b/Assets/Base.prefab differ diff --git a/Assets/BaseScript.js b/Assets/BaseScript.js index fa5f2de24f..ca171e1d3d 100644 --- a/Assets/BaseScript.js +++ b/Assets/BaseScript.js @@ -14,13 +14,15 @@ var unit_creation_waypoint : Vector2; private var current_construction : String; private var construction_in_progress : boolean = false; private var construction_progress : float = 0; -private var world_position : Vector2; +private var world_position : Vector2 = Vector2(-1,-1); var MAX_UNITS_PER_BASE = 15; var CONSTRUCTION_TIME : float = 2; var UNIT_DROP_X : float = 0; var UNIT_DROP_Y : float = 0; +var target_position : Vector2; + function Start () { // There are some limitations to this function wrt transform @@ -77,6 +79,26 @@ function InitializeBase() function SetPosition(x : int, y : int) { world_position = new Vector2(x,y); + target_position = new Vector2(x,y); +} + +function GetPosition() : Vector2 +{ + return world_position; +} + +function SetTarget(x : int, y : int) +{ + target_position = new Vector2(x,y); +} + +function Selected(selected : boolean) : boolean +{ + // TODO: Do a Selected animation + + // TODO: React to being selected in some way + + return true; } function SetBaseType(type : String) @@ -97,17 +119,17 @@ function SetBaseType(type : String) } } -function GetBaseType() +function GetBaseType() : String { return baseType; } -function GetForkerCount() +function GetForkerCount() : int { return forkerCount; } -function GetBrancherCount() +function GetBrancherCount() : int { return brancherCount; } @@ -116,7 +138,7 @@ function SetPlayerType(isPlayer : boolean) { player = isPlayer; } -function IsPlayer() +function IsPlayer() : boolean { return player; } @@ -127,7 +149,7 @@ function SetBaseColor(color : Color) gameObject.transform.GetChild(0).renderer.material.color = color; } -function FormatUnitName(unit : String) +function FormatUnitName(unit : String) : String { return String.Format("{0}_{1}_{2}", unit, @@ -139,8 +161,9 @@ function CreateUnit(unit : String) { var name : String = FormatUnitName(unit); var modifier : int = 1; + var newUnit : Transform; - if(15 > gameObject.transform.position.x) + if(15 > world_position.y) { modifier *= -1; } @@ -151,42 +174,56 @@ function CreateUnit(unit : String) switch(unit) { case "forker": - CreateForker(UNIT_DROP_X, UNIT_DROP_Y, name); + newUnit = CreateForker(UNIT_DROP_X, UNIT_DROP_Y, name); break; case "brancher": - CreateBrancher(UNIT_DROP_X, UNIT_DROP_Y, name); + newUnit = CreateBrancher(UNIT_DROP_X, UNIT_DROP_Y, name); break; } - var unitScript = GameObject.Find(name).GetComponent(UnitScript); + var unitScript : UnitScript = newUnit.GetComponent(UnitScript); unitScript.SetTeam(player); unitScript.SetHomeBase(gameObject.transform); - unitScript.SetPosition(UNIT_DROP_X,UNIT_DROP_Y); } +/* function SetupPiece(piece : Transform, x : int, y : int, z : float, name : String) { GameObject.Find("HexPlain").GetComponent(HexBoardScript).SetupPiece(piece, x, y, z, name); } +*/ -function CreateForker(x : int, y : int, name : String) +function MoveTo(piece : Transform, x : int, y : int) +{ + GameObject.Find(String.Format("HexPlain/_{0}_{1}_", x, y)).GetComponent(CellScript).MoveTo(piece); +} + +function CreateForker(x : int, y : int, name : String) : Transform { var forkerClone : Transform = Instantiate(forker); + forkerClone.name = name; - SetupPiece(forkerClone, x, y, z_placement, name); + /*SetupPiece(forkerClone, x, y, z_placement, name);*/ + MoveTo(forkerClone, x, y); forkerCount++; number_of_forkers_alive++; + + return forkerClone; } -function CreateBrancher(x : int, y : int, name : String) +function CreateBrancher(x : int, y : int, name : String) : Transform { var brancherClone : Transform = Instantiate(brancher); + brancherClone.name = name; - SetupPiece(brancherClone, x, y, z_placement, name); + /*SetupPiece(brancherClone, x, y, z_placement, name);*/ + MoveTo(brancherClone, x, y); brancherCount++; number_of_branchers_alive++; + + return brancherClone; } function UnitDeath(unit : String) diff --git a/Assets/Branch.prefab b/Assets/Branch.prefab index 4eddf1f5ec..1ffa8359fb 100644 Binary files a/Assets/Branch.prefab and b/Assets/Branch.prefab differ diff --git a/Assets/BranchBush.prefab b/Assets/BranchBush.prefab index b9adab2ec2..c51f80ccac 100644 Binary files a/Assets/BranchBush.prefab and b/Assets/BranchBush.prefab differ diff --git a/Assets/BushScript.js b/Assets/BushScript.js index f73d71fb76..4b7d179d87 100644 --- a/Assets/BushScript.js +++ b/Assets/BushScript.js @@ -3,7 +3,7 @@ var branch : Transform; private var branchCount : int = 4; -private var world_position : Vector2; +private var world_position : Vector2 = Vector2(-1,-1); function Start () { @@ -11,6 +11,15 @@ function Start () function Update () { + if(0 >= branchCount) + { + Destroy(gameObject); + } +} + +function Selected(selected : boolean) : boolean +{ + return false; } function SetPosition(x : int, y : int) @@ -18,20 +27,23 @@ function SetPosition(x : int, y : int) world_position = new Vector2(x,y); } -function CreateBranch(x : int, y : int) +function GetPosition() : Vector2 { - var name : String = String.Format("branch_{0}", branchCount); - var branchClone : Transform = Instantiate(branch); - - GameObject.Find("HexPlain").GetComponent(HexBoardScript).SetupPiece(branchClone, x, y, 2, name); - - GameObject.Find(name).GetComponent(PickupScript).SetPosition(x,y); - branchCount++; + return world_position; } -function TakeBranch() +function GetBranch(x : int, y : int) : Transform { - branchCount--; + var branch : Transform = null; + + if(0 < branchCount) + { + var name : String = String.Format("branch_{0}", branchCount); + branchCount--; + branch = Instantiate(branch); + } + + return branch; } function GetBranchCount() @@ -39,17 +51,16 @@ function GetBranchCount() return branchCount; } -function GetCellScript() +function GetCellScript() : CellScript { return GameObject.Find(String.Format("HexPlain/_{0}_{1}_", world_position.x, world_position.y)).GetComponent(CellScript); } - -function Target() +function Targeted(selection : Transform) { - GetCellScript().Targeted(true); } -function UnTarget() + +function DoSelectedGUI(rect : Rect) { - GetCellScript().Targeted(false); } + diff --git a/Assets/CamControl.js b/Assets/CamControl.js index 928d76c482..cc256d8ec4 100644 --- a/Assets/CamControl.js +++ b/Assets/CamControl.js @@ -8,11 +8,11 @@ var htMax : int = 12; var htMin : int = 4; var camOrigin : GameObject; -public var currentSelection : GameObject; // hold Fire1-selected -private var originalColorOfSelected : Color; -public var targetedSelection : GameObject; // hold Fire2-selected -private var originalColorOfTargeted : Color; -public var mouseoverTarget : GameObject; +public var currentSelection : Transform; // hold Fire1-selected +//private var originalColorOfSelected : Color; +public var targetedSelection : Transform; // hold Fire2-selected +//private var originalColorOfTargeted : Color; +public var mouseoverTarget : Transform; function Start () { @@ -45,6 +45,7 @@ function SetPosition(position : Vector3) var y : float = camPos.y; camPos = position; camPos.y = y; + gameObject.transform.localPosition = camPos; } function GetSelectedGUIRect() @@ -56,7 +57,7 @@ function OnGUI() { if(null != currentSelection) { - if(currentSelection.name.Contains("Base")) + if(currentSelection.name.Contains("base")) { currentSelection.GetComponent(BaseScript).DoSelectedGUI(GetSelectedGUIRect()); } @@ -72,12 +73,12 @@ function OnGUI() } } -/* -function Mouseover(mouseoverTarget : Transform) +function Mouseover(mouseover : Transform) { - mouseoverTarget.GetComponent(CellScript).OnMouseOver(); + ClearCurrentMouseover(); + mouseoverTarget = mouseover; + mouseoverTarget.GetComponent(CellScript).MousedOver(); } -*/ function DoMousePicking() { @@ -86,33 +87,40 @@ function DoMousePicking() var hit : RaycastHit; var layerMask : LayerMask = -1; - //layerMask = 1 << LayerMask.NameToLayer("GridCells"); - //Mouseover(hit.transform); + layerMask = 1 << LayerMask.NameToLayer("GridCells"); if(Input.GetButtonDown("Fire1")) { - layerMask = 1 << LayerMask.NameToLayer("Forkers") | + //Debug.Log("Fire2 pressed"); + /*layerMask = 1 << LayerMask.NameToLayer("Forkers") | 1 << LayerMask.NameToLayer("Branchers") | - 1 << LayerMask.NameToLayer("Buildings"); + 1 << LayerMask.NameToLayer("Buildings");*/ // Check if we hit a selectable object like a unit or building if(Physics.Raycast(ray, hit, Mathf.Infinity, layerMask)) { - SelectObject(hit.transform.gameObject); + SelectObject(hit.transform); } } - - if((null != currentSelection) && (Input.GetButtonUp("Fire2"))) + else if((null != currentSelection) && (Input.GetButtonUp("Fire2"))) { //Debug.Log("Fire2 pressed"); - layerMask = 1 << LayerMask.NameToLayer("Selectable") | + /*layerMask = 1 << LayerMask.NameToLayer("Selectable") | 1 << LayerMask.NameToLayer("Trees") | - 1 << LayerMask.NameToLayer("Buildings"); + 1 << LayerMask.NameToLayer("Buildings");*/ // Operate on targetedSelection accordingly if(Physics.Raycast(ray, hit, Mathf.Infinity, layerMask)) { - TargetObject(hit.transform.gameObject); + TargetObject(hit.transform); + } + } + else + { + // Check if we hit a selectable object like a unit or building + if(Physics.Raycast(ray, hit, Mathf.Infinity, layerMask)) + { + Mouseover(hit.transform); } } } @@ -121,23 +129,34 @@ function ClearCurrentSelection() { if(null != currentSelection) { - currentSelection.renderer.material.color = originalColorOfSelected; + //currentSelection.renderer.material.color = originalColorOfSelected; + currentSelection.GetComponent(CellScript).Selected(false); currentSelection = null; } } -/* + function ClearCurrentTargeted() { if(null != targetedSelection) { - targetedSelection.renderer.material.color = originalColorOfTargeted; + //targetedSelection.renderer.material.color = originalColorOfTargeted; + targetedSelection.GetComponent(CellScript).Targeted(true); targetedSelection = null; } } -*/ -function SelectObject(selected : GameObject) + +function ClearCurrentMouseover() +{ + if(null != mouseoverTarget) + { + mouseoverTarget.GetComponent(CellScript).MouseExited(); + mouseoverTarget = null; + } +} + +function SelectObject(selected : Transform) { ClearCurrentSelection(); //ClearCurrentTargeted(); @@ -146,13 +165,18 @@ function SelectObject(selected : GameObject) //originalColorOfSelected = currentSelection.renderer.material.color; //currentSelection.renderer.material.color = Color.magenta; + currentSelection.GetComponent(CellScript).Selected(true); } -function TargetObject(targeted : GameObject) +function TargetObject(targeted : Transform) { - //ClearCurrentTargeted(); + ClearCurrentTargeted(); targetedSelection = targeted; //originalColorOfTargeted = targetedSelection.renderer.material.color; //targetedSelection.renderer.material.color = Color.green; + + var targetPos : Vector2 = targetedSelection.GetComponent(CellScript).GetPosition(); + currentSelection.GetComponent(CellScript).Target(targetPos.x, targetPos.y); + } diff --git a/Assets/CellScript.js b/Assets/CellScript.js index e5cd2f676b..6eb7de3cf1 100644 --- a/Assets/CellScript.js +++ b/Assets/CellScript.js @@ -1,18 +1,23 @@ #pragma strict -//#pragma downcast + +var base : Transform; +var branch_bush : Transform; private var inhabitant : Transform; private var inhabitant_occupied : boolean = false; var inhabitant_pos : Vector2 = new Vector2(0, 0); -private var branch_bush : Transform; -private var branch_bush_occupied : boolean = false; -var branch_bush_pos : Vector2 = new Vector2(.5, -.5); + +/* +private var bush : Transform; +private var bush_occupied : boolean = false; +var bush_pos : Vector2 = new Vector2(.5, -.5); private var item_drop_a : Transform; private var item_drop_a_occupied : boolean = false; var item_drop_a_pos : Vector2 = new Vector2(-.5, .5); private var item_drop_b : Transform; private var item_drop_b_occupied : boolean = false; var item_drop_b_pos : Vector2 = new Vector2(-.5, -.5); +*/ // This table corresponds to the one in UnitScript.js regarding HEADING private var NUMBER_OF_NEIGHBORS : int = 6; @@ -72,28 +77,9 @@ private var mouseoverHighlightOn : boolean = false; private var mouseoverHighlightColor : Color = Color.yellow; private var mouseoverHighlightMod : float = 0.5; -private var inhabitants = {}; - -/* -// General Factory -function makeStruct(names) -{ - var names = names.split(','); - var count = names.length; - - function constructor() { - for(var i=0; i < count; i++) - { - this[names[i]] = arguments[i]; - } - } - - return constructor; -} +private var show_failed_target : boolean = false; -// Make a Cell "Inhabitant" class/struct -var Inhabitant = makeStruct("transform,occupied,position"); -*/ +private var selectable : boolean = false; function Start () { @@ -102,9 +88,7 @@ function Start () { var delimiter : String = "_"; var coordStrings = gameObject.name.Split(delimiter.ToCharArray()); myLocation = new Vector2(parseInt(coordStrings[1]), parseInt(coordStrings[2])); - - // Set neighbors - SetNeighbors(); + Debug.Log(String.Format("Cell: {0}",myLocation)); // Particle system off initially ToggleParticleSystem(false); @@ -130,42 +114,128 @@ function Update () } } -function OnMouseOver() +function GetPosition() : Vector2 +{ + return myLocation; +} + +function MousedOver() { mouseoverHighlightOn = true; } -function OnMouseExit() +function MouseExited() { mouseoverHighlightOn = false; mouseoverHighlightMod = 0.5; ToggleParticleSystem(false); } -function Targeted(val : boolean) +function Target(x : int, y : int) { + var piece : Transform = GetInhabitant(); - targetedHighlightOn = val; - targetedHighlightMod = 0.5; - targetedHighlightTimediff = 0; - - if(!targetedHighlightOn) + if(selectable) { + if(piece.name.Contains("forker") || piece.name.Contains("brancher")) + { + piece.GetComponent(UnitScript).SetTarget(x,y); + } + else if(piece.name.Contains("base")) + { + piece.GetComponent(BaseScript).SetTarget(x,y); + } + } +} + +function Targeted(val : boolean) +{ + if(false == val) + { + targetedHighlightOn = false; + targetedHighlightMod = 0.5; ToggleParticleSystem(false); } + else + { + if(SlotInhabited()) + { + targetedHighlightOn = val; + targetedHighlightMod = 0.5; + targetedHighlightTimediff = 0; + } + } } function Selected(val : boolean) { - selectedHighlightOn = val; - selectedHighlightMod = 0.5; - - if(!selectedHighlightOn) + if(false == val) { + selectedHighlightOn = false; + selectedHighlightMod = 0.5; ToggleParticleSystem(false); } + else + { + if(SlotInhabited()) + { + var piece : Transform = GetInhabitant(); + + // Determine if the object here can even be selected so as to ignore further stimulus + if(piece.name.Contains("forker") || piece.name.Contains("brancher")) + { + selectable = piece.GetComponent(UnitScript).Selected(val); + } + else if(piece.name.Contains("base")) + { + selectable = piece.GetComponent(BaseScript).Selected(val); + } + else if(piece.name.Contains("bush")) + { + selectable = piece.GetComponent(BushScript).Selected(val); + } + else if(piece.name.Contains("branch") || piece.name.Contains("fork") || piece.name.Contains("fortification")) + { + selectable = piece.GetComponent(PickupScript).Selected(val); + } + + if(selectable) + { + selectedHighlightOn = true; + selectedHighlightMod = 0.5; + } + } + } +} + + +function DoSelectedGUI(rect : Rect) +{ + var piece : Transform = GetInhabitant(); + + if(selectable) + { + if(piece.name.Contains("forker") || piece.name.Contains("brancher")) + { + piece.GetComponent(UnitScript).DoSelectedGUI(rect); + } + else if(piece.name.Contains("base")) + { + piece.GetComponent(BaseScript).DoSelectedGUI(rect); + } + else if(piece.name.Contains("bush")) + { + piece.GetComponent(BushScript).DoSelectedGUI(rect); + } + else if(piece.name.Contains("branch") || piece.name.Contains("fork") || piece.name.Contains("fortification")) + { + piece.GetComponent(PickupScript).DoSelectedGUI(rect); + } + } } + + function SetPathHighlight(val : boolean, type : String) { pathHighlightOn = val; @@ -192,7 +262,7 @@ function SetPathHighlight(val : boolean, type : String) } } -function GetParticleSystem() +function GetParticleSystem() : ParticleSystem { return gameObject.GetComponent(ParticleSystem); } @@ -212,7 +282,7 @@ function ToggleParticleSystem(on : boolean) } } -function FadeColor(color : Color, mod : float, speed : int) +function FadeColor(color : Color, mod : float, speed : int) : float { var direction : int = 100 > mod ? 1 : -1; @@ -234,7 +304,7 @@ function DoMouseoverHighlight() function DoTargetedHighlight() { - targetedHighlightTimediff = FadeColor(targetedHighlightColor, targetedHighlightMod, 10); + targetedHighlightTimediff += FadeColor(targetedHighlightColor, targetedHighlightMod, 10); if(targetedHighlightTimediff >= 2) { @@ -252,77 +322,64 @@ function DoPathHighlight() FadeColor(pathHighlightColor, pathHighlightMod, 3); } - - -function GetMapSize() +function PositionValid(x : int, y : int) : boolean { - return GameObject.Find("HexPlain").GetComponent(HexBoardScript).GetSize(); -} - -function PositionValid(x : int, y : int) -{ - if((0 > x) || (GetMapSize().x < x) || - (0 > y) || (GetMapSize().y < y)) + var size : Vector2 = GameObject.Find("HexPlain").GetComponent(HexBoardScript).GetSize(); + + if((0 > x) || (size.x < x) || + (0 > y) || (size.y < y)) return false; return true; } -function GetNeighbor(neighbor : int) +function GetNeighbor(neighbor : int) : Vector2 { var neighborPos : Vector2 = Vector2(-1,-1); switch(neighbor) { case NEIGHBOR_AHEAD: - neighborPos = NEIGHBOR_AHEAD_POS; + if(PositionValid(myLocation.x+1, myLocation.y)) + neighborPos = Vector2(myLocation.x+1, myLocation.y); break; case NEIGHBOR_BEHIND: - neighborPos = NEIGHBOR_BEHIND_POS; + if(PositionValid(myLocation.x-1, myLocation.y)) + neighborPos = Vector2(myLocation.x-1, myLocation.y); break; case NEIGHBOR_LEFT: - neighborPos = NEIGHBOR_LEFT_POS; + if(PositionValid(myLocation.x, myLocation.y-1)) + neighborPos = Vector2(myLocation.x, myLocation.y-1); break; case NEIGHBOR_RIGHT: - neighborPos = NEIGHBOR_RIGHT_POS; + if(PositionValid(myLocation.x, myLocation.y+1)) + neighborPos = Vector2(myLocation.x, myLocation.y+1); break; case NEIGHBOR_BACK_LEFT: - neighborPos = NEIGHBOR_BACK_LEFT_POS; + if(PositionValid(myLocation.x-1, myLocation.y-1)) + neighborPos = Vector2(myLocation.x-1, myLocation.y-1); break; case NEIGHBOR_BACK_RIGHT: - neighborPos = NEIGHBOR_BACK_RIGHT_POS; + if(PositionValid(myLocation.x-1, myLocation.y+1)) + neighborPos = Vector2(myLocation.x-1, myLocation.y+1); break; } return neighborPos; } -function SetNeighbors() -{ - var invalidPos = Vector2(-1,-1); - - /* Ahead */ - NEIGHBOR_AHEAD_POS = PositionValid(myLocation.x+1, myLocation.y) ? invalidPos : Vector2(myLocation.x+1, myLocation.y); - /* Behind */ - NEIGHBOR_BEHIND_POS = PositionValid(myLocation.x-1, myLocation.y) ? invalidPos : Vector2(myLocation.x-1, myLocation.y); - /* Left */ - NEIGHBOR_LEFT_POS = PositionValid(myLocation.x, myLocation.y-1) ? invalidPos : Vector2(myLocation.x, myLocation.y-1); - /* Right */ - NEIGHBOR_RIGHT_POS = PositionValid(myLocation.x, myLocation.y+1) ? invalidPos : Vector2(myLocation.x, myLocation.y+1); - /* Back-Left */ - NEIGHBOR_BACK_LEFT_POS = PositionValid(myLocation.x-1, myLocation.y-1) ? invalidPos : Vector2(myLocation.x-1, myLocation.y-1); - /* Back-Right */ - NEIGHBOR_BACK_RIGHT_POS = PositionValid(myLocation.x-1, myLocation.y+1) ? invalidPos : Vector2(myLocation.x-1, myLocation.y+1); -} -function GetInhabitant(slot : String) +function GetInhabitant(/*slot : String*/) : Transform { var piece : Transform; - if(SlotInhabitated(slot)) + + /* + if(SlotInhabited(slot)) { if("inhabitant" == slot) - { + {*/ piece = inhabitant; + /* } else if("drop_a" == slot) { @@ -334,18 +391,21 @@ function GetInhabitant(slot : String) } else if("bush" == slot) { - piece = branch_bush; + piece = bush; } - } + }*/ - return null; + return piece; } -function SlotInhabitated(slot : String) +function SlotInhabited(/*slot : String*/) : boolean { + /* if("inhabitant" == slot) { + */ return inhabitant_occupied; + /* } else if("drop_a" == slot) { @@ -357,19 +417,23 @@ function SlotInhabitated(slot : String) } else if("bush" == slot) { - return branch_bush_occupied; + return bush_occupied; } + return true; + */ } -function SlotPos(slot : String) +function SlotPos(/*slot : String*/) : Vector2 { var slotPosition : Vector2 = new Vector2(-1,-1); + /* if("inhabitant" == slot) - { + {*/ slotPosition = inhabitant_pos; + /* } else if("drop_a" == slot) { @@ -381,89 +445,211 @@ function SlotPos(slot : String) } else if("bush" == slot) { - slotPosition = branch_bush_pos; - } + slotPosition = bush_pos; + }*/ return slotPosition; } // I broke this out because its reused a few times. -function PositionInhabitant(slot : String, new_inhabitant : Transform) +function PositionInhabitant(/*slot : String, */new_inhabitant : Transform, z : float) { - var z = new_inhabitant.localPosition.z; + //var z = new_inhabitant.localPosition.z; new_inhabitant.parent = gameObject.transform; - new_inhabitant.localPosition = new Vector3(SlotPos(slot)[0], SlotPos(slot)[1], z); + new_inhabitant.localPosition = Vector3(SlotPos(/*slot*/)[0], SlotPos(/*slot*/)[1], z); } -function SetInhabitant(type : String, new_inhabitant : Transform) +function ClearInhabitant(/*slot : String*/) +{ +/* + if("inhabitant" == slot) + {*/ + inhabitant_occupied = false; + inhabitant = null; + /* + } + else if("drop_a" == slot) + { + item_drop_a_occupied = false; + } + else if("drop_b" == slot) + { + item_drop_b_occupied = false; + } + else if("bush" == slot) + { + bush_occupied = false; + }*/ +} + + +function SetInhabitant(/*type : String, */new_inhabitant : Transform) : boolean { - var result = true; + var result : boolean = true; + var position : Vector2; - switch(type) + if( !SlotInhabited(/*"inhabitant"*/) ) { - case "unit": - if(false == SlotInhabitated("inhabitant")) - { - PositionInhabitant("inhabitant", new_inhabitant); - } - else + if(new_inhabitant.name.Contains("forker") || new_inhabitant.name.Contains("brancher")) + { + position = new_inhabitant.GetComponent(UnitScript).GetPosition(); + + if(PositionValid(position.x, position.y)) { - // Toggle some UI to depict a failed attempt to position here + // MoveFrom previous position + GameObject.Find(String.Format("/HexPlain/_{0}_{1}_", position.x, position.y)).GetComponent(CellScript).MoveFrom(new_inhabitant); } - break; + + // MoveTo this position + PositionInhabitant(/*"inhabitant", */new_inhabitant, 3.0); - case "pickup": - if(false == SlotInhabitated("drop_a")) - { - PositionInhabitant("drop_a", new_inhabitant); - } - else if(false == SlotInhabitated("drop_b")) - { - PositionInhabitant("drop_b", new_inhabitant); - } - else + new_inhabitant.GetComponent(UnitScript).SetPosition(myLocation.x, myLocation.y); + } + else if(new_inhabitant.name.Contains("base")) + { + position = new_inhabitant.GetComponent(BaseScript).GetPosition(); + + if(PositionValid(position.x, position.y)) { - // Toggle some UI to depict a failed attempt to position here + // MoveFrom previous position + GameObject.Find(String.Format("/HexPlain/_{0}_{1}_", position.x, position.y)).GetComponent(CellScript).MoveFrom(new_inhabitant); } - break; - - case "bush": - if(false == SlotInhabitated("bush")) + + // MoveTo this position + PositionInhabitant(/*"inhabitant", */new_inhabitant, 3.0); + + new_inhabitant.GetComponent(BaseScript).SetPosition(myLocation.x, myLocation.y); + } + else if(new_inhabitant.name.Contains("bush")) + { + position = new_inhabitant.GetComponent(BushScript).GetPosition(); + + if(PositionValid(position.x, position.y)) { - PositionInhabitant("bush", new_inhabitant); + // MoveFrom previous position + GameObject.Find(String.Format("/HexPlain/_{0}_{1}_", position.x, position.y)).GetComponent(CellScript).MoveFrom(new_inhabitant); } - else + + // MoveTo this position + PositionInhabitant(/*"inhabitant", */new_inhabitant, 3.0); + + new_inhabitant.GetComponent(BushScript).SetPosition(myLocation.x, myLocation.y); + } + else if(new_inhabitant.name.Contains("branch") || new_inhabitant.name.Contains("fork") || new_inhabitant.name.Contains("fortification")) + { + position = new_inhabitant.GetComponent(PickupScript).GetPosition(); + + if(PositionValid(position.x, position.y)) { - // Toggle some UI to depict a failed attempt to position here + // MoveFrom previous position + GameObject.Find(String.Format("/HexPlain/_{0}_{1}_", position.x, position.y)).GetComponent(CellScript).MoveFrom(new_inhabitant); } - break; - default: - result = false; + + // MoveTo this position + PositionInhabitant(/*"inhabitant", */new_inhabitant, 3.0); + + new_inhabitant.GetComponent(PickupScript).SetPosition(myLocation.x, myLocation.y); + } + else + { + return false; + } + } + else + { + // Toggle some UI to depict a failed attempt to position here } return result; } +function MoveTo(piece : Transform) : boolean +{ + var result : boolean = true; + + /* Name must be set before this point */ + Debug.Log(String.Format("MoveTo {0}", myLocation)); + + SetInhabitant(piece); + + return result; +} -function MoveTo(piece : Transform) +function MoveFrom(piece : Transform) : boolean { var result : boolean = true; + Debug.Log(String.Format("MoveFrom {0}",myLocation)); + + /* if(piece.name.Contains("forker") || piece.name.Contains("brancher")) { - SetInhabitant("unit", piece); + */ + + if(SlotInhabited(/*"inhabitant"*/) && GetInhabitant(/*"inhabitant"*/).name.Equals(piece.name)) + { + ClearInhabitant(/*"inhabitant"*/); + } + else + { + result = false; + } + + /* } else if(piece.name.Contains("branch") || piece.name.Contains("fork") || piece.name.Contains("fortification")) { - SetInhabitant("pickup", piece); + if(SlotInhabited("drop_a") && GetInhabitant("drop_a").name.Equals(piece.name)) + { + ClearInhabitant("drop_a"); + } + else if(SlotInhabited("drop_b") && GetInhabitant("drop_b").name.Equals(piece.name)) + { + ClearInhabitant("drop_b"); + } } else if(piece.name.Contains("branchbush")) { - SetInhabitant("bush", piece); + ClearInhabitant("bush"); } - else + else if(piece.name.Contains("base")) { - result = false; + ClearInhabitant("base"); + }*/ + + return result; +} + +function CreateBush(id : int) : boolean +{ + var result : boolean = false; + + var bushClone : Transform = Instantiate(branch_bush); + bushClone.name = String.Format("bush_{0}", id); + + result = MoveTo(bushClone); + if(result) + { + bushClone.GetComponent(BushScript).SetPosition(myLocation.x, myLocation.y); + } + + return result; +} + +function CreateBase(name : String, player : boolean, baseType : String) : boolean +{ + var result : boolean = false; + + var baseClone : Transform = Instantiate(base); + baseClone.name = name; + + result = MoveTo(baseClone); + if(result) + { + var baseScript : BaseScript = baseClone.GetComponent(BaseScript); + baseScript.SetBaseType(baseType); + baseScript.SetPlayerType(player); + baseScript.SetPosition(myLocation.x, myLocation.y); } return result; diff --git a/Assets/Fork.prefab b/Assets/Fork.prefab index 075e614d14..ac81028a1b 100644 Binary files a/Assets/Fork.prefab and b/Assets/Fork.prefab differ diff --git a/Assets/Fortification.prefab b/Assets/Fortification.prefab index a5f2f9b457..112f2d6426 100644 Binary files a/Assets/Fortification.prefab and b/Assets/Fortification.prefab differ diff --git a/Assets/HexBoardScript.js b/Assets/HexBoardScript.js index 35400489d1..2b90b6abec 100644 --- a/Assets/HexBoardScript.js +++ b/Assets/HexBoardScript.js @@ -14,8 +14,6 @@ private var PLAYER_TYPE_BRANCHER = "brancher"; private var PLAYER_TYPE_BOTH = "both"; // public -var base : Transform; -var branch_bush : Transform; var init_number_of_bushes : int; var z_placement : float; var groundTexture1 : Texture2D; @@ -82,7 +80,7 @@ function ToggleCamControl(val : boolean) function GetSize() { - return new Vector2(hi_index, hi_index); + return Vector2(hi_index, hi_index); } function ToggleEscapeMenu() @@ -249,8 +247,49 @@ function CreateGameScene() } } +function PositionValid(position : Vector2) +{ + if(0 > position.x || GetSize().x < position.x || + 0 > position.y || GetSize().y < position.y) + return false; + + return true; +} +function GetCellScript(x,y) +{ + return GameObject.Find(String.Format("HexPlain/_{0}_{1}_", x, y)).GetComponent(CellScript); +} + +function CreateBush(x : int, y : int) +{ + var result : boolean = false; + + if(PositionValid(Vector2(x,y))) + { + result = GetCellScript(x,y).CreateBush(bushesPlaced); + if(result) + { + bushesPlaced++; + } + } + + return result; +} + +function CreateBase(x : int, y : int, name : String, player : boolean, baseType : String) +{ + var result : boolean = false; + if(PositionValid(Vector2(x,y))) + { + result = GetCellScript(x,y).CreateBase(name, player, baseType); + } + + return result; +} + +/* function SetupPiece(piece : Transform, x : int, y : int, z : float, name : String) { if(0 > x || 0 > y) @@ -265,36 +304,13 @@ function SetupPiece(piece : Transform, x : int, y : int, z : float, name : Strin piece.transform.localPosition = new Vector3(0,0, piece.transform.localPosition.z); } } +*/ function MoveTo(piece : Transform, x : int, y : int) { GameObject.Find(String.Format("HexPlain/_{0}_{1}_", x, y)).GetComponent(CellScript).MoveTo(piece); } -function CreateBush(x : int, y : int) -{ - var name : String = String.Format("branchbush_{0}", bushesPlaced); - var bushClone : Transform = Instantiate(branch_bush); - - SetupPiece(bushClone, x, y, 3, name); - - GameObject.Find(name).GetComponent(BushScript).SetPosition(x,y); - bushesPlaced++; -} - -function CreateBase(x : int, y : int, name : String, player : boolean, baseType : String) -{ - var baseClone : Transform = Instantiate(base); - SetupPiece(baseClone, x, y, z_placement, name); - baseClone.localPosition.x -= 0.25; - baseClone.localPosition.y -= 0.25; - - var baseScript : BaseScript = GameObject.Find(name).GetComponent(BaseScript); - baseScript.SetBaseType(baseType); - baseScript.SetPlayerType(player); - baseScript.SetPosition(x,y); -} - function CreateGameboard() { var tex : Texture2D = null; @@ -332,21 +348,21 @@ function CreateGameboard() { case PLAYER_TYPE_FORKER: //Create a base. - CreateBase(playerPos.x, playerPos.y, "PlayerBase", true, PLAYER_TYPE_FORKER); + CreateBase(playerPos.x, playerPos.y, "player_base", true, PLAYER_TYPE_FORKER); //Create a base. - CreateBase(opponentPos.x, opponentPos.y, "OpponentBase", false, PLAYER_TYPE_BRANCHER); + CreateBase(opponentPos.x, opponentPos.y, "opponent_base", false, PLAYER_TYPE_BRANCHER); break; case PLAYER_TYPE_BRANCHER: //Create a base. - CreateBase(playerPos.x, playerPos.y, "PlayerBase", true, PLAYER_TYPE_BRANCHER); + CreateBase(playerPos.x, playerPos.y, "player_base", true, PLAYER_TYPE_BRANCHER); //Create a base. - CreateBase(opponentPos.x, opponentPos.y, "OpponentBase", false, PLAYER_TYPE_FORKER); + CreateBase(opponentPos.x, opponentPos.y, "opponent_base", false, PLAYER_TYPE_FORKER); break; case PLAYER_TYPE_BOTH: //Create a base. - CreateBase(playerPos.x, playerPos.y, "PlayerBase", true, PLAYER_TYPE_BOTH); + CreateBase(playerPos.x, playerPos.y, "player_base", true, PLAYER_TYPE_BOTH); //Create a base. - CreateBase(opponentPos.x, opponentPos.y, "OpponentBase", false, PLAYER_TYPE_BOTH); + CreateBase(opponentPos.x, opponentPos.y, "opponent_base", false, PLAYER_TYPE_BOTH); break; } diff --git a/Assets/HexPlain.prefab b/Assets/HexPlain.prefab index 01dd416a2a..0e4198239b 100644 Binary files a/Assets/HexPlain.prefab and b/Assets/HexPlain.prefab differ diff --git a/Assets/PickupScript.js b/Assets/PickupScript.js index f8a84cdd33..155eb05663 100644 --- a/Assets/PickupScript.js +++ b/Assets/PickupScript.js @@ -1,7 +1,7 @@ #pragma strict private var condition : int = 100; -private var world_position : Vector2; +private var world_position : Vector2 = Vector2(-1,-1); function Start () { @@ -16,7 +16,12 @@ function SetPosition(x : int, y : int) world_position = new Vector2(x,y); } -function GetCondition() +function GetPosition() : Vector2 +{ + return world_position; +} + +function GetCondition() : int { return condition; } @@ -24,4 +29,18 @@ function GetCondition() function Damage(dmg : int) { condition -= dmg; +} + +function Targeted(selection : Transform) +{ +} + +function Selected(selected : boolean) : boolean +{ + // TODO: Could always add some GUI later, but for now, not selectable + return false; +} + +function DoSelectedGUI(rect : Rect) +{ } \ No newline at end of file diff --git a/Assets/UnitScript.js b/Assets/UnitScript.js index 9d3938ee4f..680a813281 100644 --- a/Assets/UnitScript.js +++ b/Assets/UnitScript.js @@ -28,19 +28,16 @@ private var fortificationCount : int = 0; // States and Status private var newly_created : boolean = true; private var branches_held : int = 0; -private var world_position : Vector2; +private var world_position : Vector2 = Vector2(-1,-1); private var in_hand : Transform = null; +private var selected : boolean = false; private var action_points = ACTION_POINTS_PER_TURN; private var target_position : Vector2; // Animate variables private var newly_created_rotation = 0; -// MouseOver // -private var startcolor : Color; - - function Start () { // Determine the unit-type @@ -77,12 +74,12 @@ function SetPosition(x : int, y : int) RotateToHeading(); } -function GetPosition() +function GetPosition() : Vector2 { return world_position; } -function GetUnitType() +function GetUnitType() : String { return unitType; } @@ -107,26 +104,28 @@ function DoNewUnitAnimation() } } +/* function SetupPiece(piece : Transform, x : int, y : int, z : float, name : String) { GameObject.Find("HexPlain").GetComponent(HexBoardScript).SetupPiece(piece, x, y, z, name); } +*/ -function CreateFork() +function CreateFork() : Transform { var forkClone : Transform = Instantiate(fork); forkCount++; return forkClone; } -function CreateFortification() +function CreateFortification() : Transform { var fortificationClone : Transform = Instantiate(fortification); fortificationCount++; return fortificationClone; } -function FormatUnitName(item : String) +function FormatUnitName(item : String) : String { return String.Format("{0}_{1}", item, @@ -145,33 +144,35 @@ function SetHeading(newHeading : int) RotateToHeading(); } -function GetHeading() +function GetHeading() : int { return heading; } -function GetCellScript() +function GetCellScript(x : int, y : int) : CellScript { - return GameObject.Find(String.Format("HexPlain/_{0}_{1}_", world_position.x, world_position.y)).GetComponent(CellScript); + var pos : Vector2 = new Vector2((x == -1) ? world_position.x : x, (y == -1) ? world_position.y : y); + + return GameObject.Find(String.Format("HexPlain/_{0}_{1}_", pos.x, pos.y)).GetComponent(CellScript); } -function GetNeighbor(direction : int) +function GetNeighbor(direction : int) : Vector2 { var neighborDir = (heading + direction); neighborDir = ((neighborDir-180)%360) + (neighborDir < 180 ? 180 : -180); neighborDir = (neighborDir == -180) ? 180 : neighborDir; Debug.Log(neighborDir); - var neighborPos : Vector2 = GetCellScript().GetNeighbor(neighborDir); + var neighborPos : Vector2 = GetCellScript(world_position.x, world_position.y).GetNeighbor(neighborDir); return neighborPos; } -function GetMapSize() +function GetMapSize() : Vector2 { return GameObject.Find("HexPlain").GetComponent(HexBoardScript).GetSize(); } -function PositionValid(position : Vector2) +function PositionValid(position : Vector2) : boolean { if(0 > position.x || GetMapSize().x < position.x || 0 > position.y || GetMapSize().y < position.y) @@ -180,17 +181,38 @@ function PositionValid(position : Vector2) return true; } -function TossItem(item : Transform, toss_direction : int) +function TossItem(item : Transform) : boolean { - var neighboringPos : Vector2 = GetNeighbor(toss_direction); - SetupPiece(item, neighboringPos.x, neighboringPos.y, item_placement, item.name); + /*SetupPiece(item, neighboringPos.x, neighboringPos.y, item_placement, item.name);*/ + + var result : boolean = false; + + if(false == GetCellScript(world_position.x, world_position.y).MoveTo(item)) + { + var current_heading = HEADING_BACK; + var neighbor = GetNeighbor(current_heading); + + while(false == GetCellScript(neighbor.x, neighbor.y).MoveTo(item)) + { + current_heading += HEADING_RIGHT; + + if((-1 * HEADING_BACK) <= current_heading) + { + break; + } + + neighbor = GetNeighbor(current_heading); + } + } + + return result; } function GraspItem(item : Transform) { if(null != in_hand) { - TossItem(in_hand, HEADING_RIGHT); + TossItem(in_hand); } item.parent = gameObject.transform; @@ -207,7 +229,7 @@ function GraspItem(item : Transform) item.localScale = Vector3(0.2,0.2,0.2); } - item.GetComponent(PickupScript).SetPosition(GetPosition().x,GetPosition().y); + item.GetComponent(PickupScript).SetPosition(GetPosition().x, GetPosition().y); } function CreateItem() @@ -239,7 +261,7 @@ function SetTeam(teamPlayer : boolean) team_player = teamPlayer; } -function GetForkCount() +function GetForkCount() : int { return forkCount; } @@ -252,7 +274,7 @@ function Kill() } -function GetFortificationCount() +function GetFortificationCount() : int { return fortificationCount; } @@ -276,6 +298,15 @@ function DoSelectedGUI(rect : Rect) GUILayout.EndArea(); } +function Selected(selected : boolean) : boolean +{ + // TODO: Do a Selected animation + + // TODO: React to being selected in some way + + return true; +} + function FindPathTo(x : int, y : int) { // Recursively highlight diff --git a/Assets/game_scene.unity b/Assets/game_scene.unity index aba461d4b1..308c46001f 100644 Binary files a/Assets/game_scene.unity and b/Assets/game_scene.unity differ