public void fireCannon(Unit cannon, Tile t) { Player you = cannon.getVillage ().controlledBy; Player them; if (t.getVillage () != null) { // stupid null checks to prevent errors them = t.getVillage ().controlledBy; } else { // check if neutral gameGUI.displayError (@"No need to fire on neutral land"); return; } if (you == them) { // dont fire on yourself gameGUI.displayError (@"Dont shoot yourself!!"); return; } else { // finally, give em hell! bool hasTower = t.checkTower (); Unit u = t.getOccupyingUnit(); Village v = t.getVillage (); Tile l = v.getLocatedAt(); if (hasTower == true){ // t.replace (null); t.networkView.RPC ("destroyPrefab",RPCMode.AllBuffered); t.networkView.RPC ("setStructureNet",RPCMode.AllBuffered,false); } if (u!=null){ // v.removeUnit(u); v.networkView.RPC ("removeUnitNet",RPCMode.AllBuffered,u.networkView.viewID); // t.setOccupyingUnit(null); t.networkView.RPC ("removeOccupyingUnitNet",RPCMode.AllBuffered); // t.replace (null); // t.networkView.RPC ("destroyPrefab",RPCMode.AllBuffered); // t.setLandType(LandType.Tombstone); t.gameObject.networkView.RPC ("setLandTypeNet",RPCMode.AllBuffered,(int)LandType.Tombstone); GameObject tomb = Network.Instantiate (tombPrefab, new Vector3 (t.point.x, 0.4f, t.point.y), tombPrefab.transform.rotation,0) as GameObject; t.networkView.RPC ("replaceTilePrefabNet",RPCMode.AllBuffered,tomb.networkView.viewID); // Destroy (u.gameObject); gameObject.networkView.RPC ("destroyUnitNet",RPCMode.AllBuffered,u.networkView.viewID); } if (t==l){ villageManager.takeCannonDamage(v); } } }
private bool canUnitMove(Unit u, Tile t) { // castle check foreach (Tile n in t.getNeighbours()) { try{ Village v = n.getVillage (); VillageType vt = v.getMyType(); Player them = v.getPlayer (); Player you = u.getVillage().getPlayer (); if (them!=you && vt==VillageType.Castle){ gameGUI.displayError (@"I cant even get near to their castle!"); return false; } } catch { continue; } } // friendly checks if (t.getVillage ()==null || t.getVillage ().controlledBy == u.getVillage ().controlledBy) { if((t.getLandType () == LandType.Trees || t.getLandType () == LandType.Tombstone) && (u.getUnitType() == UnitType.KNIGHT || u.getUnitType() == UnitType.CANNON)){ gameGUI.displayError (@"Knights are too fancy to do manual labour. ¯\(°_o)/¯"); return false; } else if (t.checkTower ()){ gameGUI.displayError (@"The tower doesn't want you to stand ontop of it. ¯\(°_o)/¯"); return false; } else if (t.getOccupyingUnit () != null) { gameGUI.displayError (@"There is a unit already standing there!!! ¯\(°_o)/¯"); return false; } else { return true; } // enemy checks } else if (t.getVillage ().controlledBy != u.getVillage ().controlledBy){ if (u.getUnitType()==UnitType.PEASANT){ gameGUI.displayError (@"Peasants cant attack! ¯\(°_o)/¯"); return false; } else if (u.getUnitType()==UnitType.CANNON){ gameGUI.displayError (@"Cannons cant move into enemy territory"); return false; } else if((t.getLandType () == LandType.Trees || t.getLandType () == LandType.Tombstone) && u.getUnitType() == UnitType.KNIGHT){ gameGUI.displayError (@"Knights are too fancy to do manual labour. ¯\(°_o)/¯"); return false; } else if (t.checkTower () == true && (u.getUnitType()!= UnitType.KNIGHT || u.getUnitType () != UnitType.SOLDIER)){ gameGUI.displayError (@"Only a soldiers and knights can destroy an enemy tower. ¯\(°_o)/¯"); return false; } else if (t.getOccupyingUnit()!=null && u.getUnitType()<=t.getOccupyingUnit().getUnitType()){ if (t.getOccupyingUnit().getUnitType()==UnitType.CANNON && u.getUnitType()<=UnitType.SOLDIER){ gameGUI.displayError (@"You need a knight to take out their cannon"); return false; } gameGUI.displayError (@"Your unit cant fight theirs. ¯\(°_o)/¯"); return false; } else { return true; } } //default return false; }
/* * Function adds village to invader. If the dest had a village prefab on it, then we take all resources * Already plundered! */ public void takeoverTile(Village invader, Tile dest) { Village invadedVillage = dest.getVillage (); // dest.setVillage (invader); dest.gameObject.networkView.RPC ("setVillageNet", RPCMode.AllBuffered, invader.gameObject.networkView.viewID); // invader.addTile(dest); invader.gameObject.networkView.RPC ("addTileNet", RPCMode.AllBuffered, dest.gameObject.networkView.viewID); // invadedVillage.removeTile(dest); invadedVillage.gameObject.networkView.RPC ("removeTileNet", RPCMode.AllBuffered, dest.gameObject.networkView.viewID); Debug.Log (invader.getPlayer ().getName ()+ " is invading "+invadedVillage.getPlayer ().getName ()); splitRegion(dest, invadedVillage); int color = invader.getPlayer().getColor(); dest.gameObject.networkView.RPC ("setAndColor",RPCMode.AllBuffered,color); }
public void moveUnit(Unit unit, Tile dest) { Village destVillage = dest.getVillage (); Village srcVillage = unit.getVillage (); Unit destUnit = dest.getOccupyingUnit (); UnitType srcUnitType = unit.getUnitType(); bool unitPermitted = canUnitMove (unit, dest); //if the move is allowed to move onto the tile if (unitPermitted == true) { Tile originalLocation = unit.getLocation (); // moving within your region if (srcVillage == destVillage) { performMove(unit,dest); originalLocation.gameObject.networkView.RPC ("removeOccupyingUnitNet",RPCMode.AllBuffered); //TODO STOPPED HERE } else if (srcVillage != destVillage) { // taking over neutral tiles if (destVillage == null) { //srcVillage.addTile(dest); srcVillage.gameObject.networkView.RPC ("addTileNet",RPCMode.AllBuffered,dest.gameObject.networkView.viewID); dest.gameObject.networkView.RPC ("setVillageNet",RPCMode.AllBuffered,srcVillage.gameObject.networkView.viewID); int color = srcVillage.getPlayer().getColor(); dest.gameObject.networkView.RPC ("setAndColor",RPCMode.AllBuffered,color); performMove(unit,dest); villageManager.MergeAlliedRegions(dest); unit.gameObject.networkView.RPC("setActionNet",RPCMode.AllBuffered,(int)UnitActionType.CapturingNeutral); originalLocation.gameObject.networkView.RPC ("removeOccupyingUnitNet",RPCMode.AllBuffered); } else if (srcUnitType == UnitType.PEASANT) { gameGUI.displayError (@"A peasant is too weak to invade!"); return; } else { // quit if tile is guarded by unit or tower bool guarded = tileManager.checkNeighboursForGuards(dest, unit); if (guarded){ gameGUI.displayError (@"That area is being protected"); return; } // unit on unit combat!! if (destUnit!=null){ if(srcUnitType>destUnit.getUnitType()){ // kill enemy unit, remove it from tile, remove it from village, perform move gets called after. destVillage.gameObject.networkView.RPC ("removeUnitNet",RPCMode.AllBuffered,destUnit.gameObject.networkView.viewID); dest.gameObject.networkView.RPC ("setOccupyingUnitNet",RPCMode.AllBuffered,unit.gameObject.networkView.viewID); gameObject.networkView.RPC ("destroyUnitNet",RPCMode.AllBuffered,destUnit.gameObject.networkView.viewID); } else { gameGUI.displayError (@"The enemy is too strong! I dont want to die!"); return; } } // if the tile contains the enemy village // pillage, then move the hovel if (destVillage.getLocatedAt()==dest){ if (srcUnitType > UnitType.INFANTRY){ // plunder village will handle stealing resources villageManager.plunderVillage (srcVillage, destVillage, dest); // it also calls respawn hovel and creating a meadow } else { gameGUI.displayError (@"This unit is too weak to plunder villages"); return; } } // destroy towers if (dest.checkTower() == true && srcUnitType>UnitType.INFANTRY){ //dest.setStructure(null); dest.gameObject.networkView.RPC ("setStructureNet",RPCMode.AllBuffered,false); //dest.replace (null); //dest.gameObject.networkView.RPC ("destroyPrefab",RPCMode.AllBuffered); } villageManager.takeoverTile(srcVillage,dest); //also splits region villageManager.MergeAlliedRegions(dest); performMove(unit,dest); unit.gameObject.networkView.RPC("setActionNet",RPCMode.AllBuffered,(int)UnitActionType.CapturingEnemy); originalLocation.gameObject.networkView.RPC ("removeOccupyingUnitNet",RPCMode.AllBuffered); } } } }
public void MergeAlliedRegions(Tile newTile) { Village myVillage = newTile.getVillage (); List<Tile> neighbours = newTile.getNeighbours(); //int mySize = myVillage.getRegionSize (); Player myPlayer = myVillage.getPlayer (); List<Village> villagesToMerge = new List<Village>(); villagesToMerge.Add (myVillage); Village biggestVillage = myVillage; //VillageType biggestType = biggestVillage.getMyType (); foreach (Tile neighbour in neighbours) { Village neighbourVillage = neighbour.getVillage (); if( neighbourVillage != null ) { Player neighbourPlayer = neighbourVillage.getPlayer (); if((myPlayer == neighbourPlayer) && !(villagesToMerge.Contains(neighbourVillage))) { villagesToMerge.Add(neighbourVillage); VillageType neighbourType = neighbourVillage.getMyType(); int neighbourSize = neighbourVillage.getRegionSize(); if (neighbourType>biggestVillage.getMyType()) { biggestVillage = neighbourVillage; } else if (neighbourType==biggestVillage.getMyType()&&neighbourSize>biggestVillage.getRegionSize()) { biggestVillage = neighbourVillage; } } } } foreach (Village village in villagesToMerge) { if (village != biggestVillage) { // biggestVillage.addGold (village.getGold ()); biggestVillage.gameObject.networkView.RPC ("addGoldNet",RPCMode.AllBuffered,village.getGold ()); // biggestVillage.addWood (village.getWood ()); biggestVillage.gameObject.networkView.RPC ("addWoodNet",RPCMode.AllBuffered,village.getGold ()); biggestVillage.addRegion(village.getControlledRegion ()); //foreach (Unit u in village.getControlledUnits ()){ // biggestVillage.addUnit (u); //} // remove prefab Tile villageLocation = village.getLocatedAt(); //Destroy (villageLocation.prefab); //villageLocation.gameObject.networkView.RPC ("destroyPrefab",RPCMode.AllBuffered); //villageLocation.setLandType (LandType.Meadow); myPlayer.gameObject.networkView.RPC("removeVillageNet",RPCMode.AllBuffered, village.gameObject.networkView.viewID, myPlayer.getColor ()); villageLocation.gameObject.networkView.RPC("setLandTypeNet",RPCMode.AllBuffered,(int)LandType.Meadow); GameObject meadow = Network.Instantiate (meadowPrefab, new Vector3 (villageLocation.point.x, 0.1f, villageLocation.point.y), meadowPrefab.transform.rotation,0) as GameObject; villageLocation.gameObject.networkView.RPC("replaceTilePrefabNet",RPCMode.AllBuffered,meadow.networkView.viewID); //villageLocation.prefab = Instantiate (meadowPrefab, new Vector3 (villageLocation.point.x, 0.1f, villageLocation.point.y), meadowPrefab.transform.rotation) as GameObject; //myPlayer.myVillages.Remove (village); //Destroy (village.gameObject.networkView.viewID); //gameObject.networkView.RPC ("destroyVillageNet",RPCMode.AllBuffered,village.gameObject.networkView.viewID); } } }