public void Control(GameWorld gameWorld, Spacecraft me) { float x = 100 * (Mouse.GetState().X - gameWorld.ScreenCenter.X) / gameWorld.ScreenCenter.X; float y = 100 * (Mouse.GetState().Y - gameWorld.ScreenCenter.Y) / gameWorld.ScreenCenter.Y; me.RotataeAndMove(new Vector2(x, y)); if (Mouse.GetState().LeftButton == ButtonState.Pressed) me.FireBullet(); }
public void Control(GameWorld gameWorld, Spacecraft me) { Infinity.debugString += " " + targetId; var aiIdentifiableObjectInfos = gameWorld.GetIdentifiableObjects(); var target = FindTarget(aiIdentifiableObjectInfos, me); if (target == null) return; targetId = target.GameObjectId; facingOpponent = facingOpponent = MyMath.MovingTowards(target.Position, me.GamePosition, oldVelocity, 0.6f); ; //if (me.Energy < 20) plan = 2; turnVelocityLimit = 10f; moveVelocityLimit = 60f; if (plan == 1) { MoveTowardsAndAttack(me, target, aiIdentifiableObjectInfos); if (facingOpponent && MyMath.Distance(target.Position, me.GamePosition) < 600 && MyMath.Random.NextDouble() < .4f ) me.FireBullet(); if (MyMath.Distance(me.GamePosition, target.Position) < 50 + MyMath.Random.Next(50)) plan = 2; } else { MoveAwayFrom(me, target); if (MyMath.Distance(me.GamePosition, target.Position) > 200 && MyMath.Random.NextDouble() < .1f) plan = 1; } //if (MyMath.Random.NextDouble() < .01f) plan = plan%2 +1; }
public void RotateAndMoveWithVelocitySpeedAndTurnLimits(Spacecraft me, Vector2 targetVelocity) { targetVelocity.Normalize(); oldVelocity += turnVelocityLimit * targetVelocity; oldVelocity.Normalize(); oldVelocity = moveVelocityLimit * oldVelocity; me.RotataeAndMove(oldVelocity); }
public static Spacecraft CreateEnemy(GameWorld gameWorld, Vector2 gamePosition) { var spacecraft = new Spacecraft(gameWorld, gamePosition); // spacecraft.myTexture = new MyTexture(MyContentManager.Spacecraft2); // spacecraft.myTextureOverlay = new MyTexture(MyContentManager.Spacecraft2_Overlay); // spacecraft.color = Color.Pink; // spacecraft.colorOverlay = Color.LightPink; spacecraft.spacecraftController = new SpacecraftControllerAI(); return(spacecraft); }
public void Control(GameWorld gameWorld, Spacecraft me) { float x = 100 * (Mouse.GetState().X - gameWorld.ScreenCenter.X) / gameWorld.ScreenCenter.X; float y = 100 * (Mouse.GetState().Y - gameWorld.ScreenCenter.Y) / gameWorld.ScreenCenter.Y; me.RotataeAndMove(new Vector2(x, y)); if (Mouse.GetState().LeftButton == ButtonState.Pressed) { me.FireBullet(); } }
public static Spacecraft CreatePlayer(GameWorld gameWorld, Vector2 gamePosition) { var spacecraft = new Spacecraft(gameWorld, gamePosition); spacecraft.spacecraftController = new PlayerSpacecraftController(); // spacecraft.myTexture = new MyTexture(MyContentManager.Spacecraft1); // spacecraft.myTextureOverlay = new MyTexture(MyContentManager.Spacecraft1_Overlay); // spacecraft.color = Color.Wheat; // spacecraft.colorOverlay = Color.Snow; return(spacecraft); }
public void Control(GameWorld gameWorld, Spacecraft me) { Infinity.debugString += " " + targetId; var aiIdentifiableObjectInfos = gameWorld.GetIdentifiableObjects(); var target = FindTarget(aiIdentifiableObjectInfos, me); if (target == null) { return; } targetId = target.GameObjectId; facingOpponent = facingOpponent = MyMath.MovingTowards(target.Position, me.GamePosition, oldVelocity, 0.6f);; //if (me.Energy < 20) plan = 2; turnVelocityLimit = 10f; moveVelocityLimit = 60f; if (plan == 1) { MoveTowardsAndAttack(me, target, aiIdentifiableObjectInfos); if (facingOpponent && MyMath.Distance(target.Position, me.GamePosition) < 600 && MyMath.Random.NextDouble() < .4f) { me.FireBullet(); } if (MyMath.Distance(me.GamePosition, target.Position) < 50 + MyMath.Random.Next(50)) { plan = 2; } } else { MoveAwayFrom(me, target); if (MyMath.Distance(me.GamePosition, target.Position) > 200 && MyMath.Random.NextDouble() < .1f) { plan = 1; } } //if (MyMath.Random.NextDouble() < .01f) plan = plan%2 +1; }
/// <summary> /// Allows the game to perform any initialization it needs to before starting to run. /// This is where it can query for any required services and load any non-graphic /// related content. Calling base.Initialize will enumerate through any components /// and initialize them as well. /// </summary> protected override void Initialize() { // TODO: Add your initialization logic here MyContentManager.LoadContent(Content); var playerPosition = new Vector2(0, 0); var screenCenter = new Vector2(graphics.GraphicsDevice.Viewport.Width / 2f, graphics.GraphicsDevice.Viewport.Height / 2f); gameWorld = new GameWorld(playerPosition, screenCenter); player = Spacecraft.CreatePlayer(gameWorld, playerPosition); background = new Background(gameWorld); gameWorld.Add(player); for (int i = 0; i < 20; i++) { gameWorld.Add(Spacecraft.CreateEnemy(gameWorld, new Vector2(MyMath.Random.Next(1000), MyMath.Random.Next(1000)))); } base.Initialize(); }
/// <summary> /// Allows the game to perform any initialization it needs to before starting to run. /// This is where it can query for any required services and load any non-graphic /// related content. Calling base.Initialize will enumerate through any components /// and initialize them as well. /// </summary> protected override void Initialize() { // TODO: Add your initialization logic here MyContentManager.LoadContent(Content); var playerPosition = new Vector2(0, 0); var screenCenter = new Vector2(graphics.GraphicsDevice.Viewport.Width/2f, graphics.GraphicsDevice.Viewport.Height/2f); gameWorld = new GameWorld(playerPosition, screenCenter); player = Spacecraft.CreatePlayer(gameWorld, playerPosition); background = new Background(gameWorld); gameWorld.Add(player); for (int i = 0; i < 20; i++) { gameWorld.Add(Spacecraft.CreateEnemy(gameWorld, new Vector2(MyMath.Random.Next(1000), MyMath.Random.Next(1000)))); } base.Initialize(); }
private void MoveTowardsAndAttack(Spacecraft me, AiIdentifiableObjectInfo target, List <AiIdentifiableObjectInfo> list) { var adjustedTargetVelocity = target.Position - me.GamePosition; foreach (var info in list) { var diffDistanceFromMe = MyMath.Distance(me.GamePosition, info.Position) - MyMath.Distance(me.GamePosition, target.Position); if (info.GameObjectId == me.GameObjectId && info.GameObjectType == GameObjectType.Bullet && (diffDistanceFromMe) < 3 * MyMath.Distance(me.GamePosition, target.Position) ) { var vector2 = (MyMath.ReflectAcrossLine(info.Position - me.GamePosition, target.Position - me.GamePosition)); adjustedTargetVelocity += vector2; adjustedTargetVelocity += target.Position - me.GamePosition; } } adjustedTargetVelocity.Normalize(); adjustedTargetVelocity = MyMath.Distance(me.GamePosition, target.Position) * adjustedTargetVelocity; me.TargetReticle = adjustedTargetVelocity; RotateAndMoveWithVelocitySpeedAndTurnLimits(me, adjustedTargetVelocity); }
public static Spacecraft CreatePlayer(GameWorld gameWorld, Vector2 gamePosition) { var spacecraft = new Spacecraft(gameWorld, gamePosition); spacecraft.spacecraftController = new PlayerSpacecraftController(); // spacecraft.myTexture = new MyTexture(MyContentManager.Spacecraft1); // spacecraft.myTextureOverlay = new MyTexture(MyContentManager.Spacecraft1_Overlay); // spacecraft.color = Color.Wheat; // spacecraft.colorOverlay = Color.Snow; return spacecraft; }
private AiIdentifiableObjectInfo AnotherTargetAttackingMe(List <AiIdentifiableObjectInfo> list, AiIdentifiableObjectInfo identityInfo, Spacecraft spacecraft) { AiIdentifiableObjectInfo target = null; if (identityInfo.GameObjectId != spacecraft.GameObjectId && identityInfo.GameObjectType == GameObjectType.Bullet && MyMath.Distance(identityInfo.Position, spacecraft.GamePosition) < 100 && MyMath.MovingTowards(spacecraft.GamePosition, identityInfo.Position, identityInfo.Velocity, 0.7f)) { if (!enimity.ContainsKey(identityInfo.GameObjectId)) { enimity[identityInfo.GameObjectId] = 0; } enimity[identityInfo.GameObjectId] += 10; if (enimity[identityInfo.GameObjectId] > targetEnimity) { AiIdentifiableObjectInfo identityInfo1 = identityInfo; target = list.Find(info => info.GameObjectId == identityInfo1.GameObjectId); targetEnimity = enimity[identityInfo.GameObjectId]; } } return(target); }
private AiIdentifiableObjectInfo FindTarget(List <AiIdentifiableObjectInfo> list, Spacecraft spacecraft) { //return list.Find(info => info.GameObjectId == 0); AiIdentifiableObjectInfo target = null; foreach (var identityInfo in list) { if (identityInfo.GameObjectId == targetId) { target = identityInfo; } target = AnotherTargetAttackingMe(list, identityInfo, spacecraft) ?? target; } if (target != null) { return(target); } return(FindClosestSpacecraft(list, spacecraft)); }
private AiIdentifiableObjectInfo AnotherTargetAttackingMe(List<AiIdentifiableObjectInfo> list, AiIdentifiableObjectInfo identityInfo, Spacecraft spacecraft) { AiIdentifiableObjectInfo target = null; if (identityInfo.GameObjectId != spacecraft.GameObjectId && identityInfo.GameObjectType == GameObjectType.Bullet && MyMath.Distance(identityInfo.Position, spacecraft.GamePosition) < 100 &&MyMath.MovingTowards(spacecraft.GamePosition, identityInfo.Position, identityInfo.Velocity, 0.7f)) { if (!enimity.ContainsKey(identityInfo.GameObjectId)) enimity[identityInfo.GameObjectId] = 0; enimity[identityInfo.GameObjectId] += 10; if (enimity[identityInfo.GameObjectId] > targetEnimity) { AiIdentifiableObjectInfo identityInfo1 = identityInfo; target = list.Find(info => info.GameObjectId == identityInfo1.GameObjectId); targetEnimity = enimity[identityInfo.GameObjectId]; } } return target; }
private AiIdentifiableObjectInfo FindClosestSpacecraft(List<AiIdentifiableObjectInfo> list, Spacecraft me) { AiIdentifiableObjectInfo closestObject = null; float minDistance = float.MaxValue; foreach (var info in list) { if(info.GameObjectId == me.GameObjectId) continue; if (info.GameObjectType != GameObjectType.Spacecraft) continue; var distanceFromMe = MyMath.Distance(info.Position, me.GamePosition); if(distanceFromMe < minDistance) { minDistance = distanceFromMe; closestObject = info; } } return closestObject; }
private AiIdentifiableObjectInfo FindClosestSpacecraft(List <AiIdentifiableObjectInfo> list, Spacecraft me) { AiIdentifiableObjectInfo closestObject = null; float minDistance = float.MaxValue; foreach (var info in list) { if (info.GameObjectId == me.GameObjectId) { continue; } if (info.GameObjectType != GameObjectType.Spacecraft) { continue; } var distanceFromMe = MyMath.Distance(info.Position, me.GamePosition); if (distanceFromMe < minDistance) { minDistance = distanceFromMe; closestObject = info; } } return(closestObject); }
private AiIdentifiableObjectInfo FindTarget(List<AiIdentifiableObjectInfo> list, Spacecraft spacecraft) { //return list.Find(info => info.GameObjectId == 0); AiIdentifiableObjectInfo target = null; foreach (var identityInfo in list) { if (identityInfo.GameObjectId == targetId) target = identityInfo; target = AnotherTargetAttackingMe(list, identityInfo, spacecraft) ?? target; } if(target != null) return target; return FindClosestSpacecraft(list, spacecraft); }
private void MoveAwayFrom(Spacecraft me, AiIdentifiableObjectInfo target) { RotateAndMoveWithVelocitySpeedAndTurnLimits(me, me.GamePosition - target.Position); }
private void MoveAwayFrom(Spacecraft me, AiIdentifiableObjectInfo target) { RotateAndMoveWithVelocitySpeedAndTurnLimits(me, me.GamePosition-target.Position); }
public static Spacecraft CreateEnemy(GameWorld gameWorld, Vector2 gamePosition) { var spacecraft = new Spacecraft(gameWorld, gamePosition); // spacecraft.myTexture = new MyTexture(MyContentManager.Spacecraft2); // spacecraft.myTextureOverlay = new MyTexture(MyContentManager.Spacecraft2_Overlay); // spacecraft.color = Color.Pink; // spacecraft.colorOverlay = Color.LightPink; spacecraft.spacecraftController = new SpacecraftControllerAI(); return spacecraft; }
private void MoveTowardsAndAttack(Spacecraft me, AiIdentifiableObjectInfo target, List<AiIdentifiableObjectInfo> list) { var adjustedTargetVelocity = target.Position - me.GamePosition; foreach (var info in list) { var diffDistanceFromMe = MyMath.Distance(me.GamePosition, info.Position) - MyMath.Distance(me.GamePosition, target.Position); if (info.GameObjectId == me.GameObjectId && info.GameObjectType == GameObjectType.Bullet && (diffDistanceFromMe) <3*MyMath.Distance(me.GamePosition, target.Position) ) { var vector2 = (MyMath.ReflectAcrossLine(info.Position - me.GamePosition, target.Position - me.GamePosition)); adjustedTargetVelocity += vector2; adjustedTargetVelocity += target.Position - me.GamePosition; } } adjustedTargetVelocity.Normalize(); adjustedTargetVelocity = MyMath.Distance(me.GamePosition, target.Position)*adjustedTargetVelocity; me.TargetReticle = adjustedTargetVelocity; RotateAndMoveWithVelocitySpeedAndTurnLimits(me, adjustedTargetVelocity); }