public void DoAction(Entity entity) { var aiC = ComponentManager.Instance.GetEntityComponent <AiComponent>(entity); var transformC = ComponentManager.Instance.GetEntityComponent <TransformComponent>(entity); var distance = Vector2.Distance(V3ToV2(transformC.Position), aiC.Waypoint.WaypointPosition); //Debug.WriteLine(distance); if (distance <= aiC.Waypoint.Radius) { Debug.WriteLine("Reached Waypoint " + (aiC.Waypoint.Id + 1)); FindNextWaypoint(aiC); aiC.Waypoint.SetRandomTargetPosition(); } var angle = AiSystem.GetRotation(transformC.Position, aiC.Waypoint.TargetPosition); //Run everytime? if (!NearlyEqual(transformC.Angle, angle, Epsilon)) { //MathHelper.Lerp seems to cause some strange behaviour. //var curvedAngle = MathHelper.Lerp(transformC.Angle, angle, 0.05f); var curvedAngle = CurveAngle(transformC.Angle, angle, 0.05f); transformC.Rotation = Quaternion.CreateFromAxisAngle(Vector3.UnitY, curvedAngle); transformC.Angle = curvedAngle; } if (transformC.Velocity.X < PhysicsManager.MaxSpeed) { transformC.Velocity += new Vector3(PhysicsManager.Acceleration, 0, 0); } else { transformC.Velocity = new Vector3(PhysicsManager.MaxSpeed, 0, 0); } }
public void DoAction(Entity entity) { var aiC = ComponentManager.Instance.GetEntityComponent <AiComponent>(entity); var transformC = ComponentManager.Instance.GetEntityComponent <TransformComponent>(entity); var powerup = ComponentManager.Instance.GetFirstEntityOfType <PowerupModelComponent>(); var powerupTransform = ComponentManager.Instance.GetEntityComponent <TransformComponent>(powerup); var distanceToPowerup = Vector2.Distance(AiHelper.V3ToV2(transformC.Position), AiHelper.V3ToV2(powerupTransform.Position)); if (distanceToPowerup > 60) { //State change from within state as discussed here: //https://sourcemaking.com/design_patterns/state Debug.WriteLine("Changing State to Race"); aiC.SetState(new RaceState()); return; } var angle = AiSystem.GetRotation(transformC.Position, AiHelper.V3ToV2(powerupTransform.Position)); //Run everytime? if (!AiHelper.NearlyEqual(transformC.Angle, angle, Epsilon)) { //MathHelper.Lerp seems to cause some strange behaviour. //var curvedAngle = MathHelper.Lerp(transformC.Angle, angle, 0.05f); var curvedAngle = AiHelper.CurveAngle(transformC.Angle, angle, 0.15f); transformC.Rotation = Quaternion.CreateFromAxisAngle(Vector3.UnitY, curvedAngle); transformC.Angle = curvedAngle; } transformC.Velocity = AiHelper.Accelerate(transformC.Velocity); }
/// <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() { GameEntityFactory.GraphicsDevice = GraphicsDevice; frameCounterSystem = new FrameCounterSystem(true, this.Window); modelRenderSystem = new ModelRenderSystem(); modelRenderSystem.graphicsDevice = GraphicsDevice; playerInputSystem = new PlayerInputSystem(); cameraSystem = new CameraSystem(); uiSystem = new UIRenderSystem(); collisionHandlingSystem = new CollisionHandlingSystem(); aiSystem = new AiSystem(); collisionDetectionSystem = new CollisionDetectionSystem(false); frictionSystem = new FrictionSystem(); gravitySystem = new GravitySystem(); transformSystem = new TransformSystem(); SystemManager.Instance.AddToUpdateables( cameraSystem, collisionDetectionSystem, transformSystem, gravitySystem, frictionSystem, playerInputSystem, collisionHandlingSystem, aiSystem, frameCounterSystem ); SystemManager.Instance.AddToDrawables(modelRenderSystem, frameCounterSystem, uiSystem); base.Initialize(); }
public static void Initialize() { DamageableSystem = new DamageableSystem(); CapacitiesSystem = new CapacitiesSystem(); AiSystem = new AiSystem(); _systems = new List <EntityComponentSystem>(); _systems.Add(DamageableSystem); _systems.Add(CapacitiesSystem); _systems.Add(AiSystem); }
public void DoAction(Entity entity) { var aiC = ComponentManager.Instance.GetEntityComponent <AiComponent>(entity); var transformC = ComponentManager.Instance.GetEntityComponent <TransformComponent>(entity); var player = ComponentManager.Instance.GetFirstEntityOfType <PlayerComponent>(); var playerTransform = ComponentManager.Instance.GetEntityComponent <TransformComponent>(player); var distance = Vector2.Distance(AiHelper.V3ToV2(transformC.Position), aiC.Waypoint.WaypointPosition); //Debug.WriteLine("distance:" + distance + " tranform.pos:" + transformC.Position + " waypointpos:" + aiC.Waypoint.WaypointPosition); if (distance <= aiC.Waypoint.Radius) { Debug.WriteLine("Reached waypoint: " + (aiC.Waypoint.Id + 1)); AiHelper.FindNextWaypoint(aiC); aiC.Waypoint.SetRandomTargetPosition(); } //TODO: Lös varför AI:n snurrar runt waypoints ibland. var distanceToPlayer = Vector2.Distance(AiHelper.V3ToV2(transformC.Position), AiHelper.V3ToV2(playerTransform.Position)); if (distanceToPlayer < 40 && distance > aiC.Waypoint.Radius * 2) { //State change from within state as discussed here: //https://sourcemaking.com/design_patterns/state Debug.WriteLine("Changing State to Ram"); aiC.SetState(new RamState()); return; } var powerup = ComponentManager.Instance.GetFirstEntityOfType <PowerupModelComponent>(); var powerupTransform = ComponentManager.Instance.GetEntityComponent <TransformComponent>(powerup); var distanceToPowerup = Vector2.Distance(AiHelper.V3ToV2(transformC.Position), AiHelper.V3ToV2(powerupTransform.Position)); if (distanceToPowerup < 40) { //State change from within state as discussed here: //https://sourcemaking.com/design_patterns/state Debug.WriteLine("Changing State to Pickup"); aiC.SetState(new PickupState()); return; } var angle = AiSystem.GetRotation(transformC.Position, aiC.Waypoint.TargetPosition); //Run everytime? if (!AiHelper.NearlyEqual(transformC.Angle, angle, Epsilon)) { //MathHelper.Lerp seems to cause some strange behaviour. //var curvedAngle = MathHelper.Lerp(transformC.Angle, angle, 0.05f); var curvedAngle = AiHelper.CurveAngle(transformC.Angle, angle, 0.05f); transformC.Rotation = Quaternion.CreateFromAxisAngle(Vector3.UnitY, curvedAngle); transformC.Angle = curvedAngle; } transformC.Velocity = AiHelper.Accelerate(transformC.Velocity); }
void Awake() { // create instance if (instance == null) { instance = this; } else { Destroy(this); return; } _aiSystem = this.GetComponent <AiSystem>(); instance._aiEntitys = new ArrayList(); }
private void InitAi(ECSEngine engine) { _sm.RegisterSystem("Game", new AiSystem()); var entity = EntityFactory.Instance.NewEntityWithTag("AiKart"); var modelC = new ModelComponent(engine.LoadContent <Model>("Chopper"), true, false, false) { staticModel = false }; ModelRenderSystem.AddMeshTransform(ref modelC, 1, Matrix.CreateRotationY(0.2f)); ModelRenderSystem.AddMeshTransform(ref modelC, 3, Matrix.CreateRotationY(0.5f)); ComponentManager.Instance.AddComponentToEntity(entity, modelC); //Create waypoints and add the AIComponent. var waypoints = CreateWaypoints(); AiSystem.Waypoints = waypoints; var aiC = new AiComponent(waypoints[0], new CountdownState()); ComponentManager.Instance.AddComponentToEntity(entity, aiC); ComponentManager.Instance.AddComponentToEntity(entity, new Collision3Dcomponent()); var aiKartTransform = new TransformComponent { Position = new Vector3(0.0f, 5.0f, 0.0f) }; aiKartTransform.Rotation = Quaternion.CreateFromAxisAngle(Vector3.UnitY, AiSystem.GetRotation(aiKartTransform.Position, aiC.Waypoint.TargetPosition)); aiKartTransform.Scale = new Vector3(2.5f, 2.5f, 2.5f); ComponentManager.Instance.AddComponentToEntity(entity, aiKartTransform); SceneManager.Instance.AddEntityToSceneOnLayer("Game", 3, entity); ComponentManager.Instance.AddComponentToEntity(entity, new PhysicsComponent() { Mass = 5f, Force = new Vector3(15f, 250f, 0) }); ComponentManager.Instance.AddComponentToEntity(entity, new GravityComponent()); //ComponentManager.Instance.AddComponentToEntity(entity, new FrictionComponent()); //ComponentManager.Instance.AddComponentToEntity(entity, new DragComponent()); ComponentManager.Instance.AddComponentToEntity(entity, new KartComponent()); ComponentManager.Instance.AddComponentToEntity(entity, new PlayerComponent()); ComponentManager.Instance.AddComponentToEntity(entity, new LapComponent()); }
void Start() { GameManager.GetInstance().FadeOutWhiteImg(0.5f); CardSystem.GetInstance().AddAllCard(); CardSystem.GetInstance().SetCardActive(true); CardSystem.GetInstance().AllCardMoveDeck(DeckSystem.GetInstance().GetDeck(DeckTag.ANIMATION_RIGHT_DECK)); AiSystem.GetInstance().SetupOneCardAi(); if (PlayerSystem.GetInstance().Players.Count <= 0) { PlayerSystem.GetInstance().MyPlayerId = "1"; PlayerSystem.GetInstance().AddPlayer(PlayerTag.PLAYER_BOTTOM, "1", false); PlayerSystem.GetInstance().AddPlayer(PlayerTag.PLAYER_LEFT_DOWN, "3", true); PlayerSystem.GetInstance().AddPlayer(PlayerTag.PLAYER_TOP, "2", true); PlayerSystem.GetInstance().AddPlayer(PlayerTag.PLAYER_RIGHT_UP, "4", true); TurnSystem.GetInstance().AddTurnPlayer("1"); TurnSystem.GetInstance().AddTurnPlayer("2"); TurnSystem.GetInstance().AddTurnPlayer("3"); TurnSystem.GetInstance().AddTurnPlayer("4"); //NetworkSystem.GetInstance().SendServer("FIND-ROOM:" + "1"); } StartCoroutine(StartScene()); this.UpdateAsObservable() .Subscribe(_ => { if (!GameManager.GetInstance().IsStartGame) { return; } var nowId = TurnSystem.GetInstance().PlayerNowTurn.Value; if (nowId == PlayerSystem.GetInstance().MyPlayerId) { InputSystem.GetInstance().TouchUpdate(); } }); TurnSystem.GetInstance().PlayerNowTurn.Subscribe(name => { if (name == "") { return; } if (PlayerSystem.GetInstance().GetPlayer(name).IsAi) { AiSystem.GetInstance().IsStartAi.Value = true; } if (name == PlayerSystem.GetInstance().MyPlayerId) { OnTurnEndBtn(); } }); TurnSystem.GetInstance().IsFinishTurn.Subscribe(finish => { if (!GameManager.GetInstance().IsStartGame) { return; } if (finish) { Observable.Timer(TimeSpan.FromSeconds(1)) .Subscribe(_ => { if (GameManager.GetInstance().CheckDefeatPlayer()) { return; } if (GameManager.GetInstance().CheckWinPlayer()) { return; } GameManager.GetInstance().FillDecktoDeckofAttack(DeckTag.PUT_DECK, DeckTag.DRAW_DECK); }); } }); }