///<summary>Check battle started and updates units to be set dead in certain condition. also updates number and battle indicator, debug infos</summary> public void Academy_Update() { if (battleStarted) { knightNumber = 0; enemyNumber = 0; GameObject[] Units = inspector.getCurrentUnits(); foreach (GameObject unit in Units) { if (inspector.setScriptsFrom(unit) && !inspector.isDead() && inspector.getLives() < 0 && !inspector.isDoneInProgress()) { inspector.setDead(); } } GameObject[] UpdatedUnits = inspector.getCurrentUnits(); for (int i = 0; i < UpdatedUnits.Length; i++) { if (inspector.setScriptsFrom(UpdatedUnits[i]) && !inspector.isDead()) { knightNumber += (inspector.getTag() == "Knight")?1:0; enemyNumber += (inspector.getTag() == "Enemy")?1:0; } } } /* * GameObject nowUnit=cam.getStickyUnit(); * if(inspector.setScriptsFrom(nowUnit)) { * DebugInner.setFromUnit(nowUnit); * } * * infoPanel.text = $@"Id:{inspector.InnerObject.GetInstanceID()} * Hp:{DebugInner.initialLives}/{DebugInner.currentLives} * Default Damage:{DebugInner.defaultDammage} * Attack Tag:{DebugInner.attackTag} * Range:{DebugInner.range} * Unit Type:{DebugInner.unitType} * * Total Damage:{DebugInner.totalDamage} * Pre Reward:{DebugInner.preReward} * * Knight Num:{knightNumber} * Enemy Num:{enemyNumber} * Episode:{AcademyInner.GetEpisodeCount()} * "; * */ }
public override void AcademyStep() { if (!IsDone() && sys.battleStarted) { GameObject[] units = inspector.getCurrentUnits(); foreach (GameObject unit in units) { if (inspector.setScriptsFrom(unit)) { if (inspector.getScriptType() == "AgentScript" && !inspector.isDead()) { inspector.AgentDescisionRequest(); inspector.AgentAlwaysUpdate(); } } } //Enviromental Upadte sys.Academy_Update(); if ((sys.knightNumber <= 0 || sys.enemyNumber <= 0) && !onceInStep) { //On End rewardSys.RewardAtEpisodeEnds(inspector, sys); onceInStep = true; Done(); } } else if (IsDone()) { int knightlen = inspector.getCurrentKnights().Length; int enemylen = inspector.getCurrentEnemys().Length; string endstring = (knightlen > 0 && enemylen <= 0)?"Knight win":(enemylen > 0 && knightlen <= 0)?"Enemy win":(enemylen <= 0 && knightlen <= 0)?"Draw":(enemylen > 0 && knightlen > 0)?"Step Max reached":"Unknown state"; Debug.Log($"{endstring} KnightAvgHP:{inspector.AvgLives(inspector.getCurrentKnights())} / EnemyAvgHP:{inspector.AvgLives(inspector.getCurrentEnemys())}"); } }
void Update() { if (Dict != null && characterPlacer.showhp) { foreach (KeyValuePair <GameObject, Healthbar> pair in Dict) { onUpdate(pair.Key, pair.Value); } } Units = new Dictionary <int, GameObject>(); foreach (GameObject unit in inspector.getCurrentUnits()) { Units.Add(unit.GetInstanceID(), unit); } int[] keys = new int[Units.Keys.Count]; Units.Keys.CopyTo(keys, 0); //don't use time.deltatime if the timescale is 0 if (Time.timeScale == 0) { timescale = (1f / 30f); } else { timescale = Time.deltaTime; } //if key gets pressed move left/right if (Input.GetKeyDown("a")) { //transform.Translate(Vector3.right * -movespeed * timescale); if (keys.Length > 0) { Sticky -= 1; if (Sticky >= keys.Length) { Sticky = 0; } if (Sticky < 0) { Sticky = keys.Length - 1; } StickyKey = keys[Sticky]; } } if (Input.GetKeyDown("d")) { //transform.Translate(Vector3.right * movespeed * timescale); if (keys.Length > 0) { Sticky += 1; if (Sticky >= keys.Length) { Sticky = 0; } if (Sticky < 0) { Sticky = keys.Length - 1; } StickyKey = keys[Sticky]; } } //if key gets pressed move up/down if (Input.GetKey("w")) { transform.Translate(Vector3.up * movespeed * timescale); } if (Input.GetKey("s")) { transform.Translate(Vector3.up * -movespeed * timescale); } //if scrollwheel is down rotate camera if (Input.GetMouseButton(2)) { float mouseX = Input.GetAxis("Mouse X"); float mouseY = -Input.GetAxis("Mouse Y"); rotateCamera(mouseX, mouseY); } //move camera when you scroll transform.Translate(new Vector3(0, 0, Input.GetAxis("Mouse ScrollWheel")) * zoomSpeed * timescale); if (characterPlacer.battleStarted && keys.Length > 0 && (characterPlacer.knightNumber >= 1 || characterPlacer.enemyNumber >= 1)) { if (Sticky > keys.Length - 1) { Sticky = 0; } if (Sticky < 0) { Sticky = keys.Length - 1; } if (!Units.ContainsKey(StickyKey)) { StickyKey = keys[Sticky]; } if (Units.ContainsKey(StickyKey)) { if (inspector.setScriptsFrom(Units[StickyKey]) && !inspector.isDead()) { transform.position = new Vector3(Units[StickyKey].transform.position.x, transform.position.y, Units[StickyKey].transform.position.z); thatKnight = Units[StickyKey]; } else { Debug.LogWarning("Undead caught while controlling cam"); } //print($"{Units.Length}/{Sticky}/Front:{((Sticky+1<Units.Length)?Units[Sticky+1].tag:Units[0].tag)}/Current:{Units[Sticky].tag}/Backward:{((Sticky>0)?Units[Sticky-1].tag:Units[Units.Length-1].tag)}"); } else { Debug.LogError("It wasn't on the key"); } } }
public override void CollectObservations() { if (sys != null) { if (!sys.battleStarted) { Debug.LogWarning("Observation triggered before battle started. sending zero observation"); for (int i = 0; i < PlannedObs; i++) { AddVectorObs(0); AddVectorObs(0); } } else { GameObject[] Knight = inspector.getCurrentKnights(); GameObject[] Enemy = inspector.getCurrentEnemys(); for (int i = 0; i < sys.initKnightNumber; i++) { if (i >= Knight.Length || Knight?[i] == null) { AddVectorObs(0); AddVectorObs(0); continue; } if (inspector.setScriptsFrom(Knight[i])) //returns true when it's valid { if (!inspector.isDead()) { // When alive AddVectorObs(Knight[i].transform.localPosition.x); AddVectorObs(Knight[i].transform.localPosition.z); } else { // When dead AddVectorObs(0); AddVectorObs(0); } continue; } //When script is not valid but it existed as game object Debug.LogWarning("Unknown game object tagged as knight and it observated."); AddVectorObs(0); AddVectorObs(0); continue; } for (int i = 0; i < sys.initEnemyNumber; i++) { if (i >= Enemy.Length || Enemy?[i] == null) { AddVectorObs(0); AddVectorObs(0); continue; } if (inspector.setScriptsFrom(Enemy[i])) //returns true when it's valid { if (!inspector.isDead()) { AddVectorObs(Enemy[i].transform.localPosition.x); AddVectorObs(Enemy[i].transform.localPosition.z); } else { AddVectorObs(0); AddVectorObs(0); } continue; } Debug.LogError("Unknown game object tagged as enemy and it observated."); AddVectorObs(0); AddVectorObs(0); continue; } } } else { Debug.LogError("Observation triggered before even game system instantiate"); for (int i = 0; i < PlannedObs; i++) //placeholder { AddVectorObs(0); AddVectorObs(0); } } SetTextObs($"{inspector.AvgLives(inspector.getCurrentKnights())}"); }
void FixedUpdate() { if (lives != lastLives && Academy.showeffects) { if (!DamagedParticle.isPlaying) { DamagedParticle.Play(); //DamagedParticle.Simulate(Time.unscaledDeltaTime, true, false); } lastLives = lives; } //find closest enemy //ML:relating to moves if (currentTarget != null) { inspector.setScriptsFrom(currentTarget.gameObject); if (!inspector.isScriptValid() || inspector.isDead()) { currentTarget = findCurrentTarget(); } } else { currentTarget = findCurrentTarget(); } //if character ran out of lives, it should die if (lives < 0 && !dead) { die(); } else { if (Vector3.Distance(agent.destination, transform.position) <= agent.stoppingDistance && currentTarget != null) { int sign = rnd.Next(0, 2) * 2 - 1; int sign2 = rnd.Next(0, 2) * 2 - 1; agent.destination = new Vector3(currentTarget.position.x + (float)gausianRand() * RandomRange * sign, currentTarget.position.y, currentTarget.position.z + (float)gausianRand() * RandomRange * sign2); agent.isStopped = false; } else if (currentTarget == null) { agent.destination = getRandomPosition(area); } if (isPassedCooltime) //&& (rnd.Next(0, 2) * 2 - 1)>0) { { foreach (GameObject unit in HitboxComponent.GetCollideObjects()) { if (unit == null && !ReferenceEquals(unit, null)) { HitboxComponent.RemoveObject(unit); continue; } if (unit.CompareTag("Knight")) { if (inspector.setScriptsFrom(unit) && !inspector.isDead()) { inspector.setLives(inspector.getLives() - (damage)); if (inspector.getLives() < 0) { HitboxComponent.RemoveObject(unit); } } else { Debug.LogWarning("Invalid Target Triggered."); } } } isPassedCooltime = false; StartCoroutine("Cooltime"); } } //ML:relating to moves }