Пример #1
0
        public override void Initialise()
        {
            // Initialise and fade in
            GameGlobal.InitialiseAssets();
            GameGlobal.Fader.RunFunction("FadeIn");

            // DebugText
            DebugText = new Entity(entity => {
                entity.Position = new Vector2(-55, 20);

                entity.AddComponent(new Text()).Run <Text>(text => {
                    string temp = "";
                    foreach (var item in DebugData)
                    {
                        temp += item.Key + ": " + item.Value + "\n";
                    }

                    text.String = temp;

                    CoroutineHelper.Always(() => {
                        //text.Visible = (OptionSelector.SelectedOption.ID == "debug");
                    });
                });
            });

            MainMenu();
        }
Пример #2
0
        public override void Initialise()
        {
            // Initialise and
            GameGlobal.InitialiseAssets();

            // DEBUGGING
            #region Debugging
            // Debug text
            new Entity(entity => {
                entity.SortingLayer = 1;
                entity.LayerName    = "Fade";
                entity.Position     = -(Global.Resolution.ToVector2() / 2);
                entity.AddComponent(new Text()).Run <Text>(component => {
                    component.SetSpriteFont("HeartbitXX");
                    component.Color = Color.Yellow;

                    entity.UpdateAction = e => {
                        component.String = "";
                        //component.String = "Entities: " + Global.CountEntities().ToString() + ", Coroutines: " + Coroutines.Count.ToString() + ", FPS: " + Global.FPS.ToString();
                        //component.String = String.Join(", ", GameGlobal.Player.GetComponent<MainCollider>().PrevCollidingTriggers.Select(x => x.Name).ToArray());
                        component.String      += (GameGlobal.DebugString.Length > 0) ? " | " + GameGlobal.DebugString : "";
                        GameGlobal.DebugString = "";
                    };
                });
            });

            // Hotspot tester
            hotspotTest = new Entity(entity => {
                entity.AddComponent(new Sprite()).Run <Sprite>(d => {
                    d.BuildRectangle(new Point(4, 4), Color.Orange);
                });
                entity.SortingLayer = 10;
            });
            CoroutineHelper.Always(() => {
                //hotspotTest.Position = new Vector2(textTest.BoundingBox.Left + textTest.Size.X / 2, textTest.BoundingBox.Bottom);
            });
            #endregion

            // Backgrounds
            #region Backgrounds

            /*
             * new Entity(entity => {
             *  entity.LayerName = "Background";
             *  entity.Position += new Vector2(0, 80);
             *  entity.AddComponent(new Drawable() { Texture2D = Global.Content.Load<Texture2D>("WaterTest") }).Run<Drawable>(component => {
             *      Color[] colorArr = component.Texture2D.To1DArray();
             *      CoroutineHelper.Always(() => {
             *          component.Texture2D.ManipulateColors1D(colors => {
             *              colors = (Color[])colorArr.Clone();
             *              for (int y = 0; y < 100; y++)
             *                  colors.Shift(new Point(256, 100), new Rectangle(0, 1 * y, 256, 1), new Point(-(int)(Global.Camera.Position.X * (y + 1) * 0.005f), 1 * y));
             *              return colors;
             *          });
             *      });
             *  });
             * });*/
            #endregion

            // Player entity
            GameGlobal.Player = new Entity();
            GameGlobal.Player.AddComponent(new PlayerController());

            Global.AudioController.MusicSetThisFrame = false;

            // Temp
            RenderBlender = new RenderBlender(
                Color.Black * 0f,
                new List <RenderBlender.DrawableTexture>()
            {
            }
                );

            // Load level
            LevelLoader levelLoader = new LevelLoader();
            levelLoader.Load(GameData.Get("Level"), (tileset, tiles, entities) => {
                // Create tilemap
                TileMap tileMap = new TileMap(new Point(16, 16), "Tilesets/" + tileset.Substring(0, tileset.LastIndexOf('.')), tiles);
                tileMap.Build(new Point(32, 32));

                // Map collision
                foreach (var tgroup in tileMap.TileGroupEntities)
                {
                    if (tgroup.SortingLayer == GameGlobal.Player.SortingLayer)
                    {
                        tgroup.AddComponent(new Collider()).Run <Collider>(c => { c.ColliderType = Collider.ColliderTypes.Pixel; });
                    }

                    //if (tgroup.SortingLayer == GameGlobal.Player.SortingLayer - 1)
                    //    tgroup.IsPlatform = true;
                }

                // Entities
                new EntityInfoInterpreter(entities);
            });

            // Camera
            CameraController = new CameraController
            {
                Target  = GameGlobal.PlayerGraphicEntity,
                Easing  = 0.08f,
                MaxStep = 1000
            };
            CameraController.SnapOnce();
            CameraController.Mode = CameraController.Modes.LerpToTarget;

            // Level script
            LevelScripts levelScripts = new LevelScripts();
            Type         type         = levelScripts.GetType();
            MethodInfo   method       = type.GetMethod(GameData.Get("Level"));

            if (method != null)
            {
                method.Invoke(levelScripts, new object[0]);
            }

            if (!Global.AudioController.MusicSetThisFrame)
            {
                Global.AudioController.MusicFadeOut();
            }

            if (GameGlobal.Fader.Data["Cancel"] != "true")
            {
                // This delay is currently for the camera repositioning if min / max are applied
                CoroutineHelper.WaitRun(0.5f, () => {
                    GameGlobal.Fader.RunFunction("FadeIn");
                });
            }
        }
        public void Interpret(EntityInfo entityInfo)
        {
            if (entityInfo.Name == "StartPosition" && GameData.Get("Player/Position") == null)
            {
                GameGlobal.Player.Position = new Vector2(entityInfo.Position.X * 16, entityInfo.Position.Y * 16);
            }
            else if (entityInfo.Name == "Switch")
            {
                new Entity(entity => {
                    entity.AddComponent(new Switch(entityInfo));
                });
            }
            else if (entityInfo.Name == "MovableObject")
            {
                ZInterpreter data = new ZInterpreter(entityInfo.Data);
                new Entity(entity => {
                    entity.LayerName    = "Main";
                    entity.SortingLayer = GameGlobal.PlayerGraphicEntity.SortingLayer - 1;
                    entity.Position     = (entityInfo.Position * 16) + (entityInfo.Size.ToVector2() / 2) * 16;
                    entity.AddComponent(new Sprite()).Run <Sprite>(sprite => {
                        sprite.LoadTexture("Graphics/WoodenBox");
                    });
                    entity.AddComponent(new MoveableObject());
                    if (data.GetString("id") != null)
                    {
                        entity.Data.Add("PO_ID", data.GetString("id"));
                        string poidposstr = GameData.Get("PO_ID:" + entity.Data["PO_ID"] + "/Position");
                        if (poidposstr != null)
                        {
                            string[] poidpos = poidposstr.Split(',');
                            entity.Position  = new Vector2((float)Convert.ToDouble(poidpos[0]), (int)Convert.ToDouble(poidpos[1]));
                        }
                    }
                });
            }
            else if (entityInfo.Name == "MovablePlatform")
            {
                ZInterpreter data = new ZInterpreter(entityInfo.Data);
                new Entity(entity => {
                    entity.LayerName    = "Main";
                    entity.SortingLayer = 10;
                    entity.Position     = entityInfo.Position * 16;
                    entity.AddComponent(new Sprite()).Run <Sprite>(sprite => {
                        sprite.BuildRectangle(new Point(32, 16), Color.White);
                    });

                    entity.AddComponent(new MovablePlatform(entity.Position)).Run <MovablePlatform>(mp => {
                        mp.Speed     = (data.HasKey("speed")) ? data.GetFloat("speed") : 1;
                        mp.XDistance = (data.HasKey("xdistance")) ? data.GetFloat("xdistance") : 0;
                        mp.YDistance = (data.HasKey("ydistance")) ? data.GetFloat("ydistance") : 0;
                    });
                });
            }
            else if (entityInfo.Name == "LightSource")
            {
                ZInterpreter data = new ZInterpreter(entityInfo.Data);

                // Pre set light sources
                if (data.HasKey("type"))
                {
                    if (data.GetString("type") == "OrangeTorch")
                    {
                        Level.RenderBlender.DrawableTextures.Add(new RenderBlender.DrawableTexture("alphamask2")
                        {
                            Position = (entityInfo.Position * 16),
                            Blend    = RenderBlender.Subtract,
                            Update   = item => { item.Scale = 1.6f; }
                        });
                        Level.RenderBlender.DrawableTextures.Add(new RenderBlender.DrawableTexture("alphamask2")
                        {
                            Position = (entityInfo.Position * 16),
                            Color    = Color.Orange * 0.1f,
                            Update   = item => { item.Scale = 0.6f + 0.04f * (float)Math.Sin(GameGlobal.TimeLoop * 50); }
                        });
                    }
                    else if (data.GetString("type") == "BlueMushroom")
                    {
                        Level.RenderBlender.DrawableTextures.Add(new RenderBlender.DrawableTexture("alphamask2")
                        {
                            Position = (entityInfo.Position * 16),
                            Blend    = RenderBlender.Subtract,
                            Color    = Color.White * 0.8f,
                            Update   = item => { item.Scale = 1.3f; }
                        });
                        Level.RenderBlender.DrawableTextures.Add(new RenderBlender.DrawableTexture("alphamask")
                        {
                            Position = (entityInfo.Position * 16),
                            Color    = Color.DeepSkyBlue * 0.2f,
                            Update   = item => { item.Scale = 0.6f + 0.03f * (float)Math.Sin(GameGlobal.TimeLoop * 3); }
                        });
                    }
                }
                // Custom light sources
                else
                {
                    Level.RenderBlender.DrawableTextures.Add(new RenderBlender.DrawableTexture(data.GetString("texture") != "" ? data.GetString("texture"): "White")
                    {
                        Position = (entityInfo.Position * 16) + data.GetPointArr("offset")[0].ToVector2(),
                        Blend    = (data.GetString("blendstate") == "Subtract") ? RenderBlender.Subtract : RenderBlender.Lighting,
                        Color    = GameMethods.GetProperty <Color>(data.GetString("color")),
                        Scale    = data.GetFloat("scale"),
                        Layer    = data.GetInt("layer")
                    });
                }
            }
            else if (entityInfo.Name == "Lighting")
            {
                ZInterpreter data = new ZInterpreter(entityInfo.Data);

                Level.RenderBlender.ClearColor = Color.Black * data.GetFloat("darkness");
            }
            else if (entityInfo.Name == "SavePoint")
            {
                new SavePoint(entityInfo);
            }
            else if (entityInfo.Name == "Enemy")
            {
                ZInterpreter data = new ZInterpreter(entityInfo.Data);

                Type type = Type.GetType("MyGame.Enemies." + data.GetString("type"));
                Activator.CreateInstance(type, new object[] { entityInfo });
            }
            else if (entityInfo.Name == "Audio2D")
            {
                ZInterpreter data = new ZInterpreter(entityInfo.Data);

                Vector2 position  = (entityInfo.Position * 16) + (entityInfo.Size.ToVector2() / 2) * 16;
                float   distance  = ((data.HasKey("distance")) ? data.GetFloat("distance") : 5) * 16;
                float   minvolume = ((data.HasKey("minvolume")) ? data.GetFloat("minvolume") : 0);
                float   maxvolume = ((data.HasKey("maxvolume")) ? data.GetFloat("maxvolume") : 1);

                SoundEffectInstance sfi = Global.AudioController.Play("SFX/" + data.GetString("file"));
                sfi.Volume = 0;
                CoroutineHelper.Always(() => {
                    if (Global.RunWhenEventLoops("ReplaySFX_" + data.GetString("file"), sfi == null || sfi.State != SoundState.Playing))
                    {
                        sfi = Global.AudioController.Play("SFX/" + data.GetString("file"));
                    }

                    float difference = GameGlobal.Player.Position.GetDistance(position);
                    float percent    = (((difference - distance) * 100) / ((maxvolume) - distance)).Between(minvolume, maxvolume);
                    sfi.Volume       = (percent / 100);
                });

                Global.SceneManager.CurrentScene.OnExit += () => {
                    Global.AudioController.Stop("SFX/" + data.GetString("file"));
                };
            }
            else if (entityInfo.Name == "InteractScript")
            {
                ZInterpreter data = new ZInterpreter(entityInfo.Data);

                new Entity(entity => {
                    entity.Name      = "InteractScript";
                    entity.LayerName = "Main";

                    entity.Data.Add("Script", data.GetString("script"));

                    entity.SortingLayer = GameGlobal.Player.SortingLayer;
                    entity.Position     = (entityInfo.Position * 16) + (entityInfo.Size.ToVector2() / 2) * 16;
                    entity.AddComponent(new Drawable()).Run <Drawable>(d => {
                        d.BuildRectangle(new Point(entityInfo.Size.X * 16, entityInfo.Size.Y * 16), Color.Blue);
                        d.Visible = false;
                    });

                    entity.AddComponent(new Collider()).Run <Collider>(c => { c.TriggerType = Collider.TriggerTypes.NonSolid; c.ColliderType = Collider.ColliderTypes.Box; });

                    if (data.HasKey("autoscript"))
                    {
                        CoroutineHelper.WaitRun(0.1f, () => {
                            string[] script = data.GetString("autoscript").Split('.');
                            Type type       = Type.GetType("MyGame." + script[0]);
                            MethodInfo mi   = type.GetMethod(script[1], BindingFlags.Static | BindingFlags.Public);
                            mi.Invoke(null, new object[] { entity });
                        });
                    }
                });
            }
            else if (entityInfo.Name == "TouchScript")
            {
                ZInterpreter data = new ZInterpreter(entityInfo.Data);

                new Entity(entity => {
                    entity.Name      = "TouchScript";
                    entity.LayerName = "Main";

                    entity.Data.Add("Script", data.GetString("script"));

                    entity.SortingLayer = GameGlobal.Player.SortingLayer;
                    entity.Position     = (entityInfo.Position * 16) + (entityInfo.Size.ToVector2() / 2) * 16;
                    entity.AddComponent(new Drawable()).Run <Drawable>(d => {
                        d.BuildRectangle(new Point(entityInfo.Size.X * 16, entityInfo.Size.Y * 16), Color.Blue);
                        d.Visible = false;
                    });

                    entity.AddComponent(new Collider()).Run <Collider>(c => { c.TriggerType = Collider.TriggerTypes.NonSolid; c.ColliderType = Collider.ColliderTypes.Box; });
                });
            }
            else if (entityInfo.Name == "Ambience")
            {
                ZInterpreter data = new ZInterpreter(entityInfo.Data);

                SoundEffectInstance sfi = Global.AudioController.Play("SFX/" + data.GetString("file"));
                sfi.Volume = (data.HasKey("volume")) ? data.GetFloat("volume") : 1;
                CoroutineHelper.Always(() => {
                    if (sfi.State == SoundState.Stopped)
                    {
                        sfi        = Global.AudioController.Play("SFX/" + data.GetString("file"));
                        sfi.Volume = (data.HasKey("volume")) ? data.GetFloat("volume") : 1;
                    }
                });

                Global.SceneManager.CurrentScene.OnExit += () => {
                    Global.AudioController.Stop("SFX/" + data.GetString("file"));
                };
            }
            else if (entityInfo.Name == "Music")
            {
                ZInterpreter data = new ZInterpreter(entityInfo.Data);

                float fadeTo = (data.HasKey("fadeTo")) ? data.GetFloat("fadeTo") : 1;

                if (Global.AudioController.CurrentMusicFile == null || Global.AudioController.CurrentMusicFile == "")
                {
                    Global.AudioController.PlayMusic(data.GetString("file"));
                    Global.AudioController.CurrentMusicVolume = fadeTo;
                }
                else
                {
                    Global.AudioController.PlayMusic(data.GetString("file"), fadeTo);
                }
            }
            else if (entityInfo.Name == "NPCChest")
            {
                ZInterpreter data = new ZInterpreter(entityInfo.Data);

                NPCChest.Create(entityInfo, data);
            }
            else if (entityInfo.Name == "Chest")
            {
                ZInterpreter data = new ZInterpreter(entityInfo.Data);

                new Entity(entity => {
                    entity.Name         = "Chest";
                    entity.LayerName    = "Main";
                    entity.SortingLayer = GameGlobal.Player.SortingLayer - 1;
                    entity.Position     = (entityInfo.Position * 16) + (entityInfo.Size.ToVector2() / 2) * 16;
                    entity.AddComponent(new Sprite()).Run <Sprite>(d => {
                        d.LoadTexture("Entities/Chest_" + data.GetString("type"));
                        d.AddAnimation(new Animation("Closed", 0, new Point(32, 32), new Point(0, 0)));
                        d.AddAnimation(new Animation("Opening", 0.1f, new Point(32, 32), new Point(1, 0), new Point(2, 0), new Point(3, 0), new Point(4, 0)));
                        d.RunAnimation("Closed");
                    });

                    entity.AddComponent(new Collider()).Run <Collider>(c => { c.TriggerType = Collider.TriggerTypes.NonSolid; c.ColliderType = Collider.ColliderTypes.Box; });
                });
            }
            else if (entityInfo.Name == "NPC")
            {
                ZInterpreter data = new ZInterpreter(entityInfo.Data);

                new Entity(entity => {
                    entity.Name         = "NPC";
                    entity.LayerName    = "Main";
                    entity.SortingLayer = GameGlobal.Player.SortingLayer;
                    entity.Position     = (entityInfo.Position * 16) + (entityInfo.Size.ToVector2() / 2) * 16;
                    entity.Origin       = Vector2.Zero;
                    entity.AddComponent(new Drawable()).Run <Drawable>(d => {
                        d.BuildRectangle(new Point(entityInfo.Size.X * 16, entityInfo.Size.Y * 16), Color.CornflowerBlue);
                    });

                    entity.AddComponent(new Collider()).Run <Collider>(c => { c.TriggerType = Collider.TriggerTypes.NonSolid; c.ColliderType = Collider.ColliderTypes.Box; });
                });
            }
            else if (entityInfo.Name == "Water")
            {
                ZInterpreter data = new ZInterpreter(entityInfo.Data);

                new Entity(entity => {
                    entity.Name         = "Water";
                    entity.LayerName    = "Main";
                    entity.SortingLayer = 4;
                    entity.Opacity      = 1f;
                    entity.Position     = (entityInfo.Position * 16) + (entityInfo.Size.ToVector2() / 2) * 16;
                    entity.AddComponent(new Drawable()).Run <Drawable>(d => {
                        Color waterColor = (data.HasKey("color")) ? GameMethods.GetProperty <Color>(data.GetString("color")) * 0.2f : new Color(0, 0, 0, 0.2f);
                        d.BuildRectangle(new Point(entityInfo.Size.X * 16, entityInfo.Size.Y * 16), waterColor);

                        /* Animate water */
                        int offset     = 0;
                        Color[] colors = new Color[6];
                        for (int i = 0; i < colors.Length; i++)
                        {
                            colors[i] = (i % 6 >= 3) ? Color.AliceBlue * 0.3f : Color.DarkSlateBlue * 0.1f;
                        }
                        colors[3] = Color.AliceBlue * 0.5f;

                        int offsetPrev = 0;
                        CoroutineHelper.Always(() => {
                            offset = 6 + (int)(Math.Sin(Global.GameTime.TotalGameTime.TotalMilliseconds / 500) * 3);

                            if (offset != offsetPrev)
                            {
                                offsetPrev = offset;
                                d.Texture2D.ManipulateColorsRect1D(new Rectangle(0, 0, d.Texture2D.Width, 2), colors1D => {
                                    int width = d.Texture2D.Width;
                                    for (int x = 0; x < width; x++)
                                    {
                                        colors1D[0 * width + x] = colors[(x + offset).Wrap(0, colors.Length - 1)];
                                        colors1D[1 * width + x] = colors[(2 - x + offset / 2).Wrap(0, colors.Length - 1)] * 0.5f;
                                    }

                                    return(colors1D);
                                });
                            }
                        });
                        /* /Animate water */
                    });

                    entity.AddComponent(new Collider()).Run <Collider>(c => { c.TriggerType = Collider.TriggerTypes.NonSolid; c.ColliderType = Collider.ColliderTypes.Box; });
                });
            }
            else if (entityInfo.Name == "Background")
            {
                ZInterpreter data = new ZInterpreter(entityInfo.Data);

                new Entity(entity => {
                    entity.LayerName = "Background";
                    entity.Position  = new Vector2(0, 0);

                    if (data.HasKey("sortinglayer"))
                    {
                        entity.SortingLayer = data.GetInt("sortinglayer");
                    }

                    if (data.HasKey("opacity"))
                    {
                        entity.Opacity = data.GetFloat("opacity");
                    }

                    float[] coefficient = data.GetFloatArr("coefficient");
                    float[] offset      = data.GetFloatArr("offset");

                    entity.AddComponent(new CameraOffsetTexture()
                    {
                        Texture2D = Global.Content.Load <Texture2D>("Backgrounds/" + data.GetString("image")), Coefficient = new Vector2(coefficient[0], coefficient[1]), Offset = new Vector2(offset[0], offset[1])
                    });

                    if (data.HasKey("animate"))
                    {
                        entity.GetComponent <CameraOffsetTexture>().Animate      = true;
                        entity.GetComponent <CameraOffsetTexture>().AnimStepTime = data.GetFloat("animate");
                        entity.GetComponent <CameraOffsetTexture>().Size         = data.GetPointArr("crop")[0];
                    }
                });
            }
            else if (entityInfo.Name == "Foreground")
            {
                ZInterpreter data = new ZInterpreter(entityInfo.Data);

                new Entity(entity => {
                    entity.LayerName    = "Foreground";
                    entity.Position     = Vector2.Zero;
                    entity.SortingLayer = 8;
                    float[] coefficient = data.GetFloatArr("coefficient");
                    float[] offset      = data.GetFloatArr("offset");

                    entity.AddComponent(new CameraOffsetTexture()
                    {
                        Texture2D = Global.Content.Load <Texture2D>("Foregrounds/" + data.GetString("image")), Coefficient = new Vector2(coefficient[0], coefficient[1]), Offset = new Vector2(offset[0], offset[1])
                    });

                    if (data.HasKey("animate"))
                    {
                        entity.GetComponent <CameraOffsetTexture>().Animate      = true;
                        entity.GetComponent <CameraOffsetTexture>().AnimStepTime = data.GetFloat("animate");
                        entity.GetComponent <CameraOffsetTexture>().Size         = data.GetPointArr("crop")[0];
                    }
                });
            }
            else if (entityInfo.Name == "AutoDoor")
            {
                new Entity(entity => {
                    entity.Name      = "AutoDoor";
                    entity.LayerName = "Main";

                    ZInterpreter data = new ZInterpreter(entityInfo.Data);
                    entity.Data.Add("Level", data.GetString("level"));
                    entity.Data.Add("Position", data.GetString("position"));

                    entity.SortingLayer = GameGlobal.Player.SortingLayer + 5;
                    entity.Position     = (entityInfo.Position * 16) + (entityInfo.Size.ToVector2() / 2) * 16;
                    entity.AddComponent(new Drawable()).Run <Drawable>(d => {
                        d.BuildRectangle(new Point(entityInfo.Size.X * 16, entityInfo.Size.Y * 16), Color.Blue);
                        d.Visible = false;
                    });

                    entity.AddComponent(new Collider()).Run <Collider>(c => { c.TriggerType = Collider.TriggerTypes.NonSolid; c.ColliderType = Collider.ColliderTypes.Box; });
                });
            }
            else if (entityInfo.Name == "Door")
            {
                new Entity(entity => {
                    entity.Name      = "Door";
                    entity.LayerName = "Main";

                    ZInterpreter data = new ZInterpreter(entityInfo.Data);
                    entity.Data.Add("Level", data.GetString("level"));
                    entity.Data.Add("Position", data.GetString("position"));

                    entity.SortingLayer = GameGlobal.Player.SortingLayer;
                    entity.Position     = (entityInfo.Position * 16) + (entityInfo.Size.ToVector2() / 2) * 16;
                    entity.AddComponent(new Drawable()).Run <Drawable>(d => {
                        d.BuildRectangle(new Point(entityInfo.Size.X * 16, entityInfo.Size.Y * 16), Color.Blue);
                        d.Visible = false;
                    });

                    entity.AddComponent(new Collider()).Run <Collider>(c => { c.TriggerType = Collider.TriggerTypes.NonSolid; c.ColliderType = Collider.ColliderTypes.Box; });
                });
            }
            else if (entityInfo.Name == "Graphic")
            {
                ZInterpreter data = new ZInterpreter(entityInfo.Data);

                new Entity(entity => {
                    entity.Name         = "Graphic";
                    entity.LayerName    = "Main";
                    entity.SortingLayer = (data.HasKey("sortinglayer")) ? data.GetInt("sortinglayer") : 2;

                    entity.Position = (entityInfo.Position * 16);

                    if (data.HasKey("origin"))
                    {
                        var origin    = data.GetFloatArr("origin");
                        entity.Origin = new Vector2(origin[0], origin[1]);
                        if (entity.Origin.X == 1)
                        {
                            entity.Position.X += (16 * entity.Origin.X);
                        }
                        if (entity.Origin.Y == 1)
                        {
                            entity.Position.Y += (16 * entity.Origin.Y);
                        }
                    }

                    if (data.HasKey("collider"))
                    {
                        entity.AddComponent(new Collider());

                        if (data.GetString("collider").ToLower() == "pixel")
                        {
                            entity.GetComponent <Collider>().ColliderType = Collider.ColliderTypes.Pixel;
                        }
                        else if (data.GetString("collider").ToLower() == "box")
                        {
                            if (data.HasKey("boxrect"))
                            {
                                int[] boxrect = data.GetIntArr("boxrect");
                                entity.GetComponent <Collider>().ColliderType = Collider.ColliderTypes.Box;
                                entity.GetComponent <Collider>().Offset       = new Offset(boxrect[0], boxrect[1], boxrect[2], boxrect[3]);
                            }
                        }
                    }

                    if (data.HasKey("offset"))
                    {
                        var offset       = data.GetFloatArr("offset");
                        entity.Position += new Vector2(offset[0], offset[1]);
                    }

                    if (data.HasKey("id"))
                    {
                        entity.ID = data.GetString("id");
                    }

                    entity.AddComponent(new Sprite()).Run <Sprite>(d => {
                        d.LoadTexture("Graphics/" + data.GetString("image"));
                    });
                });
            }
            else if (entityInfo.Name == "Animation")
            {
                new Entity(entity => {
                    entity.Name         = "Animation";
                    entity.LayerName    = "Main";
                    entity.Origin       = Vector2.Zero;
                    entity.SortingLayer = 2;

                    ZInterpreter data = new ZInterpreter(entityInfo.Data);
                    entity.Opacity    = (data.HasKey("opacity")) ? data.GetFloat("opacity") : 1;

                    entity.AddComponent(new Sprite()).Run <Sprite>(d => {
                        d.LoadTexture("Graphics/" + data.GetString("image"));
                        d.AddAnimation(new Animation("Default", data.GetFloat("delay"), data.GetPointArr("size")[0], data.GetPointArr("frames")));
                        d.RunAnimation("Default");
                        entity.Position = (entityInfo.Position * 16) + (entityInfo.Size.ToVector2() / 2) * 16;
                    });
                });
            }
            else if (entityInfo.Name == "CameraLock")
            {
                new Entity(entity => {
                    entity.Name      = "CameraLock";
                    entity.LayerName = "Main";
                    entity.Data.Add("Type", entityInfo.Data);
                    entity.SortingLayer = GameGlobal.Player.SortingLayer;
                    entity.Opacity      = 0;
                    entity.AddComponent(new Drawable()).Run <Drawable>(d => {
                        d.BuildRectangle(new Point(entityInfo.Size.X * 16, entityInfo.Size.Y * 16), Color.Red);
                        entity.Position = (entityInfo.Position * 16) + (entityInfo.Size.ToVector2() / 2) * 16;
                    });

                    entity.AddComponent(new Collider()).Run <Collider>(c => { c.TriggerType = Collider.TriggerTypes.NonSolid; c.ColliderType = Collider.ColliderTypes.Box; });
                });
            }
        }
Пример #4
0
        public void TheTree()
        {
            SoundEffectInstance forestAmbienceSFX = Global.AudioController.Play("SFX/Forest_Ambience");

            forestAmbienceSFX.Volume = 0.3f;
            CoroutineHelper.Always(() => {
                if (forestAmbienceSFX.State == SoundState.Stopped)
                {
                    forestAmbienceSFX        = Global.AudioController.Play("SFX/Forest_Ambience");
                    forestAmbienceSFX.Volume = 0.3f;
                }
            });

            Global.SceneManager.CurrentScene.OnExit += () => {
                Global.AudioController.Stop("SFX/Forest_Ambience");
            };


            if (GameData.Get("Levels/TheTree/Intro") == "True")
            {
                // Music
                Global.AudioController.PlayMusic("Overworld Happy");
                return;
            }

            MyGame.Scenes.Level.CameraController.Offset = new Vector2(16, 0);

            // Initial fade
            GameGlobal.Fader.RunFunction("BlackOut");
            GameGlobal.Fader.RunFunction("Cancel");
            GameGlobal.Fader.Data["Time"] = "5";
            CoroutineHelper.WaitRun(2, () => {
                GameGlobal.Fader.RunFunction("Resume");
                GameGlobal.Fader.RunFunction("FadeIn");
            });

            GameGlobal.PlayerController.MovementEnabled = false;
            GameGlobal.PlayerController.Kinetic         = true;
            GameGlobal.PlayerGraphic.Visible            = false;
            GameGlobal.PlayerController.MovementMode    = PlayerController.MovementModes.None;

            // Camera pan down
            Entity camePos = new Entity(e => { e.Position = GameGlobal.Player.Position + new Vector2(0, -800); });

            CameraController.Instance.Easing  = 0.03f;
            CameraController.Instance.MaxStep = 1f;
            CameraController.Instance.Target  = camePos;
            CameraController.Instance.SnapOnce();

            // Seagulls
            CoroutineHelper.WaitRun(11, () => {
                CoroutineHelper.WaitRun(1, () => {
                    Global.AudioController.Play("SFX/BushRustle");
                });
                new Entity(entity => {
                    entity.LayerName    = "Main";
                    entity.SortingLayer = 8;
                    entity.AddComponent(new Sprite()
                    {
                        Texture2D = Global.Content.Load <Texture2D>("Entities/Seagull")
                    }).Run <Sprite>(s => {
                        s.AddAnimation(new Animation("Default", 0.1f, new Point(16, 16), new Point(0, 0), new Point(1, 0), new Point(2, 0), new Point(3, 0), new Point(3, 0), new Point(3, 0)));
                        s.RunAnimation("Default");
                    });

                    entity.Position = GameGlobal.Player.Position + new Vector2(0, -100);
                    entity.Scale    = new Vector2(2.1f, 2.1f);

                    CoroutineHelper.RunUntil(() => { return(entity.Position.Y < -500); }, () => {
                        entity.Position += new Vector2(0.3f, -0.2f * entity.Scale.Y);
                        entity.Scale    += new Vector2(-0.003f, -0.003f);
                        if (entity.Scale.X < 0.5f)
                        {
                            entity.Scale = new Vector2(0.5f, 0.5f);
                        }
                    });
                });
                new Entity(entity => {
                    entity.LayerName    = "Main";
                    entity.SortingLayer = 8;
                    entity.AddComponent(new Sprite()
                    {
                        Texture2D = Global.Content.Load <Texture2D>("Entities/Seagull")
                    }).Run <Sprite>(s => {
                        s.AddAnimation(new Animation("Default", 0.1f, new Point(16, 16), new Point(0, 0), new Point(1, 0), new Point(2, 0), new Point(3, 0), new Point(3, 0), new Point(3, 0)));
                        s.RunAnimation("Default");
                    });

                    entity.Position = GameGlobal.Player.Position + new Vector2(-40, -100);
                    entity.Scale    = new Vector2(2, 2);

                    CoroutineHelper.RunUntil(() => { return(entity.Position.Y < -500); }, () => {
                        entity.Position += new Vector2(0.3f, -0.2f * entity.Scale.Y);
                        entity.Scale    += new Vector2(-0.003f, -0.003f);
                        if (entity.Scale.X < 0.5f)
                        {
                            entity.Scale = new Vector2(0.5f, 0.5f);
                        }
                    });
                });
            });

            CoroutineHelper.WaitRun(7, () => {
                camePos.Position = GameGlobal.Player.Position + new Vector2(0, -40);

                CoroutineHelper.WaitRun(12, () => {
                    GameGlobal.Player.Position = camePos.Position + new Vector2(0, -20);
                    GameGlobal.PlayerGraphic.RunAnimation("Jump");

                    GameMethods.ShowMessages(new List <MessageBox>()
                    {
                        new MessageBox("Pause", ".|.|.|| !", camePos.Position),
                        new MessageBox("Pause", "Huh|.|.|.||| I don't remember\nsleeping there!?", camePos.Position)
                    }, null, () => {
                        CoroutineHelper.WaitRun(3, () => {
                            GameGlobal.PlayerGraphic.Visible = true;

                            MessageBox WoahMSG = new MessageBox("Pause", "Woah!", camePos.Position, MessageBox.Type.ManualDestroy);
                            WoahMSG.Build();

                            float temp = GameGlobal.Player.Position.Y;
                            CoroutineHelper.RunFor(0.7f, p => {
                                if (GameGlobal.Player.Position.Y < camePos.Position.Y + 45)
                                {
                                    GameGlobal.Player.Position.Y += 1f * (p * 4.15f);
                                }

                                if (GameGlobal.Player.Position.Y > WoahMSG.Position.Y + 32)
                                {
                                    WoahMSG.Position = GameGlobal.Player.Position + new Vector2(-WoahMSG.Container.Size.X / 2, -32);
                                }
                            }, () => {
                                GameGlobal.Player.Position.Y = camePos.Position.Y + 45;
                                GameGlobal.PlayerGraphic.RunAnimation("Lay");
                                Global.AudioController.Play("SFX/Thump");
                                WoahMSG.Destroy();

                                GameMethods.SmokePuffs(8, GameGlobal.PlayerGraphicEntity.Position, new Point(8, 8));

                                CameraController.Instance.Shake(0.7f);

                                CoroutineHelper.WaitRun(2, () => {
                                    // Music
                                    Global.AudioController.PlayMusic("Overworld Happy", 1);

                                    GameMethods.ShowMessages(new List <MessageBox>()
                                    {
                                        new MessageBox("Pause", "OUCH!", GameGlobal.Player.Position + new Vector2(0, -32)),
                                    }, null, () => {
                                        GameGlobal.PlayerController.MovementEnabled = true;
                                        GameGlobal.PlayerController.MovementMode    = PlayerController.MovementModes.Normal;
                                        GameGlobal.PlayerController.Kinetic         = false;
                                        CameraController.Instance.Easing            = 0.1f;
                                        CameraController.Instance.MaxStep           = 1000;
                                        CameraController.Instance.Target            = GameGlobal.Player;
                                        camePos.Destroy();

                                        GameGlobal.PlayerGraphic.RunAnimation("Stand");
                                        GameGlobal.PlayerController.Direction  = 1;
                                        GameGlobal.PlayerController.IsGrounded = true;

                                        GameData.Set("Levels/TheTree/Intro", "True");

                                        GameGlobal.Fader.RunFunction("SetDefault");
                                    });
                                });
                            });
                        });
                    });
                });
            });
        }
Пример #5
0
        public static void InitialiseAssets()
        {
            CoroutineHelper.Always(() => {
                TimeLoop = (TimeLoop + Global.DeltaTime).Between(0, (float)Math.PI * 360);
            });

            // Fader
            Fader = new Entity(entity => {
                entity.LayerName = "Fade";

                entity.Data.Add("Time", "");
                entity.Data.Add("Cancel", "");

                entity.AddComponent(new Drawable()).Run <Drawable>(component => {
                    component.BuildRectangle(new Point(Global.ScreenBounds.Width, Global.ScreenBounds.Height), Color.Black);
                });

                entity.AddFunction("SetDefault", (e) => {
                    entity.Data["Time"]   = "0.5";
                    entity.Data["Cancel"] = "false";
                });

                entity.RunFunction("SetDefault");

                entity.AddFunction("FadeIn", (e, c) => {
                    entity.RunFunction("BlackOut");
                    CoroutineHelper.RunFor((float)Convert.ToDecimal(entity.Data["Time"]), pcnt => { if (entity.Data["Cancel"] != "true")
                                                                                                    {
                                                                                                        e.Opacity = 1 - pcnt;
                                                                                                    }
                                           }, () => {
                        c?.Invoke(e);
                    });
                });

                entity.AddFunction("FadeOut", (e, c) => {
                    entity.RunFunction("InstantIn");
                    CoroutineHelper.RunFor((float)Convert.ToDecimal(entity.Data["Time"]), pcnt => { if (entity.Data["Cancel"] != "true")
                                                                                                    {
                                                                                                        e.Opacity = pcnt;
                                                                                                    }
                                           }, () => {
                        c?.Invoke(e);
                    });
                });

                entity.AddFunction("InstantIn", (e) => {
                    e.Opacity = 0;
                });

                entity.AddFunction("BlackOut", (e) => {
                    e.Opacity = 1;
                });

                entity.AddFunction("Cancel", (e) => {
                    entity.Data["Cancel"] = "true";
                });

                entity.AddFunction("Resume", (e) => {
                    entity.Data["Cancel"] = "false";
                });
            });
        }