Example #1
0
        public override void Initialize(ConfigSection section)
        {
            base.Initialize(section);

            textureName = section["texture"];
            circlePos = section["position"].AsVector2();
            Radius = section["radius"];

            var bodyDescription = new scBodyDescription();
            bodyDescription.bodyType = scBodyType.Kinematic;

            bodyDescription.transform.position = circlePos;
            bodyDescription.inertiaScale = section["inertiaScale"];
            bodyDescription.linearDamping = section["linearDamping"];
            bodyDescription.angularDamping = section["angularDamping"];

            bodyDescription.userData = this;

            var shape = new scCircleShape();
            shape.radius = Radius;

            var bodyPartDescription = new scBodyPartDescription();
            bodyPartDescription.restitution = section["restitution"];
            bodyPartDescription.density = section["density"];
            bodyPartDescription.shape = shape;
            bodyPartDescription.friction = section["friction"];

            Body = Game.level.World.createBody(bodyDescription, bodyPartDescription);
            Body.owner = this;
        }
Example #2
0
        public scBody createBody(scBodyDescription description, IList<scBodyPartDescription> bodyPartDescriptions)
        {
            var body = new scBody();
            body.transform = description.transform;
            body.bodyType = description.bodyType;

            foreach (var bodyPartDescription in bodyPartDescriptions)
            {
                var bodyPart = new scBodyPart();
                bodyPart.shape = bodyPartDescription.shape;
                bodyPart.userData = bodyPartDescription.userData;

                body.bodyParts.Add(bodyPart);
            }

            bodies.Add(body);
            if (bodyAdded != null)
            {
                bodyAdded(body);
            }
            return body;
        }
Example #3
0
        public void Initialize(ConfigOption option)
        {
            levelConfig = ConfigFile.FromFile(option.Value);

            {
                var physicsSection = levelConfig["Physics"];

                PhysicsScale = physicsSection["scale"];
                GroundFriction = physicsSection["groundFriction"];
                World = new scPhysicsWorld();
                World.game = game;

                physicsSection.IfOptionExists("gravity", o => World.gravity = o.AsVector2());
                //                physicsSection["gravity"].AsVector2(), physicsSection["doSleep"].AsBool()
                //                World.ContinuousPhysics = physicsSection["continuousPhysics"].AsBool();

                physicsWorldDebugRenderer = new scPhysicsWorldDebugRenderer();
                physicsWorldDebugRenderer.world = World;
            }

            LevelGenerator = new LevelGenerator(this);
            //            bodyList = LevelGenerator.generateLevel();

            var viewport = game.GraphicsDevice.Viewport;

            InitializePlayers();

            camera = new Camera2D(game);
            camera.Initialize();
            camera.Position = levelConfig["Camera"]["startPos"].AsVector2();
            camera.ViewBounds = levelConfig["Camera"]["viewBounds"].AsRectangle();
            camera.MaxMoveSpeed = levelConfig["Camera"]["maxMoveSpeed"];

            CreatePhysicalViewBounds();

            var gameObjectsConfig = ConfigFile.FromFile(levelConfig.GlobalSection["objects"]);

            foreach (var section in gameObjectsConfig.Sections)
            {
                if (section.Value == gameObjectsConfig.GlobalSection)
                {
                    // Skip the global section because it can not contain any useful info in this case.
                    continue;
                }
                var go = GameObject.Create(game, section.Key, section.Value);
                GameObjects.Add(go);
            }

            Menu.InitializeFromConfigFile(levelConfig.GlobalSection["userInterface"].AsConfigFile());

            levelConfig.IfSectionExists("Audio",
                section =>
                {
                    section.IfOptionExists("ambientCue", opt => AmbientMusicCue = opt);
                });

            if (!levelConfig.Sections.ContainsKey("Debug"))
            {
                return;
            }

            var debugSection = levelConfig["Debug"];

            debugSection.IfOptionExists("drawPhysics", opt => game.drawPhysics = opt.AsBool());
            debugSection.IfOptionExists("drawVisualHelpers", opt =>
                {
                    if (opt.AsBool())
                        game.drawVisualHelpers.SetNormal();
                    else
                        game.drawVisualHelpers.SetNone();
                });
            debugSection.IfOptionExists("drawDebugData", opt =>
            {
                if (opt.AsBool())
                    game.drawDebugData.SetNormal();
                else
                    game.drawDebugData.SetNone();
            });

            {
                var circleshape = new scCircleShape();
                circleshape.radius = 75;
                var bodyPartDescription = new scBodyPartDescription();
                bodyPartDescription.shape = circleshape;
                var bodyDescription = new scBodyDescription();
                bodyDescription.bodyType = scBodyType.Static;
                bodyDescription.transform.position = new Vector2(-150, 0);
                var bodyPartDescriptions = new List<scBodyPartDescription>();
                bodyPartDescriptions.Add(bodyPartDescription);
                var body = World.createBody(bodyDescription, bodyPartDescriptions);
            }

            {
                var rectangleshape = scRectangleShape.fromLocalPositionAndHalfExtents(new Vector2(0, 0), new Vector2(75, 30));
                var bodyPartDescription = new scBodyPartDescription();
                bodyPartDescription.shape = rectangleshape;
                var bodyDescription = new scBodyDescription();
                bodyDescription.bodyType = scBodyType.Static;
                bodyDescription.transform.position = new Vector2(50, 150);
                var bodyPartDescriptions = new List<scBodyPartDescription>();
                bodyPartDescriptions.Add(bodyPartDescription);
                var body = World.createBody(bodyDescription, bodyPartDescriptions);
            }

            {
                var edgeshape = new scEdgeShape();
                edgeshape.start = new Vector2(-20, -40);
                edgeshape.end = new Vector2(20, 40);
                var bodyPartDescription = new scBodyPartDescription();
                bodyPartDescription.shape = edgeshape;
                var bodyDescription = new scBodyDescription();
                bodyDescription.bodyType = scBodyType.Static;
                bodyDescription.transform.position = new Vector2(100, 50);
                var bodyPartDescriptions = new List<scBodyPartDescription>();
                bodyPartDescriptions.Add(bodyPartDescription);
                var body = World.createBody(bodyDescription, bodyPartDescriptions);
                TESTEDGE = body;
            }

            {
                var rectangleshape = scRectangleShape.fromLocalPositionAndHalfExtents(new Vector2(0, 0), new Vector2(40, 40));
                var bodyPartDescription = new scBodyPartDescription();
                bodyPartDescription.shape = rectangleshape;
                var bodyDescription = new scBodyDescription();
                bodyDescription.bodyType = scBodyType.Static;
                bodyDescription.transform.position = new Vector2(200, 50);
                var bodyPartDescriptions = new List<scBodyPartDescription>();
                bodyPartDescriptions.Add(bodyPartDescription);
                var body = World.createBody(bodyDescription, bodyPartDescriptions);
                TESTRECTANGLE = body;
            }
        }
Example #4
0
        public List<scBody> generateLevel()
        {
            String pathToCollisionFile = level.levelConfig.GlobalSection["collision"];

            var vertices = new Vector2[4];

            var startPointsStatic = new List<Vector2>();

            var grays = new List<Vector2>();

            var map = new Bitmap(pathToCollisionFile);

            for (int x = 0; x < map.Size.Width; ++x)
            {
                for (int y = 0; y < map.Size.Height; ++y)
                {
                    var pixel = map.GetPixel(x, y);
                    if (isRed(pixel))
                    {
                        startPointsStatic.Add(new Vector2(x, y));
                    }
                }
            }

            foreach (var startPoint in startPointsStatic)
            {
                vertices = GetVertices(map, startPoint);

                var edges = new List<EdgeInfo>();

                if (vertices.Length < 2)
                {
                    throw new Exception();
                }

                for (int i = 1; i < vertices.Length; ++i)
                {
                    var vertexA = vertices[i - 1];
                    var vertexB = vertices[i];

                    var edgeInfo = new EdgeInfo();
                    edgeInfo.shape = new scEdgeShape();
                    edgeInfo.isVertical = vertexA.X == vertexB.X;
                    edgeInfo.shape.start = vertexA;
                    edgeInfo.shape.end = vertexB;
                    edges.Add(edgeInfo);

                }

                IList<scBodyPartDescription> bodyPartDescriptions = new List<scBodyPartDescription>();

                foreach (var edge in edges)
                {
                    var bodyPartDescription = new scBodyPartDescription();
                    var elementInfo = new LevelElementInfo();
                    bodyPartDescription.shape = edge.shape;
                    if (edge.isVertical)
                    {
                        bodyPartDescription.friction = 0.0f;
                        elementInfo.type = LevelElementType.Wall;
                    }
                    else
                    {
                        bodyPartDescription.friction = level.GroundFriction;
                        elementInfo.type = LevelElementType.Ground;
                    }
                    bodyPartDescription.userData = elementInfo;
                    bodyPartDescriptions.Add(bodyPartDescription);
                }

                var bodyDescription = new scBodyDescription();
                bodyDescription.bodyType = scBodyType.Static;

                bodyDescription.transform.rotation.radians = 0;
                bodyDescription.transform.position = startPoint;

                var body = level.World.createBody(bodyDescription, bodyPartDescriptions);

                bodyList.Add(body);
            }

            return bodyList;
        }
Example #5
0
        public override void Initialize(ConfigSection section)
        {
            base.Initialize(section);

            TextureName = section["texture"];
            _dimensions = section["dimensions"].AsVector2();
            section.IfOptionExists("toggleEvent",
                opt => Game.Events[opt].addListener(onToggleEvent));

            section.IfOptionExists("fadeTime", opt => FadeTime = opt);

            var stateName = section["state"];

            if (stateName == "active")
            {
                State.setActive();
                CurrentFadeTime = FadeTime;
            }
            else if (stateName == "inactive")
            {
                State.setInactive();
                CurrentFadeTime = 0.0f;
            }
            else
            {
                throw new ArgumentException("Unsupported GameObject state: " + stateName);
            }

            section.IfOptionExists("movementSpeed", opt => MovementSpeed = opt);

            section.IfOptionExists("toggleWaypointEvent", opt => Game.Events[opt].addListener(onToggleWaypointEvent));

            var bodyDescription = new scBodyDescription();
            bodyDescription.userData = this;
            var bodyPartDescription = new scBodyPartDescription();
            var shape = scRectangleShape.fromLocalPositionAndHalfExtents(Vector2.Zero, _dimensions / 2);
            bodyPartDescription.shape = shape;
            bodyDescription.transform.position = section["position"].AsVector2();
            Body = Game.level.World.createBody(bodyDescription, bodyPartDescription);
            Body.owner = this;

            section.IfOptionExists("waypointStart",
                opt => WaypointStart = opt.AsVector2(),
                () => WaypointStart = Pos);

            section.IfOptionExists("waypointEnd",
                opt => WaypointEnd = opt.AsVector2(),
                () => WaypointEnd = Pos);

            section.IfOptionExists("target",
                targetName =>
                {
                    if (targetName == "start")
                    {
                        _targetIndex.Value = 0;
                    }
                    else if (targetName == "end")
                    {
                        _targetIndex.Value = 1;
                    }
                    else
                    {
                        throw new ArgumentException("Unsupported target name: " + targetName);
                    }
                },
                () => _targetIndex.Value = 1);
        }
Example #6
0
        public override void Initialize(ConfigSection section)
        {
            base.Initialize(section);

            if (section.Options.ContainsKey("texture"))
            {
                _textureName = section["texture"];
            }

            var pos = section["position"].AsVector2();
            _dim = section["dimensions"].AsVector2();

            if (section.Options.ContainsKey("enterEvent"))
            {
                _enterEvent = section["enterEvent"];
            }

            if (section.Options.ContainsKey("leaveEvent"))
            {
                _leaveEvent = section["leaveEvent"];
            }

            if (section.Options.ContainsKey("enterEventData"))
            {
                _enterEventData = section["enterEventData"];
            }

            if (section.Options.ContainsKey("leaveEventData"))
            {
                _leaveEventData = section["leaveEventData"];
            }

            var bodyDescription = new scBodyDescription();
            bodyDescription.userData = this;
            var bodyPartDescription = new scBodyPartDescription();
            var shape = scRectangleShape.fromLocalPositionAndHalfExtents(Vector2.Zero, _dim / 2);
            bodyPartDescription.shape = shape;
            bodyPartDescription.isTrigger = true;
            bodyDescription.transform.position = pos;
            Body = Game.level.World.createBody(bodyDescription, bodyPartDescription);
            Body.owner = this;
        }
Example #7
0
        public override void Initialize(ConfigSection section)
        {
            base.Initialize(section);

            var bodyDescription = new scBodyDescription();
            bodyDescription.bodyType = scBodyType.Static;
            bodyDescription.transform.position = section["position"].AsVector2();
            bodyDescription.userData = this;

            var bodyPartDescription = new scBodyPartDescription();
            bodyPartDescription.shape = new scCircleShape() { radius = section["proximityRadius"] };
            bodyPartDescription.isTrigger = true;
            bodyPartDescription.userData = this;

            Body = Game.level.World.createBody(bodyDescription, bodyPartDescription);

            Body.beginCollision += (other) => { if (other == Master.Body) IsMasterInProximity = true; };
            Body.endCollision += (other) => { if (other == Master.Body) IsMasterInProximity = false; };

            TextureOnName = section["textureOn"];
            TextureOffName = section["textureOff"];

            string masterName = section["master"];

            Master = Game.level.GetGameObject(masterName);

            var state = section["state"];

            if (state == "active")
            {
                InitialOnOffState.setActive();
            }
            else if (state == "inactive")
            {
                InitialOnOffState.setInactive();
            }
            else
            {
                throw new ArgumentException("Unsupported GameObject state: " + state);
            }

            OnOffState.Value = InitialOnOffState.Value;

            if (section.Options.ContainsKey("onButtonOn"))
            {
                OnEvent = Game.Events[section["onButtonOn"]];
            }

            if (section.Options.ContainsKey("onButtonOnData"))
            {
                OnEventData = section["onButtonOnData"];
            }

            if (section.Options.ContainsKey("onButtonOff"))
            {
                OffEvent = Game.Events[section["onButtonOff"]];
            }

            if (section.Options.ContainsKey("onButtonOffData"))
            {
                OffEventData = section["onButtonOffData"];
            }

            Game.Events["playerButtonPress"].addListener(OnPlayerButtonPress);
            Game.Events["playerButtonRelease"].addListener(OnPlayerButtonRelease);
        }
Example #8
0
 public scBody createBody(scBodyDescription description, scBodyPartDescription bodyPartDescription)
 {
     var parts = new List<scBodyPartDescription>();
     parts.Add(bodyPartDescription);
     return createBody(description, parts);
 }
Example #9
0
        public override void Initialize(ConfigSection section)
        {
            base.Initialize(section);

            textureName = section["texture"];
            var position = section["position"].AsVector2();
            SideLength = section["sideLength"];
            Speed = section["speed"];
            MaxSpeed = section["maxSpeed"];
            JumpImpulse = section["jumpImpulse"];
            JumpThreshold = section["jumpThreshold"];

            var bodyDescription = new scBodyDescription();
            bodyDescription.bodyType = scBodyType.Kinematic;
            bodyDescription.transform.position = position;
            bodyDescription.inertiaScale = section["inertiaScale"];
            bodyDescription.linearDamping = section["linearDamping"];
            bodyDescription.angularDamping = section["angularDamping"];
            bodyDescription.userData = this;

            var bodyPartDescription = new scBodyPartDescription();

            bodyPartDescription.shape = scRectangleShape.fromHalfExtents(new Vector2(SideLength / 2));
            bodyPartDescription.friction = section["friction"];
            bodyPartDescription.restitution = section["restitution"];
            bodyPartDescription.density = section["density"];

            Body = Game.level.World.createBody(bodyDescription, bodyPartDescription);
            Body.owner = this;
        }