Example #1
0
 // Create systems
 public void createLevelSystems()
 {
     _systemManager.add(new InputSystem(_systemManager, _entityManager), -1);
     _systemManager.add(new PhysicsSystem(_systemManager, _entityManager), -1);
     _systemManager.add(new CameraSystem(_systemManager, _entityManager), -1);
     _systemManager.add(new EventSystem(_systemManager, _entityManager), -1);
     _systemManager.add(new RopeSystem(_systemManager, _entityManager), -1);
     _systemManager.add(new ExplosionSystem(_systemManager, _entityManager), -1);
     _renderSystem    = new RenderSystem(_game, _systemManager, _entityManager);
     _animationSystem = new AnimationSystem(_systemManager, _entityManager, _game.animationManager);
     _systemManager.add(_renderSystem, -1);
     _systemManager.add(_animationSystem, -1);
     _systemManager.add(new TreeSystem(_systemManager, _entityManager), -1);
     _systemManager.add(new FluidSystem(_systemManager, _entityManager), -1);
     _systemManager.add(new AIBehaviorSystem(_systemManager, _entityManager), -1);
     _systemManager.add(new DialogueSystem(_systemManager, _entityManager), -1);
     _playerSystem = (PlayerSystem)_systemManager.getSystem(SystemType.Player);
 }
Example #2
0
        public PhysicsSystem(SystemManager systemManager, EntityManager entityManager)
        {
            _systemManager = systemManager;
            _entityManager = entityManager;
            _bodiesToRemove = new List<Body>();
            _playerSystem = (PlayerSystem)systemManager.getSystem(SystemType.Player);
            _worlds = new Dictionary<string, World>();
            _groundBodies = new Dictionary<string, Body>();
            /*
            // Create world
            _world = new World(gravity);

            // Contact callbacks
            _world.ContactManager.BeginContact += new BeginContactDelegate(BeginContact);
            _world.ContactManager.EndContact += new EndContactDelegate(EndContact);
            _world.ContactManager.PreSolve += new PreSolveDelegate(PreSolve);
            _world.ContactManager.PostSolve += new PostSolveDelegate(PostSolve);

            // Create ground body/entity
            _groundBody = _entityManager.factory.createGroundBody(_world);*/
        }
Example #3
0
        public PhysicsSystem(SystemManager systemManager, EntityManager entityManager)
        {
            _systemManager  = systemManager;
            _entityManager  = entityManager;
            _bodiesToRemove = new List <Body>();
            _playerSystem   = (PlayerSystem)systemManager.getSystem(SystemType.Player);
            _worlds         = new Dictionary <string, World>();
            _groundBodies   = new Dictionary <string, Body>();

            /*
             * // Create world
             * _world = new World(gravity);
             *
             * // Contact callbacks
             * _world.ContactManager.BeginContact += new BeginContactDelegate(BeginContact);
             * _world.ContactManager.EndContact += new EndContactDelegate(EndContact);
             * _world.ContactManager.PreSolve += new PreSolveDelegate(PreSolve);
             * _world.ContactManager.PostSolve += new PostSolveDelegate(PostSolve);
             *
             * // Create ground body/entity
             * _groundBody = _entityManager.factory.createGroundBody(_world);*/
        }
Example #4
0
        // update
        public void update(GameTime gameTime)
        {
            if (_singleStep || !_paused)
            {
                string      levelUid    = LevelSystem.currentLevelUid;
                LevelSystem levelSystem = _systemManager.getSystem(SystemType.Level) as LevelSystem;

                if (levelSystem.finalized)
                {
                    PlayerSystem     playerSystem           = _systemManager.getSystem(SystemType.Player) as PlayerSystem;
                    RopeSystem       ropeSystem             = _systemManager.getSystem(SystemType.Rope) as RopeSystem;
                    PhysicsComponent playerPhysicsComponent = _entityManager.getComponent(levelUid, PlayerSystem.PLAYER_ID, ComponentType.Physics) as PhysicsComponent;
                    List <int>       toolbarEntities        = _entityManager.getEntitiesPosessing(levelUid, ComponentType.Toolbar);

                    // Player equipment
                    if (playerSystem != null)
                    {
                        ToolbarComponent       playerToolbar           = _entityManager.getComponent(levelUid, PlayerSystem.PLAYER_ID, ComponentType.Toolbar) as ToolbarComponent;
                        WorldPositionComponent playerPositionComponent = _entityManager.getComponent(levelUid, PlayerSystem.PLAYER_ID, ComponentType.WorldPosition) as WorldPositionComponent;
                        ItemComponent          selectedItem            = playerToolbar.selectedItem;

                        if (selectedItem != null)
                        {
                            selectedItem.primaryContinuousAction   = InputSystem.newMouseState.LeftButton == ButtonState.Pressed;
                            selectedItem.primarySingleAction       = selectedItem.primaryContinuousAction && InputSystem.oldMouseState.LeftButton == ButtonState.Released;
                            selectedItem.secondaryContinuousAction = InputSystem.newMouseState.RightButton == ButtonState.Pressed;
                            selectedItem.secondarySingleAction     = selectedItem.secondaryContinuousAction && InputSystem.oldMouseState.RightButton == ButtonState.Released;
                            //bool leftTriggerDown = InputSystem.usingGamepad && InputSystem.newGamepadState.Triggers.Left > 0.5f && InputSystem.oldGamepadState.Triggers.Left <= 0.5f;
                            //bool rightTriggerDown = InputSystem.usingGamepad && InputSystem.newGamepadState.Triggers.Right > 0.5f && InputSystem.oldGamepadState.Triggers.Right <= 0.5f;
                            AimComponent aimComponent = _entityManager.getComponent(levelUid, PlayerSystem.PLAYER_ID, ComponentType.Aim) as AimComponent;

                            if (selectedItem.definition.hasAimingComponent && aimComponent != null)
                            {
                                WorldPositionComponent worldPositionComponent = _entityManager.getComponent(levelUid, PlayerSystem.PLAYER_ID, ComponentType.WorldPosition) as WorldPositionComponent;

                                if (worldPositionComponent != null)
                                {
                                    Vector2 worldPosition = worldPositionComponent.position;
                                    if (InputSystem.usingGamepad)
                                    {
                                        Vector2 vector = InputSystem.newGamepadState.ThumbSticks.Left * selectedItem.state.currentRangeLimit;
                                        vector.Y           *= -1;
                                        aimComponent.angle  = (float)Math.Atan2(vector.Y, vector.X);
                                        aimComponent.length = vector.Length();
                                        aimComponent.vector = vector;
                                    }
                                    else
                                    {
                                        Vector2 relative = (InputSystem.worldMouse - worldPosition);
                                        aimComponent.angle  = (float)Math.Atan2(relative.Y, relative.X);
                                        aimComponent.length = Math.Min(relative.Length(), selectedItem.state.currentRangeLimit);
                                        aimComponent.vector = relative;
                                    }
                                }
                            }
                        }
                    }

                    // All toolbars
                    for (int i = 0; i < toolbarEntities.Count; i++)
                    {
                        ToolbarComponent toolbarComponent = _entityManager.getComponent(levelUid, toolbarEntities[i], ComponentType.Toolbar) as ToolbarComponent;
                        ItemComponent    selectedItem     = toolbarComponent.selectedItem;

                        if (selectedItem != null)
                        {
                            if (selectedItem.secondarySingleAction)
                            {
                                Console.WriteLine("secondary action");
                            }

                            switch (selectedItem.definition.uid)
                            {
                            // RopeGun
                            case "ropegun":
                                if (selectedItem.primarySingleAction)
                                {
                                    AimComponent aimComponent  = _entityManager.getComponent(levelUid, toolbarEntities[i], ComponentType.Aim) as AimComponent;
                                    Vector2      initialPointA = (_entityManager.getComponent(levelUid, toolbarEntities[i], ComponentType.WorldPosition) as WorldPositionComponent).position;
                                    Vector2      initialPointB = initialPointA + new Vector2((float)Math.Cos(aimComponent.angle), (float)Math.Sin(aimComponent.angle)) * aimComponent.length;
                                    int          ropeEntityId  = _entityManager.factory.createSingleAnchorRope(levelUid, initialPointA, initialPointB, _defaultRopeMaterial, true);

                                    if (ropeEntityId != -1)
                                    {
                                        RopeGrabComponent ropeGrabComponent    = _entityManager.getComponent(levelUid, toolbarComponent.entityId, ComponentType.RopeGrab) as RopeGrabComponent;
                                        RopeComponent     ropeComponent        = _entityManager.getComponent(levelUid, ropeEntityId, ComponentType.Rope) as RopeComponent;
                                        PhysicsComponent  physicsComponent     = _entityManager.getComponent(levelUid, toolbarEntities[i], ComponentType.Physics) as PhysicsComponent;
                                        RopeGrabComponent newRopeGrabComponent = null;
                                        Vector2           initialVelocity      = physicsComponent.body.LinearVelocity;
                                        RopeNode          currentNode          = null;
                                        int ropeSegmentCount;

                                        if (physicsComponent == null)
                                        {
                                            break;
                                        }

                                        // Handle initial velocity
                                        currentNode      = ropeComponent.ropeNodeHead;
                                        ropeSegmentCount = currentNode.count;
                                        System.Diagnostics.Debug.Assert(ropeSegmentCount != 0);
                                        int count = ropeSegmentCount;
                                        while (currentNode != null)
                                        {
                                            float weight = (float)count / (float)ropeSegmentCount;

                                            currentNode.body.LinearVelocity = currentNode.body.LinearVelocity + initialVelocity * weight;

                                            count--;
                                            currentNode = currentNode.next;
                                        }

                                        // Handle previous grabs
                                        if (ropeGrabComponent != null)
                                        {
                                            RopeComponent previouslyGrabbedRope = _entityManager.getComponent(levelUid, ropeGrabComponent.ropeEntityId, ComponentType.Rope) as RopeComponent;
                                            ropeSystem.releaseRope(ropeGrabComponent, physicsComponent.body);

                                            if (previouslyGrabbedRope.destroyAfterRelease)
                                            {
                                                previouslyGrabbedRope.timeToLive = 100;
                                            }
                                            _entityManager.removeComponent(levelUid, toolbarComponent.entityId, ropeGrabComponent);
                                            ropeGrabComponent = null;
                                        }

                                        newRopeGrabComponent = new RopeGrabComponent(ropeEntityId, ropeComponent.ropeNodeHead, 0f, ropeComponent.reverseClimbDirection);
                                        ropeSystem.grabRope(newRopeGrabComponent, physicsComponent.body);
                                        _entityManager.addComponent(levelUid, toolbarComponent.entityId, newRopeGrabComponent);
                                    }
                                }
                                break;

                            // Dynamite
                            case "dynamite":
                                if (selectedItem.primarySingleAction)
                                {
                                    AimComponent aimComponent = _entityManager.getComponent(levelUid, toolbarEntities[i], ComponentType.Aim) as AimComponent;

                                    _entityManager.factory.createDynamite(levelUid, playerPhysicsComponent.body.Position, aimComponent.vector * 80f);
                                }
                                break;

                            // Water gun
                            case "watergun":
                                if (selectedItem.primaryContinuousAction)
                                {
                                    FluidSystem  fluidSystem      = _systemManager.getSystem(SystemType.Fluid) as FluidSystem;
                                    AimComponent aimComponent     = _entityManager.getComponent(levelUid, toolbarEntities[i], ComponentType.Aim) as AimComponent;
                                    Vector2      aimUnitVector    = Vector2.Normalize(aimComponent.vector);
                                    Vector2      particlePosition =
                                        playerPhysicsComponent.body.Position +
                                        aimUnitVector +
                                        new Vector2(StasisMathHelper.floatBetween(-0.1f, 0.1f, _rng), StasisMathHelper.floatBetween(-0.1f, 0.1f, _rng));
                                    Vector2 particleVelocity = aimUnitVector * 0.4f;

                                    fluidSystem.createParticle(particlePosition, particleVelocity);
                                }
                                break;
                            }

                            selectedItem.primarySingleAction       = false;
                            selectedItem.secondarySingleAction     = false;
                            selectedItem.primaryContinuousAction   = false;
                            selectedItem.secondaryContinuousAction = false;
                        }
                    }
                }
            }
            _singleStep = false;
        }
Example #5
0
 // startPersistentSystems -- Certain systems (such as WorldMap, Level, Player, etc...) that need to be active throughout the entire game
 public void startPersistentSystems()
 {
     _playerSystem = new PlayerSystem(_systemManager, _entityManager);
     _equipmentSystem = new EquipmentSystem(_systemManager, _entityManager);
     _levelSystem = new LevelSystem(this, _systemManager, _entityManager);
     _systemManager.add(_playerSystem, -1);
     _systemManager.add(_equipmentSystem, -1);
     _systemManager.add(_levelSystem, -1);
 }
Example #6
0
 // Create systems
 public void createLevelSystems()
 {
     _systemManager.add(new InputSystem(_systemManager, _entityManager), -1);
     _systemManager.add(new PhysicsSystem(_systemManager, _entityManager), -1);
     _systemManager.add(new CameraSystem(_systemManager, _entityManager), -1);
     _systemManager.add(new EventSystem(_systemManager, _entityManager), -1);
     _systemManager.add(new RopeSystem(_systemManager, _entityManager), -1);
     _systemManager.add(new ExplosionSystem(_systemManager, _entityManager), -1);
     _renderSystem = new RenderSystem(_game, _systemManager, _entityManager);
     _animationSystem = new AnimationSystem(_systemManager, _entityManager, _game.animationManager);
     _systemManager.add(_renderSystem, -1);
     _systemManager.add(_animationSystem, -1);
     _systemManager.add(new TreeSystem(_systemManager, _entityManager), -1);
     _systemManager.add(new FluidSystem(_systemManager, _entityManager), -1);
     _systemManager.add(new AIBehaviorSystem(_systemManager, _entityManager), -1);
     _systemManager.add(new DialogueSystem(_systemManager, _entityManager), -1);
     _playerSystem = (PlayerSystem)_systemManager.getSystem(SystemType.Player);
 }