Example #1
0
        public void update(GameTime gameTime)
        {
            if (!_paused || _singleStep)
            {
                LevelSystem levelSystem = _systemManager.getSystem(SystemType.Level) as LevelSystem;

                if (levelSystem.finalized)
                {
                    string     levelUid          = LevelSystem.currentLevelUid;
                    List <int> dynamiteEntities  = _entityManager.getEntitiesPosessing(levelUid, ComponentType.Dynamite);
                    List <int> explosionEntities = _entityManager.getEntitiesPosessing(levelUid, ComponentType.Explosion);
                    List <int> debrisEntities    = _entityManager.getEntitiesPosessing(levelUid, ComponentType.Debris);

                    // Dynamite entities
                    for (int i = 0; i < dynamiteEntities.Count; i++)
                    {
                        DynamiteComponent dynamiteComponent = (DynamiteComponent)_entityManager.getComponent(levelUid, dynamiteEntities[i], ComponentType.Dynamite);

                        if (dynamiteComponent.timeToLive > 0)
                        {
                            dynamiteComponent.timeToLive--;
                        }
                        else
                        {
                            explodeDynamite(levelUid, dynamiteEntities[i], dynamiteComponent);
                        }
                    }

                    // Explosion entities -- Explosion contact logic is handled by the PhysicsSystem's contact listeners. This just removes them, since they only should exist for 1 frame
                    for (int i = 0; i < explosionEntities.Count; i++)
                    {
                        PhysicsComponent physicsComponent = (PhysicsComponent)_entityManager.getComponent(levelUid, explosionEntities[i], ComponentType.Physics);

                        physicsComponent.body.World.RemoveBody(physicsComponent.body);
                        _entityManager.killEntity(levelUid, explosionEntities[i]);
                    }

                    // Break fixtures and create debris
                    for (int i = 0; i < _debrisToCreate.Count; i++)
                    {
                        Fixture fixture = _debrisToCreate[i].fixture;

                        if (fixture.Shape != null)
                        {
                            int entityId = (int)fixture.Body.UserData;
                            PrimitivesRenderComponent primitiveRenderComponent = (PrimitivesRenderComponent)_entityManager.getComponent(levelUid, entityId, ComponentType.PrimitivesRender);
                            PrimitiveRenderObject     primitiveRenderObject    = primitiveRenderComponent.primitiveRenderObjects[0];
                            RenderableTriangle        triangleToRemove         = null;

                            for (int j = 0; j < primitiveRenderObject.renderableTriangles.Count; j++)
                            {
                                if (primitiveRenderObject.renderableTriangles[j].fixture == fixture)
                                {
                                    triangleToRemove = primitiveRenderObject.renderableTriangles[j];
                                    break;
                                }
                            }
                            if (triangleToRemove != null)
                            {
                                primitiveRenderObject.renderableTriangles.Remove(triangleToRemove);
                            }

                            _entityManager.factory.createDebris(levelUid, fixture, _debrisToCreate[i].force, _debrisToCreate[i].timeToLive, triangleToRemove, primitiveRenderObject.texture, primitiveRenderObject.layerDepth);
                            fixture.Body.DestroyFixture(fixture);
                        }
                    }
                    _debrisToCreate.Clear();

                    // Debris
                    for (int i = 0; i < debrisEntities.Count; i++)
                    {
                        DebrisComponent debrisComponent = (DebrisComponent)_entityManager.getComponent(levelUid, debrisEntities[i], ComponentType.Debris);
                        debrisComponent.timeToLive--;

                        if (debrisComponent.restitutionCount < DebrisComponent.RESTITUTION_RESTORE_COUNT)
                        {
                            debrisComponent.fixture.Restitution = debrisComponent.fixture.Restitution + debrisComponent.restitutionIncrement;
                        }

                        if (debrisComponent.timeToLive < 0)
                        {
                            killDebris(levelUid, debrisEntities[i]);
                        }
                    }
                }
            }
            _singleStep = false;
        }
Example #2
0
        // draw
        public void draw(GameTime gameTime)
        {
            string      levelUid                        = LevelSystem.currentLevelUid;
            FluidSystem fluidSystem                     = (FluidSystem)_systemManager.getSystem(SystemType.Fluid);
            List <int>  primitiveRenderEntities         = _entityManager.getEntitiesPosessing(levelUid, ComponentType.PrimitivesRender);
            List <int>  ropeEntities                    = _entityManager.getEntitiesPosessing(levelUid, ComponentType.Rope);
            List <int>  characterRenderEntities         = _entityManager.getEntitiesPosessing(levelUid, ComponentType.CharacterRender);
            List <int>  characterMovementEntities       = _entityManager.getEntitiesPosessing(levelUid, ComponentType.CharacterMovement);
            List <int>  treeEntities                    = _entityManager.getEntitiesPosessing(levelUid, ComponentType.Tree);
            List <int>  aimEntities                     = _entityManager.getEntitiesPosessing(levelUid, ComponentType.Aim);
            List <int>  explosionEntities               = _entityManager.getEntitiesPosessing(levelUid, ComponentType.Explosion);
            List <RopeGrabComponent> ropeGrabComponents = _entityManager.getComponents <RopeGrabComponent>(levelUid, ComponentType.RopeGrab);
            List <TooltipComponent>  tooltipComponents  = _entityManager.getComponents <TooltipComponent>(levelUid, ComponentType.Tooltip);
            Vector2 screenCenter                        = _cameraSystem.screenCenter;

            // Temporary debug draw
            if (LoderGame.debug)
            {
                Vector2 debugOffset = new Vector2(0f, 5f);
                _graphicsDevice.SetRenderTarget(_debugFluid);
                _graphicsDevice.Clear(Color.Black);
                _spriteBatch.Begin();

                // Cells
                foreach (KeyValuePair <int, Dictionary <int, List <int> > > row1Pair in fluidSystem.fluidGrid)
                {
                    foreach (KeyValuePair <int, List <int> > row2Pair in row1Pair.Value)
                    {
                        int     gridSize = (int)(FluidSystem.CELL_SPACING * scale) - 1;
                        Vector2 position = new Vector2((float)row1Pair.Key * FluidSystem.CELL_SPACING, (float)row2Pair.Key * FluidSystem.CELL_SPACING);
                        _spriteBatch.Draw(_pixel, (position - debugOffset) * scale + _halfScreen, new Rectangle(0, 0, gridSize, gridSize), Color.DarkBlue, 0f, Vector2.Zero, 1f, SpriteEffects.None, 0f);
                    }
                }

                // Particle pressures
                for (int i = 0; i < FluidSystem.MAX_PARTICLES; i++)
                {
                    Particle particle = fluidSystem.liquid[i];
                    _spriteBatch.Draw(_pixel, (particle.position - debugOffset) * scale + _halfScreen, new Rectangle(0, 0, 16, 16), Color.Red * 0.5f, 0f, new Vector2(8, 8), Math.Abs(particle.pressure) / FluidSystem.MAX_PRESSURE, SpriteEffects.None, 0f);
                }

                // Particle near pressures
                for (int i = 0; i < FluidSystem.MAX_PARTICLES; i++)
                {
                    Particle particle = fluidSystem.liquid[i];
                    _spriteBatch.Draw(_pixel, (particle.position - debugOffset) * scale + _halfScreen, new Rectangle(0, 0, 16, 16), Color.Orange * 0.5f, 0f, new Vector2(8, 8), Math.Abs(particle.pressureNear) / FluidSystem.MAX_PRESSURE_NEAR, SpriteEffects.None, 0f);
                }

                // Particle positions
                for (int i = 0; i < FluidSystem.MAX_PARTICLES; i++)
                {
                    Particle particle = fluidSystem.liquid[i];
                    Color    color    = particle.active ? Color.White : Color.DarkGray;
                    _spriteBatch.Draw(_pixel, (particle.position - debugOffset) * scale + _halfScreen, new Rectangle(0, 0, 4, 4), color, 0, new Vector2(2, 2), 1, SpriteEffects.None, 0);
                }

                // Simulation AABB
                Vector2[] vertices = new Vector2[4];
                vertices[0] = fluidSystem.simulationAABB.LowerBound;
                vertices[1] = new Vector2(fluidSystem.simulationAABB.UpperBound.X, fluidSystem.simulationAABB.LowerBound.Y);
                vertices[2] = fluidSystem.simulationAABB.UpperBound;
                vertices[3] = new Vector2(fluidSystem.simulationAABB.LowerBound.X, fluidSystem.simulationAABB.UpperBound.Y);
                for (int i = 0; i < 4; i++)
                {
                    Vector2   a        = vertices[i];
                    Vector2   b        = vertices[i == 3 ? 0 : i + 1];
                    Vector2   relative = b - a;
                    float     angle    = (float)Math.Atan2(relative.Y, relative.X);
                    Rectangle rect     = new Rectangle(0, 0, (int)(relative.Length() * scale), 2);
                    _spriteBatch.Draw(_pixel, (a - debugOffset) * scale + _halfScreen, rect, Color.Lime, angle, new Vector2(0, 1), 1f, SpriteEffects.None, 0);
                }

                _spriteBatch.End();
                _graphicsDevice.SetRenderTarget(null);
            }

            // Begin drawing a source for post effects under the player's level
            _graphicsDevice.SetRenderTarget(_postSourceUnder);
            _graphicsDevice.Clear(Color.Black);

            // Draw background
            if (_backgroundRenderer.background != null)
            {
                _spriteBatch.Begin(SpriteSortMode.BackToFront, BlendState.AlphaBlend);
                _backgroundRenderer.drawFirstHalf();
                _spriteBatch.End();
            }

            // Begin ordered drawing
            _spriteBatch.Begin(SpriteSortMode.BackToFront, BlendState.AlphaBlend);
            _halfScreen       = new Vector2(_graphicsDevice.Viewport.Width, _graphicsDevice.Viewport.Height) / 2;
            _viewMatrix       = Matrix.CreateTranslation(new Vector3(-screenCenter, 0)) * Matrix.CreateScale(new Vector3(_scale, -_scale, 1f));
            _projectionMatrix = Matrix.CreateOrthographic(_graphicsDevice.Viewport.Width, _graphicsDevice.Viewport.Height, 0, 1);
            _primitivesEffect.Parameters["view"].SetValue(_viewMatrix);
            _primitivesEffect.Parameters["projection"].SetValue(_projectionMatrix);

            // Primitive rendering
            for (int i = 0; i < primitiveRenderEntities.Count; i++)
            {
                int entityId = primitiveRenderEntities[i];
                PrimitivesRenderComponent primitiveRenderComponent = (PrimitivesRenderComponent)_entityManager.getComponent(levelUid, entityId, ComponentType.PrimitivesRender);

                for (int j = 0; j < primitiveRenderComponent.primitiveRenderObjects.Count; j++)
                {
                    PrimitiveRenderObject primitiveRenderObject = primitiveRenderComponent.primitiveRenderObjects[j];
                    PhysicsComponent      physicsComponent      = (PhysicsComponent)_entityManager.getComponent(levelUid, entityId, ComponentType.Physics);
                    IComponent            component;

                    // Update world matrix
                    if (physicsComponent != null)
                    {
                        primitiveRenderObject.worldMatrix = primitiveRenderObject.originMatrix * Matrix.CreateRotationZ(physicsComponent.body.Rotation) * Matrix.CreateTranslation(new Vector3(physicsComponent.body.Position, 0));
                    }
                    else if (_entityManager.tryGetComponent(levelUid, entityId, ComponentType.FollowMetamer, out component))
                    {
                        FollowMetamerComponent followMetamerComponent = component as FollowMetamerComponent;
                        primitiveRenderObject.worldMatrix = primitiveRenderObject.originMatrix * Matrix.CreateRotationZ(followMetamerComponent.metamer.currentAngle + StasisMathHelper.halfPi) * Matrix.CreateTranslation(new Vector3(followMetamerComponent.metamer.position, 0));
                    }

                    // Update vertices
                    primitiveRenderObject.updateVertices();

                    addRenderablePrimitive(primitiveRenderObject);
                }
            }

            // Rope rendering
            for (int i = 0; i < ropeEntities.Count; i++)
            {
                int           entityId      = ropeEntities[i];
                RopeComponent ropeComponent = _entityManager.getComponent(levelUid, entityId, ComponentType.Rope) as RopeComponent;
                RopeNode      current       = ropeComponent.ropeNodeHead;
                RopeNode      head          = current;
                RopeNode      tail          = head.tail;
                Vector2       position;
                float         muIncrement = 1f / (float)ropeComponent.interpolationCount;

                while (current != null)
                {
                    float mu = 0f;
                    for (int j = 0; j < ropeComponent.interpolationCount; j++)
                    {
                        Texture2D texture = current.ropeNodeTextures[j].texture;
                        Vector2   a;
                        Vector2   b = current.body.GetWorldPoint(new Vector2(current.halfLength, 0));
                        Vector2   c = current.body.GetWorldPoint(new Vector2(-current.halfLength, 0));
                        Vector2   d;

                        // Determine a's position
                        if (current.previous == null)
                        {
                            a = b + (b - c);
                        }
                        else
                        {
                            a = current.previous.body.GetWorldPoint(new Vector2(current.halfLength, 0));
                        }

                        // Determine d's position
                        if (current.next == null)
                        {
                            d = c + (c - b);
                        }
                        else
                        {
                            d = current.next.body.GetWorldPoint(new Vector2(-current.halfLength, 0));
                        }

                        StasisMathHelper.interpolate(ref a, ref b, ref c, ref d, mu, out position);
                        _spriteBatch.Draw(texture, (position - screenCenter) * _scale + _halfScreen, texture.Bounds, Color.White, current.body.Rotation + current.ropeNodeTextures[j].angleOffset, current.ropeNodeTextures[j].center, 1f, SpriteEffects.None, 0.1f);

                        mu += muIncrement;
                    }

                    current = current.next;
                }
            }

            // Character rendering
            for (int i = 0; i < characterRenderEntities.Count; i++)
            {
                PhysicsComponent         physicsComponent         = (PhysicsComponent)_entityManager.getComponent(levelUid, characterRenderEntities[i], ComponentType.Physics);
                CharacterRenderComponent characterRenderComponent = _entityManager.getComponent(levelUid, characterRenderEntities[i], ComponentType.CharacterRender) as CharacterRenderComponent;
                Vector2   offset;
                Texture2D texture = _animationManager.getTexture(characterRenderComponent.character, characterRenderComponent.animation, characterRenderComponent.currentFrame, out offset);

                _spriteBatch.Draw(texture, (physicsComponent.body.Position - screenCenter) * _scale + _halfScreen, texture.Bounds, Color.White, 0, offset, 1f, SpriteEffects.None, 0.05f);
            }

            /*
             * for (int i = 0; i < characterMovementEntities.Count; i++)
             * {
             *  PhysicsComponent physicsComponent = (PhysicsComponent)_entityManager.getComponent(levelUid, characterMovementEntities[i], ComponentType.Physics);
             *  CharacterMovementComponent characterMovementComponent = (CharacterMovementComponent)_entityManager.getComponent(levelUid, characterMovementEntities[i], ComponentType.CharacterMovement);
             *  Vector2 movementUnitVector = characterMovementComponent.movementUnitVector;
             *  Rectangle source = new Rectangle(0, 0, (int)(movementUnitVector.Length() * _scale), 2);
             *  float angle = (float)Math.Atan2(movementUnitVector.Y, movementUnitVector.X);
             *
             *  _spriteBatch.Draw(_pixel, (physicsComponent.body.Position - screenCenter) * _scale + _halfScreen, source, Color.Yellow, angle, new Vector2(0, 1), 1f, SpriteEffects.None, 0);
             * }*/

            // Tree
            _primitivesEffect.Parameters["world"].SetValue(Matrix.Identity);
            for (int i = 0; i < treeEntities.Count; i++)
            {
                TreeComponent treeComponent = _entityManager.getComponent(levelUid, treeEntities[i], ComponentType.Tree) as TreeComponent;

                if (treeComponent.tree.active)
                {
                    addRenderablePrimitive(treeComponent.tree);
                    treeComponent.tree.rootMetamer.draw(this);
                }
            }

            drawRenderablePrimitives();

            // Rope grab components (TEMPORARY)
            for (int i = 0; i < ropeGrabComponents.Count; i++)
            {
                foreach (KeyValuePair <Body, RevoluteJoint> pair in ropeGrabComponents[i].joints)
                {
                    Vector2 pointA   = pair.Value.BodyA.Position;
                    Vector2 pointB   = pair.Value.BodyB.Position;
                    Vector2 relative = pointB - pointA;
                    float   angle    = (float)Math.Atan2(relative.Y, relative.X);

                    _spriteBatch.Draw(_pixel, (pointA - screenCenter) * _scale + _halfScreen, new Rectangle(0, 0, (int)(relative.Length() * _scale), 2), Color.Green, angle, new Vector2(0, 1), 1f, SpriteEffects.None, 0f);
                }
            }

            // Draw explosions (TEMPORARY)
            for (int i = 0; i < explosionEntities.Count; i++)
            {
                ExplosionComponent explosionComponent = (ExplosionComponent)_entityManager.getComponent(levelUid, explosionEntities[i], ComponentType.Explosion);
                _spriteBatch.Draw(_circle, (explosionComponent.position - screenCenter) * _scale + _halfScreen, _circle.Bounds, Color.Red, 0f, new Vector2(_circle.Width, _circle.Height) / 2f, ((explosionComponent.radius * _scale) / (_circle.Width / 2f)), SpriteEffects.None, 0f);
            }

            // Aim components
            for (int i = 0; i < aimEntities.Count; i++)
            {
                AimComponent aimComponent  = (AimComponent)_entityManager.getComponent(levelUid, aimEntities[i], ComponentType.Aim);
                Vector2      worldPosition = (_entityManager.getComponent(levelUid, aimEntities[i], ComponentType.WorldPosition) as WorldPositionComponent).position;
                float        length        = aimComponent.length;

                _spriteBatch.Draw(_reticle, (worldPosition - screenCenter + new Vector2((float)Math.Cos(aimComponent.angle), (float)Math.Sin(aimComponent.angle)) * length) * _scale + _halfScreen, _reticle.Bounds, Color.Red, aimComponent.angle, new Vector2(_reticle.Width, _reticle.Height) / 2f, 1f, SpriteEffects.None, 0f);
            }

            _spriteBatch.End();

            // Begin drawing source for post effects over the player's layer
            _graphicsDevice.SetRenderTarget(_postSourceOver);
            _graphicsDevice.Clear(Color.Transparent);
            _spriteBatch.Begin(SpriteSortMode.BackToFront, BlendState.AlphaBlend);

            // Draw background's second half
            if (_backgroundRenderer.background != null)
            {
                _backgroundRenderer.drawSecondHalf();
            }

            // Draw tooltips
            for (int i = 0; i < tooltipComponents.Count; i++)
            {
                TooltipComponent tooltip = tooltipComponents[i];

                if (tooltip.draw)
                {
                    Vector2 tooltipPosition = (tooltip.position - screenCenter) * _scale + _halfScreen - new Vector2(0, 50f);

                    _spriteBatch.DrawString(_tooltipFont, tooltip.message, tooltipPosition + new Vector2(2, 2), Color.Black, 0f, Vector2.Zero, 1f, SpriteEffects.None, 0.0001f);
                    _spriteBatch.DrawString(_tooltipFont, tooltip.message, tooltipPosition, Color.White, 0f, Vector2.Zero, 1f, SpriteEffects.None, 0f);
                    tooltip.draw = false;
                }
            }

            _spriteBatch.End();
            _graphicsDevice.SetRenderTarget(null);
            _graphicsDevice.Clear(Color.Transparent);


            // Render fluid
            _graphicsDevice.SetRenderTarget(_fluidRenderTarget);
            _graphicsDevice.Clear(Color.Transparent);
            spriteBatch.Begin();
            int limit = fluidSystem.numActiveParticles;

            for (int i = 0; i < limit; i++)
            {
                // Current particle
                Particle particle = fluidSystem.liquid[fluidSystem.activeParticles[i]];
                Color    color    = new Color(1, particle.velocity.X < 0 ? -particle.velocity.X : particle.velocity.X, particle.velocity.Y < 0 ? -particle.velocity.Y : particle.velocity.Y);
                spriteBatch.Draw(_fluidParticleTexture, (particle.position - _cameraSystem.screenCenter) * scale + _halfScreen, _fluidParticleTexture.Bounds, color, 0, new Vector2(16, 16), 1, SpriteEffects.None, 0);
            }
            spriteBatch.End();
            _graphicsDevice.SetRenderTarget(_renderedFluid);
            _graphicsDevice.Clear(Color.Transparent);

            // Draw post-processed render target to screen
            _graphicsDevice.Textures[1] = _postSourceUnder;
            _fluidEffect.Parameters["renderSize"].SetValue(new Vector2(_renderedFluid.Width, _renderedFluid.Height));
            spriteBatch.Begin(SpriteSortMode.Immediate, BlendState.AlphaBlend, SamplerState.LinearClamp, null, null, _fluidEffect);
            spriteBatch.Draw(_fluidRenderTarget, Vector2.Zero, Color.DarkBlue);
            spriteBatch.End();
            _graphicsDevice.SetRenderTarget(null);

            // Draw post source under and over
            _spriteBatch.Begin();
            _spriteBatch.Draw(_postSourceUnder, _postSourceUnder.Bounds, Color.White);
            _spriteBatch.End();

            // Draw fluid
            if (fluidSystem != null)
            {
                _spriteBatch.Begin();
                _spriteBatch.Draw(_renderedFluid, _renderedFluid.Bounds, Color.White);
                _spriteBatch.End();
            }

            _spriteBatch.Begin(SpriteSortMode.Immediate, BlendState.AlphaBlend);
            _spriteBatch.Draw(_postSourceOver, _postSourceOver.Bounds, Color.White);
            _spriteBatch.End();

            // Particle debug
            if (LoderGame.debug)
            {
                _spriteBatch.Begin();
                _spriteBatch.Draw(_debugFluid, Vector2.Zero, _debugFluid.Bounds, Color.White, 0f, Vector2.Zero, _enlargeDebugFuild ? 1f : 0.25f, SpriteEffects.None, 0f);
                _spriteBatch.End();

                /*
                 * _spriteBatch.Begin();
                 * int limit = fluidSystem.numActiveParticles;
                 * for (int i = 0; i < limit; i++)
                 * {
                 *  // Current particle
                 *  Particle particle = fluidSystem.liquid[fluidSystem.activeParticles[i]];
                 *  spriteBatch.Draw(_pixel, (particle.position - _cameraSystem.screenCenter) * scale + _halfScreen, new Rectangle(0, 0, 2, 2), Color.White, 0, new Vector2(1, 1), 1, SpriteEffects.None, 0);
                 * }
                 * _spriteBatch.End();
                 */
            }

            // AI Wander Behavior debug
            if (LoderGame.debug)
            {
                AIBehaviorSystem aiBehaviorSystem       = _systemManager.getSystem(SystemType.AIBehavior) as AIBehaviorSystem;
                List <int>       wanderBehaviorEntities = _entityManager.getEntitiesPosessing(levelUid, ComponentType.AIWanderBehavior);

                for (int i = 0; i < wanderBehaviorEntities.Count; i++)
                {
                    AIWanderBehaviorComponent wanderComponent     = _entityManager.getComponent(levelUid, wanderBehaviorEntities[i], ComponentType.AIWanderBehavior) as AIWanderBehaviorComponent;
                    List <WaypointsComponent> waypointsComponents = _entityManager.getComponents <WaypointsComponent>(levelUid, ComponentType.Waypoints);

                    if (waypointsComponents.Count > 0)
                    {
                        WaypointsComponent waypointsComponent = aiBehaviorSystem.getWaypointsComponent(wanderComponent.waypointsUid, waypointsComponents);
                        Vector2            waypointPosition   = waypointsComponent.waypoints[wanderComponent.currentWaypointIndex];

                        _spriteBatch.Begin();
                        _spriteBatch.Draw(_circle, (waypointPosition - screenCenter) * _scale + _halfScreen, _circle.Bounds, Color.Red, 0f, new Vector2(_circle.Width, _circle.Height) / 2f, 0.02f, SpriteEffects.None, 0f);
                        _spriteBatch.End();
                    }
                }
            }
        }