예제 #1
0
        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);
            }
        }
예제 #2
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);
        }
예제 #3
0
        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);
        }
예제 #4
0
        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());
        }