Esempio n. 1
0
 public void Update(float deltaTime, List <GameObject> nearbyNPC, GameMap map)
 {
     PhysicsProcessor.Update(deltaTime, nearbyNPC, map, this);
     StatsProcessor.Update(deltaTime, StatsComponent);
     SpriteProcessor.Update(deltaTime, SpriteComponent);
     AnimationProcessor.SwitchToFrameCategory(this);
 }
Esempio n. 2
0
        private bool CheckIfInRange(PhysicsComponent source, GameObject objective)
        {
            Vector2 direction = Vector2.Zero;

            switch (source.FacingDirection)
            {
            case FacingDirections.UP:
                direction = new Vector2(0, 1);
                break;

            case FacingDirections.DOWN:
                direction = new Vector2(0, -1);
                break;

            case FacingDirections.LEFT:
                direction = new Vector2(1, 0);
                break;

            case FacingDirections.RIGHT:
                direction = new Vector2(-1, 0);
                break;
            }

            var normalized = Vector2.Subtract(source.position, objective.PhysicsComponent.position);

            normalized.Normalize();

            return((PhysicsProcessor.GetDistanceFromUnit(objective.PhysicsComponent, source) <= Range) &&
                   direction.Equals(normalized));
        }
 public Simulation Create(PhysicsProcessor sceneProcessor, PhysicsEngineFlags flags = PhysicsEngineFlags.None)
 {
     var scene = new PhysicsScene { Processor = sceneProcessor, Simulation = new Simulation(flags) };
     lock (this)
     {
         scenes.Add(scene);
     }
     return scene.Simulation;
 }
Esempio n. 4
0
 // POINT OF INTEREST
 // Use this to link the agent's bodies to the specified processor
 public void LinkToProcessor(PhysicsProcessor physicsProcessor)
 {
     _agentLink = new ObjectLinker[_agentGeom.Length];
     for (int iGeom = 0; iGeom < _agentGeom.Length; iGeom++)
     {
         _agentLink[iGeom] = new ObjectLinker(_agentGeom[iGeom]);
         physicsProcessor.AddLink(_agentLink[iGeom]);
     }
 }
 public void Release(PhysicsProcessor processor)
 {
     lock (this)
     {
         var scene = scenes.SingleOrDefault(x => x.Processor == processor);
         if (scene == null) return;
         scenes.Remove(scene);
         scene.Simulation.Dispose();
     }
 }
Esempio n. 6
0
 public void Update(Point mousePositionInWindow, List <GameObject> entities)
 {
     foreach (var entity in entities)
     {
         if (PhysicsProcessor.CheckIfClicked(mousePositionInWindow.ToVector2() / Constants.DEFAULT_ZOOMING_MODIFIER + Camera.position, entity.PhysicsComponent))
         {
             player.PhysicsComponent.objective = entity;
             return;
         }
     }
     player.PhysicsComponent.objective = null;
 }
Esempio n. 7
0
        private bool Attack(GameObject unit, GameObject objective)
        {
            if (unit.StatsComponent.Clock > unit.StatsComponent.HitIntervalLastTicks + Constants.Unit.DEFAULT_UNIT_HIT_INTERVAL - unit.StatsComponent.IntervalModifier)
            {
                if (objective != null &&
                    StatsProcessor.CheckIfAlive(objective.StatsComponent) &&
                    PhysicsProcessor.GetDistanceFromUnit(objective.PhysicsComponent, unit.PhysicsComponent) < Range)
                {
                    AudioProcessor.PlaySoundEffect(Constants.SoundEffects.FXSounds.HIT);
                    unit.StatsComponent.HitIntervalLastTicks = unit.StatsComponent.Clock;
                    StatsProcessor.TakeDamage(objective.StatsComponent, unit.StatsComponent.Damage);
                    ParticlesProcessor.NewParticleStreamAt(Constants.Particles.DEFAULT_ATTACK_PARTICLES_AMMOUNT, objective.PhysicsComponent.position, Constants.Particles.ParticlesStyle.ATTACK);
                    UIProcessor.SetFloatingText(Constants.UI.DEFAULT_FLOATING_TEXT_DURATION, "-" + unit.StatsComponent.Damage, objective.PhysicsComponent.position, Color.Red);
                    if (!StatsProcessor.CheckIfAlive(objective.StatsComponent))
                    {
                        StatsProcessor.GainExperience(unit, objective.StatsComponent.ExperienceReward);
                    }
                }
            }

            return(false);
        }
Esempio n. 8
0
        /// <summary>
        /// Initializes the Physics engine using the specified flags.
        /// </summary>
        /// <param name="processor"></param>
        /// <param name="flags">The flags.</param>
        /// <exception cref="System.NotImplementedException">SoftBody processing is not yet available</exception>
        internal Simulation(PhysicsProcessor processor, PhysicsEngineFlags flags = PhysicsEngineFlags.None)
        {
            this.processor = processor;

            if (flags == PhysicsEngineFlags.None)
            {
                if (OnSimulationCreation != null)
                {
                    flags = OnSimulationCreation();
                }
            }

            MaxSubSteps = 1;
            FixedTimeStep = 1.0f / 60.0f;

            collisionConfiguration = new BulletSharp.DefaultCollisionConfiguration();
            dispatcher = new BulletSharp.CollisionDispatcher(collisionConfiguration);
            broadphase = new BulletSharp.DbvtBroadphase();

            //this allows characters to have proper physics behavior
            broadphase.OverlappingPairCache.SetInternalGhostPairCallback(new BulletSharp.GhostPairCallback());

            //2D pipeline
            var simplex = new BulletSharp.VoronoiSimplexSolver();
            var pdSolver = new BulletSharp.MinkowskiPenetrationDepthSolver();
            var convexAlgo = new BulletSharp.Convex2DConvex2DAlgorithm.CreateFunc(simplex, pdSolver);

            dispatcher.RegisterCollisionCreateFunc(BulletSharp.BroadphaseNativeType.Convex2DShape, BulletSharp.BroadphaseNativeType.Convex2DShape, convexAlgo); //this is the ONLY one that we are actually using
            dispatcher.RegisterCollisionCreateFunc(BulletSharp.BroadphaseNativeType.Box2DShape, BulletSharp.BroadphaseNativeType.Convex2DShape, convexAlgo);
            dispatcher.RegisterCollisionCreateFunc(BulletSharp.BroadphaseNativeType.Convex2DShape, BulletSharp.BroadphaseNativeType.Box2DShape, convexAlgo);
            dispatcher.RegisterCollisionCreateFunc(BulletSharp.BroadphaseNativeType.Box2DShape, BulletSharp.BroadphaseNativeType.Box2DShape, new BulletSharp.Box2DBox2DCollisionAlgorithm.CreateFunc());
            //~2D pipeline

            //default solver
            var solver = new BulletSharp.SequentialImpulseConstraintSolver();

            if (flags.HasFlag(PhysicsEngineFlags.CollisionsOnly))
            {
                collisionWorld = new BulletSharp.CollisionWorld(dispatcher, broadphase, collisionConfiguration);
            }
            else if (flags.HasFlag(PhysicsEngineFlags.SoftBodySupport))
            {
                //mSoftRigidDynamicsWorld = new BulletSharp.SoftBody.SoftRigidDynamicsWorld(mDispatcher, mBroadphase, solver, mCollisionConf);
                //mDiscreteDynamicsWorld = mSoftRigidDynamicsWorld;
                //mCollisionWorld = mSoftRigidDynamicsWorld;
                throw new NotImplementedException("SoftBody processing is not yet available");
            }
            else
            {
                discreteDynamicsWorld = new BulletSharp.DiscreteDynamicsWorld(dispatcher, broadphase, solver, collisionConfiguration);
                collisionWorld = discreteDynamicsWorld;
            }

            if (discreteDynamicsWorld != null)
            {
                solverInfo = discreteDynamicsWorld.SolverInfo; //we are required to keep this reference, or the GC will mess up
                dispatchInfo = discreteDynamicsWorld.DispatchInfo;

                solverInfo.SolverMode |= BulletSharp.SolverModes.CacheFriendly; //todo test if helps with performance or not

                if (flags.HasFlag(PhysicsEngineFlags.ContinuosCollisionDetection))
                {
                    CanCcd = true;
                    solverInfo.SolverMode |= BulletSharp.SolverModes.Use2FrictionDirections | BulletSharp.SolverModes.RandomizeOrder;
                    dispatchInfo.UseContinuous = true;
                }
            }
        }
Esempio n. 9
0
 //------------------------------------------------------------------
 public PhysicManager()
 {
     //create a default integrator
     this.processor = new ForwardEuler();
 }
        internal void Attach(PhysicsProcessor.AssociatedData data)
        {
            Data = data;

            //this is mostly required for the game studio gizmos
            if (Simulation.DisableSimulation)
            {
                return;
            }

            //this is not optimal as UpdateWorldMatrix will end up being called twice this frame.. but we need to ensure that we have valid data.
            Entity.Transform.UpdateWorldMatrix();

            if (ColliderShapes.Count == 0)
            {
                Logger.Error($"Entity {Entity.Name} has a PhysicsComponent without any collider shape.");
                return; //no shape no purpose
            }

            if (ColliderShape == null) ComposeShape();

            if (ColliderShape == null)
            {
                Logger.Error($"Entity {Entity.Name} has a PhysicsComponent but it failed to compose the collider shape.");
                return; //no shape no purpose
            }

            BoneIndex = -1;

            OnAttach();
        }
Esempio n. 11
0
 public void execute(GameObject player)
 {
     PhysicsProcessor.MoveUp(player.PhysicsComponent);
 }
Esempio n. 12
0
 public void execute(GameObject player)
 {
     PhysicsProcessor.StopMoveRight(player.PhysicsComponent);
 }
Esempio n. 13
0
        internal void Attach(PhysicsProcessor.AssociatedData data)
        {
            Data = data;

            //this is mostly required for the game studio gizmos
            if (Simulation.DisableSimulation)
            {
                return;
            }

            //this is not optimal as UpdateWorldMatrix will end up being called twice this frame.. but we need to ensure that we have valid data.
            Entity.Transform.UpdateWorldMatrix();

            if (ColliderShapes.Count == 0) return; //no shape no purpose

            if (ColliderShape == null) ComposeShape();

            if (ColliderShape == null) return; //no shape no purpose

            BoneIndex = -1;

            OnAttach();
        }
        public async Task Run()
        {
            Services = new ServiceRegistry();

            // Database file provider
            Services.AddService <IDatabaseFileProviderService>(new DatabaseFileProviderService(new DatabaseFileProvider(ObjectDatabase.CreateDefaultDatabase())));

            // Content manager
            Content = new ContentManager(Services);
            Services.AddService <IContentManager>(Content);
            Services.AddService(Content);

            //Services.AddService<IGraphicsDeviceService>(new GraphicsDeviceServiceLocal(null));

            // Game systems
            var gameSystems = new GameSystemCollection(Services);

            Services.AddService <IGameSystemCollection>(gameSystems);
            gameSystems.Initialize();

            // Load scene (physics only)
            var loadSettings = new ContentManagerLoaderSettings
            {
                // Ignore all references (Model, etc...)
                ContentFilter = ContentManagerLoaderSettings.NewContentFilterByType()
            };
            var scene = await Content.LoadAsync <Scene>("MainScene", loadSettings);

            var sceneInstance = new SceneInstance(Services, scene, ExecutionMode.None);
            var sceneSystem   = new SceneSystem(Services)
            {
                SceneInstance = sceneInstance,
            };

            Services.AddService(sceneSystem);

            var physics = new PhysicsProcessor();

            sceneInstance.Processors.Add(physics);

            var socket = new SimpleSocket();

            socket.Connected += clientSocket =>
            {
                Console.WriteLine("Client connected");

                var reader = new BinarySerializationReader(clientSocket.ReadStream);
                while (true)
                {
                    // Receive ray start/end
                    var start = reader.Read <Vector3>();
                    var end   = reader.Read <Vector3>();
                    // Raycast
                    var result = physics.Simulation.Raycast(start, end);
                    Console.WriteLine($"Performing raycast: {(result.Succeeded ? "hit" : "miss")}");
                    // Send result
                    clientSocket.WriteStream.WriteByte((byte)(result.Succeeded ? 1 : 0));
                    clientSocket.WriteStream.Flush();
                }
            };
            await socket.StartServer(2655, false);

            Console.WriteLine("Server listening, press a key to exit");
            Console.ReadKey();
        }