/// <summary> /// LoadContent will be called once per game and is the place to load /// all of your content. /// </summary> protected override void LoadContent() { // Create a new SpriteBatch, which can be used to draw textures. spriteBatch = new SpriteBatch(GraphicsDevice); Globals.Globals.spriteBatch = spriteBatch; Globals.Globals.content = Content; //Give global handlers to things we need playerShip = new Ship(this, null); enemyShip = new AIShip(this, null, playerShip); gameBackground = Content.Load <Texture2D>("images/background"); vickyHappy = Content.Load <Texture2D>("images/VickyHappy"); vickyHurt = Content.Load <Texture2D>("images/VickyHurt"); vickyShoot = Content.Load <Texture2D>("images/VickyShooting"); vickyRepair = Content.Load <Texture2D>("images/VickyRepair"); vickyBail = Content.Load <Texture2D>("images/VickyBailing"); enemyShipImg = Content.Load <Texture2D>("images/EnemyShip"); boom = Content.Load <Texture2D>("images/boom"); plank = Content.Load <Texture2D>("images/plank"); gameMusic = Content.Load <Song>("music/battleMusic"); MediaPlayer.Play(gameMusic); explosionCloud = Content.Load <Texture2D>("images/explosionCloud"); // TODO: call all resource.load() methods Globals.Resources.Fonts.load(); Globals.Resources.Textures.load(); screenManager = new ScreenManager.ScreenManager(); //Add title screen here if we want it ScreenManager.ScreenManager.addScreen(new ScreenManager.Screens.GameScreen()); ScreenManager.ScreenManager.addScreen(new ScreenManager.Screens.HUDScreen()); }
public ShipController CreateAIShip(Vector2 pos, int id, int shipType = 0, int team = 0) { ShipController shipController = new ShipController(); AIShip ship = new AIShip(shipsInfo[shipType], projectilesController, particleSystem, team, pos, id); shipController.SetShip(ship, CreateTestShip(pos, shipType)); AddShip(shipController); return(shipController); }
public void CheckReceived() { if (received.Length >= length) { var command = ITell.FromStream(received, length); switch (command) { case TellClient.SetWorld c: var sys = c.sys; AIShip ai = null; var playerId = client.playerMain?.playerShip?.id; if (playerId.HasValue && client.entityLookup.TryGetValue(playerId.Value, out var en)) { ai = en as AIShip; } ai ??= sys.entities.all.OfType <AIShip>().FirstOrDefault(); if (ai != null) { lock (client.World) { client.World = sys; } client.removed = ai; sys.RemoveEntity(ai); var playerShip = new PlayerShip(new Player(client.prev.settings), ai.ship, ai.sovereign); playerShip.mortalChances = 0; sys.AddEntity(playerShip); sys.UpdatePresent(); client.SetPlayerMain(new PlayerMain(client.Width, client.Height, new Profile(), playerShip)); TellServer(new TellServer.AssumePlayerShip() { shipId = ai.id }); } break; case TellClient.SetCamera c: client.camera = c.pos; break; case TellClient.SyncSharedState c: foreach (var state in c.entities) { if (client.entityLookup.TryGetValue(state.id, out Entity e)) { e.SetSharedState(state); } } break; } received.Close(); } }
public void Handle(TellServer.AssumePlayerShip c) { var Id = c.shipId; var ai = (AIShip)game.entityLookup[Id]; var World = game.World; removed = ai; World.RemoveEntity(ai); playerShip = new PlayerShip(new Player(new Settings()), ai.ship, World.types.Lookup <Sovereign>("sovereign_player")); World.AddEntity(playerShip); }
public void AssignShipFollow(AIShip theShip) { if (followingShips.Count > 0) { followingShips[0].AssignShipFollow(theShip); } else { theShip.target = this; followingShips.Add(theShip); } }
public void FindFollowingPosition(AIShip parent = null) { AIShip otherShip = (AIShip)target; otherShip.followingShips.Add(this); if (followingShips.Count > 0) { followingShips[0].stateAI = eAIState.Travelling; followingShips[0].target = null; followingShips.RemoveAt(0); } }
IEnumerator WeaponTargetSelectionSequence(List<ShipComponent> components) { Debug.Log("Select Target to fire upon: [Click] to confirm, [Esc] to cancel"); List<AIShip> aiShips = TurnBasedCombatSystem.Instance.aiShips; int numAIShips = aiShips.Count; if (numAIShips > 0) { bool targetConfirmed = false; int targetShipIndex = 0; yield return StartCoroutine(CameraDirector.Instance.AimAtTarget(trans, aiShips[targetShipIndex].transform, 1.0f)); while (!targetConfirmed) { if (Input.GetKeyDown(KeyCode.Escape)) { targetConfirmed = true; targetShip = null; } if (Input.GetKeyDown(KeyCode.Tab)) { targetShipIndex = ++targetShipIndex % numAIShips; Debug.Log(targetShipIndex); yield return StartCoroutine(CameraDirector.Instance.AimAtTarget(trans, aiShips[targetShipIndex].transform, 1.0f)); } if (Input.GetMouseButton(2)) { CameraDirector.Instance.OrbitAroundImmediate(trans, Input.GetAxis("Mouse X"), Input.GetAxis("Mouse Y")); } if (Input.GetMouseButtonDown(0)) { Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition); RaycastHit hit; if (Physics.Raycast(ray, out hit, 1000f, 1 << GlobalTagsAndLayers.Instance.layers.enemyShipLayer)) { Debug.Log("Clicked on: " + hit.collider.gameObject.name); targetConfirmed = true; targetShip = hit.collider.gameObject.GetSafeComponent<AIShip>(); } } yield return null; } } else { Debug.Log("No Targets available"); } }
public void CreateWorld(int numEnemies, int numAllies, int numPlayers) { for (int i = 0; i < numEnemies; i++) { AIShip newship = (AIShip)Instantiate(allyShipPrefab, new Vector3(Random.Range(-20, 20), Random.Range(20, 22)), transform.rotation); newship.teamId = 1; } for (int i = 0; i < numAllies; i++) { AIShip newship = (AIShip)Instantiate(allyShipPrefab, new Vector3(Random.Range(-20, 20), Random.Range(-20, -22)), transform.rotation); newship.teamId = 0; } bool first = true; for (int i = 0; i < numPlayers; i++) { PlayerController newship = (PlayerController)Instantiate(playerPrefab, new Vector3(Random.Range(-20, 20), Random.Range(-20, -22)), transform.rotation); if (first) { //newship.teamId = 1; //newship.transform.position = new Vector3(newship.transform.position.x, // 20, 0); first = false; } } if (numPlayers >= 2) { Camera firstCam = GameObject.Find("camera0").GetComponent <Camera>(); firstCam.rect = new Rect(0, 0, 0.5f, 1); Camera secondCam = GameObject.Find("camera1").GetComponent <Camera>(); secondCam.rect = new Rect(0.5f, 0, 0.5f, 1); } }
private void MakeShipsFollow() { List <Ship> nearestShips = new List <Ship>(); int shipNum = playerManager.ships.Count; for (int i = 0; i < shipNum; i++) { if (playerManager.ships[i].AIControlled) { AIShip temp = (AIShip)playerManager.ships[i]; if (temp.isAlive && temp.teamId == teamId) { if (Vector2.Distance(temp.transform.position, transform.position) < 3) { nearestShips.Add(temp); if (nearestShips.Count >= 2 - followingShips.Count) { break; } } } } } for (int i = 0; i < nearestShips.Count; i++) { AIShip temp = (AIShip)nearestShips[i]; temp.stateAI = AIShip.eAIState.Following; temp.target = this; followingShips.Add(temp); if (temp.followingShips.Count > 0) { for (int j = 0; j < temp.followingShips.Count; j++) { AIShip subShip = (AIShip)temp.followingShips[j]; subShip.target = null; subShip.stateAI = AIShip.eAIState.Travelling; } } } }
private AIShip CreateAIShip(string shipId, Point2D pos, BoundaryStrategy boundaryStrat, Difficulty diff, IHandlesEntities entHandler) { AIStrategyFactory strategyFac = new AIStrategyFactory(diff.DifficultyLevel, diff.ShootCooldown); JObject obj = Util.Deserialize(FileRegistry[shipId]); //int health = obj.Value<int>("health"); List <Color> shipColors = new List <Color> { Color.Crimson, Color.Yellow, Color.White, Color.Red }; float scale = obj.Value <float>("scale"); JArray enginesObj = obj.Value <JArray>("engines"); JArray toolsObj = obj.Value <JArray>("tools"); JArray emittersObj = obj.Value <JArray>("emitters"); JObject shapeObj = obj.Value <JObject>("shape"); Team team = Team.Computer; Point2D offset = SwinGame.PointAt(0, 0); //shape Shape shape = new ShapeFactory().Create(shapeObj, scale, SwinGame.PointAt(0, 0)); int health = shape.Mass / 2; shape.TeleportTo(pos); //components List <Component> components = BuildComponents(enginesObj, toolsObj, emittersObj, entHandler, boundaryStrat, team, offset, diff.AIMod); //build and return ship AIShip result = new AIShip(shipId, FileRegistry[shipId], pos, SwinGame.PointAt(0, 0), shape, shipColors, health, SwinGame.VectorTo(0, 0), SwinGame.VectorTo(0, -1), boundaryStrat, team, components); //create strategy AIStrategy aiStrat = strategyFac.Create((IAIEntity)result, entHandler); result.AIStrategy = aiStrat; result.TeleportTo(pos); return(result); }
public IEnumerator ActivateComponents(List<ShipComponent> components) { activationComplete = false; targetShip = null; if(components[0] is Component_Weapon) { yield return StartCoroutine(WeaponTargetSelectionSequence(components)); if (targetShip) { trans.LookAt(targetShip.transform); foreach (Component_Weapon weapon in components) { Debug.Log("Firing on: " + targetShip.gameObject.name); weapon.Fire(targetShip.transform, ()=>{ activationComplete = true; targetShip.TakeDamage(weapon.damage); }); } } } else { Debug.Log("not weapons"); activationComplete = true; } while (!activationComplete) { yield return null; } yield return StartCoroutine(CameraDirector.Instance.FocusOn(trans, 1.0f)); }
public AIShipMapUnit(Map map, AIShip aIShip, Vector movementOffset) : base(map, aIShip, movementOffset) { this.aIShip = aIShip; }
public void AddShip(AIShip ship) { Ships.Add(ship); }
public AIAction(AIShip host) { this.host = host; }
void FixedUpdate() { if (!isAlive) { return; } CustomUpdate(); forward.x = transform.up.x; forward.y = transform.up.y; float deltaTime = Time.deltaTime; switch (stateAI) { case eAIState.Travelling: { // find a target if we dont have one if (target == null || !target.isAlive || target.teamId == teamId) { //todo better distance calculation once sections are completed int shipNum = playerManager.ships.Count; int foundNum = -1; float dist = 9999999; for (int i = 0; i < shipNum; i++) { Ship temp = playerManager.ships[i]; if (temp.shipId != shipId && temp.isAlive && temp.teamId != teamId && Vector2.Distance(temp.transform.position, transform.position) < dist) { dist = Vector2.Distance(temp.transform.position, transform.position); foundNum = i; } } if (foundNum >= 0) { target = playerManager.ships[foundNum]; stateAI = eAIState.Hunting; } } else { stateAI = eAIState.Hunting; } } break; case eAIState.Following: { if (target != null) { float distance = Vector2.Distance(targetLocation, transform.position); float angle = TurnTowards(deltaTime, targetLocation); if (angle < thrustAngle && distance > followDistance) { MoveForward(deltaTime, 3.3f, true); } else { thrustingTime = 0; myBody.angularDrag = 1.5f; if (distance > followDistance / 2) { MoveForward(deltaTime, 1.5f, false, 0.2f); } else if (animThrusting) { thrusterAnim.SetBool("IsStarting", false); thrusterAnim.SetBool("IsEnding", true); animThrusting = false; } } if (target.AIControlled) { AIShip theOtherShip = ((AIShip)target); if (!target.isAlive || theOtherShip.stateAI == eAIState.Travelling || theOtherShip.stateAI == eAIState.Fleeing) { stateAI = eAIState.Travelling; theOtherShip.followingShips.Remove(this); } } else { if (!target.isAlive) { stateAI = eAIState.Travelling; target.followingShips.Remove(this); } } } else { stateAI = eAIState.Travelling; } } break; case eAIState.Hunting: { if (target != null && target.isAlive) { if (firingTimer > 0) { firingTimer -= Time.deltaTime; if (firingTimer < 0) { firingTimer = 0; } } float distance = Vector2.Distance(target.transform.position, transform.position); float angle = TurnTowards(deltaTime, target.transform.position); if (angle < shootAngle && firingTimer == 0) { if (distance < fireDistance) { FireLaser(); } //chance check to see if this turns into a strafing run if (thrustingTime > 0 && Random.Range(0, 50) > 49) //if its moving at a good pace { float coinToss = Random.Range(0, 2); if (coinToss >= 1) { targetLocation = new Vector2(transform.position.x, transform.position.y) + (forward.normalized * 9) + new Vector2(forward.y, -forward.x).normalized *Random.Range(2, 4); } else { targetLocation = new Vector2(transform.position.x, transform.position.y) + (forward.normalized * 9) + new Vector2(-forward.y, forward.x).normalized *Random.Range(2, 4); } stateAI = eAIState.StrafingToAttack; } } if (angle < thrustAngle && distance > followDistance) { MoveForward(deltaTime, 3.3f, true); } else { thrustingTime = 0; myBody.angularDrag = 1.5f; if (distance > followDistance / 2) { MoveForward(deltaTime, 1.5f, false, 0.2f); } else if (animThrusting) { thrusterAnim.SetBool("IsStarting", false); thrusterAnim.SetBool("IsEnding", true); animThrusting = false; } } //check for any other ship that is following the same target if (followingShips.Count < 2) { int shipNum = playerManager.ships.Count; for (int i = 0; i < shipNum; i++) { if (playerManager.ships[i].AIControlled) { AIShip temp = (AIShip)playerManager.ships[i]; if (temp.shipId != this.shipId && temp.isAlive && temp.teamId == teamId && temp.stateAI == eAIState.Hunting && temp.target.shipId == this.target.shipId && temp.followingShips.Count < 2 ) { target = temp; FindFollowingPosition(); stateAI = eAIState.Following; break; } } } } } else { stateAI = eAIState.Travelling; } } break; case eAIState.Fleeing: { if (aggressor != null) { float distance = Vector2.Distance(aggressor.transform.position, transform.position); if (distance < fleeDistance) { Vector2 pos = new Vector2(transform.position.x, transform.position.y); var enemyPos = aggressor.transform.position; Vector2 targetDirection = pos - new Vector2(enemyPos.x, enemyPos.y); fleeOffsetTime += deltaTime; if (fleeOffsetTime > fleeOffsetMaxTime) { RecalulateFleeOffset(); } Vector2 targetPoint = (targetDirection.normalized * fleeDistance) + pos + fleeOffset; float angle = TurnTowards(deltaTime, targetPoint); if (angle < thrustAngle) { MoveForward(deltaTime, 3.3f, true); } else { thrustingTime = 0; myBody.angularDrag = 1.5f; MoveForward(deltaTime, 1.5f, false, 0.2f); } } else { stateAI = eAIState.Travelling; } } else { //todo //search for any nearby enemy units, if there is one, assign a new aggressor //if not break out of fleeing stateAI = eAIState.Travelling; } } break; case eAIState.StrafingToFlee: { TurnTowards(deltaTime, targetLocation); MoveForward(deltaTime, 3.3f, true); //test distance from target, if it is small enough, then go into fleeing fleeOffsetTime += deltaTime; float distance = Vector2.Distance(targetLocation, transform.position); if (distance < 0.2f || fleeOffsetTime > fleeOffsetMaxTime) { stateAI = eAIState.Fleeing; } } break; case eAIState.StrafingToAttack: { TurnTowards(deltaTime, targetLocation); MoveForward(deltaTime, 3.3f, true); //test distance from target, if it is small enough, then go into fleeing fleeOffsetTime += deltaTime; float distance = Vector2.Distance(targetLocation, transform.position); if (distance < 0.2f) { stateAI = eAIState.Hunting; } } break; } ; }
public void Remove(AIShip ship) { Ships.Remove(ship); }
public PilotCommand(AIShip pilot, Node currentCommand, Guid guid) { this.pilot = pilot; this.currentCommand = currentCommand; this.guid = guid; }
public AiMoveToPos(double toPosX, double toPosY, AIShip host) : base(host) { this.toPosX = toPosX; this.toPosY = toPosY; }