void OnInputFieldSubmit(string inputText) { if (File.Exists(inputText)) { string jsonText = ""; using (StreamReader sr = new StreamReader(inputText)){ jsonText = sr.ReadToEnd(); } BattleHistory battleHistory = new BattleHistory(); battleHistory.FromJSON(jsonText); Game.StartBattle(battleHistory); } }
IEnumerator StartBattleInternal(BattleHistory battleHistory) { AsyncOperation asyncOperation = SceneManager.LoadSceneAsync("Battle scene"); yield return(new WaitUntil(() => asyncOperation.isDone)); GameObject go = GameObject.Find("Battle"); Battle battle = go.GetComponent <Battle>(); battle.Init(this, battleHistory); #if UNITY_EDITOR EditorGUIUtility.PingObject(battle); Selection.objects = new Object[] { battle.gameObject }; #endif }
void Start() { Object.DontDestroyOnLoad(this.gameObject); Random.InitState(0451); if (this.loadMapEditorInstead) { SceneManager.LoadScene("Map editor"); } else { Scenario scenario; { string dir = "Scenarios/"; string jsonText = ""; using (StreamReader sr = new StreamReader(dir + this.scenarioToLoad)){ jsonText = sr.ReadToEnd(); } scenario = JsonUtility.FromJson <Scenario>(jsonText); } BattleMap map; { string dir = "Maps/"; string jsonText = ""; using (StreamReader sr = new StreamReader(dir + scenario.map)){ jsonText = sr.ReadToEnd(); } map = JsonUtility.FromJson <BattleMap>(jsonText); } BattleHistory battleHistory = new BattleHistory(); battleHistory.scenario = scenario; battleHistory.startingMap = map; battleHistory.currentTeamIndex = scenario.startingTeamIndex; battleHistory.moves = new List <object>(); Game.StartBattle(battleHistory); } }
public static List <Country> Battle(List <Country> countries) { using (var context = new ApplicationDbContext()) { foreach (var c in countries) { foreach (var a in c.Assaults) { Country aim = a.Target; if (aim != c) { List <Army> standing = aim.StandingForce.ToList <Army>(); float attackBonus = 1; if (c.Researches.FirstOrDefault(m => m.Technology.Name == "Operation Rebirth") != null) { attackBonus += 0.2f; } if (c.Researches.FirstOrDefault(m => m.Technology.Name == "Tactics") != null) { attackBonus += 0.1f; } float defenseBonus = 1; if (aim.Researches.FirstOrDefault(m => m.Technology.Name == "City walls") != null) { defenseBonus += 0.2f; } if (aim.Researches.FirstOrDefault(m => m.Technology.Name == "Tactics") != null) { defenseBonus += 0.1f; } int attackPower = 0; foreach (var f in a.Forces) { attackPower += f.Size * f.Type.Attack; } int defensivePower = 0; foreach (var f in aim.StandingForce) { defensivePower += f.Size * f.Type.Defense; } attackPower = (int)(attackPower * attackBonus); defensivePower = (int)(defensivePower * defenseBonus); string result; int wonPotato = 0; int wonGold = 0; string losses = ""; if (attackPower > defensivePower) { result = "Won"; wonPotato = aim.Potato / 2; wonGold = aim.Gold / 2; } else if (attackPower == defensivePower) { result = "Tie"; } else { result = "Lost"; } if (attackPower > defensivePower) { c.Gold += aim.Gold / 2; aim.Gold -= aim.Gold / 2; c.Potato += aim.Potato / 2; aim.Potato -= aim.Potato / 2; foreach (var lost in aim.StandingForce.ToList <Army>()) { lost.Size = (int)(lost.Size * 0.9f); } losses = " nothing."; } else { foreach (var lost in a.Forces.ToList <Force>()) { int lostForces = (int)(lost.Size * 0.9f); lost.Size = (int)(lost.Size * 0.9f); losses += lost.Type.Name + " " + lostForces + " "; } } BattleHistory battle = new BattleHistory() { Attacker = c.Name, Defender = aim.Name, Result = result, WonGold = wonPotato, WonPotato = wonGold, Losses = losses, Attack = attackPower, Defense = defensivePower, Turn = context.Game.First(m => m.Id == 1).Turn }; context.History.Add(battle); context.SaveChanges(); } } } return(countries); } }
public static void StartBattle(BattleHistory battleHistory) { IEnumerator coroutine = instance.StartBattleInternal(battleHistory); instance.StartCoroutine(coroutine); }
public void Init(Game game, BattleHistory battleHistory) { this.game = game; // Copy the history, except for the moves; they'll be applied later. this.history = battleHistory; List <object> moveHistory = battleHistory.moves; this.history.moves = new List <object>(); Scenario scenario = this.history.scenario; BattleMap map = this.history.startingMap; this.mapDisplay.Init(this, map.size); // Build misc objects. { // Let us know when anything on the UI is clicked (and not handled by a button or something). EventTrigger eventTrigger = this.uiRefs.canvas.gameObject.AddComponent <EventTrigger>(); var entry = new EventTrigger.Entry(); entry.eventID = EventTriggerType.PointerClick; entry.callback.AddListener((data) => { var button = ((PointerEventData)data).button; if (button == PointerEventData.InputButton.Left) { this.MouseEvent(null, MapDisplay.MouseEventType.Click); } else if (button == PointerEventData.InputButton.Right) { this.MouseEvent(null, MapDisplay.MouseEventType.RightClick); } }); eventTrigger.triggers.Add(entry); } // Build map. this.mapSize = map.size; this.tiles = new BattleTile[this.mapSize.x * this.mapSize.y]; TileData baseTileData = GameData.GetTile(map.baseTileName); TileData tileData = baseTileData; this.pathNetwork = new PathNetwork(); for (int y = 0; y < map.size.y; ++y) { for (int x = 0; x < map.size.x; ++x) { var pos = new Vector2i(x, y); tileData = baseTileData; foreach (var o in map.tileOverrides) { if (o.pos == pos) { tileData = GameData.GetTile(o.name); break; } } MapTile mapTile = this.mapDisplay.GetTile(pos); BattleTile newTile = mapTile.gameObject.AddComponent <BattleTile>(); newTile.Init(this, x, y, tileData); this.tiles[x + y * this.mapSize.x] = newTile; } } foreach (BattleTile tile in this.tiles) { for (int n = 0; n < 6; ++n) { BattleTile neighbor = tile.GetNeighbor(n); if (neighbor) { this.pathNetwork.ConnectNodes(tile, neighbor); } } if (tile.data.allowsMovement == false) { this.pathNetwork.SetNodeEnabled(tile, false); } } // Create teams and place mechs. var playerSpawns = new Stack <Vector2i>(); var enemySpawns = new Stack <Vector2i>(); foreach (var e in map.entities) { if (e.name == "PlayerMechSpawn") { playerSpawns.Push(e.pos); } else if (e.name == "EnemyMechSpawn") { enemySpawns.Push(e.pos); } } this.teams = new List <BattleTeam>(); foreach (Scenario.Team teamData in scenario.teams) { BattleTeam team = new BattleTeam(); this.teams.Add(team); team.mechs = new List <BattleMech>(); team.isPlayer = teamData.isPlayer; if (team.isPlayer == false) { team.ai = new BattleTeamAi(team); } team.visibleTiles = new bool[this.tiles.Length]; foreach (Scenario.Mech m in teamData.mechs) { MechData mechData = GameData.GetMech(m.mechName); Vector2i spawnPos = team.isPlayer ? playerSpawns.Pop() : enemySpawns.Pop(); BattleTile tile = this.GetTile(spawnPos); GameObject mechGO = new GameObject(string.Concat("Mech: ", mechData.name)); mechGO.transform.parent = tile.transform; BattleMech mech = mechGO.AddComponent <BattleMech>(); mech.Init(this, mechData); mech.PlaceAtMapTile(tile.mapTile); mech.SetDirection(m.direction); Assert.IsTrue(tile.mech == null); tile.mech = mech; mech.tile = tile; if (team.isPlayer == false) { mech.ai = new BattleMechAi(mech); } this.pathNetwork.SetNodeEnabled(tile, false); team.mechs.Add(mech); mech.team = team; } } this.currentTeamIndex = this.history.scenario.startingTeamIndex; this.currentTeam = this.teams[this.currentTeamIndex]; // UI stuff. this.uiRefs.advanceAiButton.interactable = false; this.uiRefs.tileInfoBorder.SetActive(false); this.uiRefs.mechTab.SetActive(false); this.uiRefs.pilotTab.SetActive(false); this.uiRefs.tileTab.SetActive(false); this.uiRefs.actionsPanel.SetActive(false); Utility.AddButtonClickListener(this.uiRefs.finishTurnButton, this.UnitListButtonPressed); Utility.AddButtonClickListener(this.uiRefs.advanceAiButton, this.UnitListButtonPressed); Utility.AddButtonClickListener(this.uiRefs.mechTabButton, this.TileInfoTabButtonClicked); Utility.AddButtonClickListener(this.uiRefs.pilotTabButton, this.TileInfoTabButtonClicked); Utility.AddButtonClickListener(this.uiRefs.tileTabButton, this.TileInfoTabButtonClicked); Utility.AddButtonClickListener(this.uiRefs.moveButton, this.ActionButtonPressed); Utility.AddButtonClickListener(this.uiRefs.setTargetButton, this.ActionButtonPressed); Utility.AddButtonClickListener(this.uiRefs.fireNowButton, this.ActionButtonPressed); Utility.AddToggleListener(this.uiRefs.fireAutoToggle, this.ActionToggleChanged); Utility.AddButtonHoverListener(this.uiRefs.fireNowButton, this.ActionButtonHoverChanged); this.BringTileTabButtonToFront(this.uiRefs.mechTabButton); this.UpdateRightPanel(); this.apTextOriginalColor = this.uiRefs.apText.color; // Execute any moves to start with. foreach (object o in moveHistory) { this.ExecuteMove(o); } this.currentTeamIndex = battleHistory.currentTeamIndex; this.currentTeam = this.teams[this.currentTeamIndex]; // Start first state. this.UpdateFogOfWar(); if (this.currentTeam.isPlayer) { this.SetState(BattleState.SelectingAction); } else { this.SetState(BattleState.AiControl); } }