Ejemplo n.º 1
0
        public static DebugGUIHandler Create(string name)
        {
            IGameObject     gO      = Scene.CreateGameObject(name);
            DebugGUIHandler handler = gO.AddComponent <DebugGUIHandler>(22);

            IGameObject fpsGO    = Scene.CreateGameObject("fps", gO, new Vector2(Scene.MainViewport.Right, Scene.MainViewport.Top));
            GUILabel    fpsLabel = fpsGO.AddComponent <GUILabel>("fps", new FontFamily("Consolas"), Color.LIME);

            fpsLabel.Dock    = GUIDock.TopRight;
            fpsLabel.Width   = 0.1f;
            fpsLabel.Height  = 0.06f;
            handler.FpsLabel = fpsLabel;

            IGameObject upsGO    = Scene.CreateGameObject("ups", gO, new Vector2(Scene.MainViewport.Right - fpsLabel.WorldWidth * 1.1f, Scene.MainViewport.Top));
            GUILabel    upsLabel = upsGO.AddComponent <GUILabel>("ups", new FontFamily("Consolas"), Color.ORANGE);

            upsLabel.Dock    = GUIDock.TopRight;
            upsLabel.Width   = 0.1f;
            upsLabel.Height  = 0.06f;
            handler.UpsLabel = upsLabel;

            Timer fpsUpsTimer = gO.AddComponent <Timer>();

            fpsUpsTimer.Time             = 0.1f;
            fpsUpsTimer.OnTimerComplete += timer => {
                fpsLabel.Text = Window.Window.FramesPerSecond + "fps";
                upsLabel.Text = Time.UpdatesPerSecond + "ups";
                timer.Start();
            };
            fpsUpsTimer.Start();
            handler.Timer = fpsUpsTimer;

            IGameObject[] logGOs    = new IGameObject[handler.LineCount];
            GUILabel[]    logLabels = new GUILabel[handler.LineCount];
            for (int i = 0; i < handler.LineCount; i++)
            {
                logGOs[i]           = Scene.CreateGameObject($"log_line_{i}", gO, new Vector2(Scene.MainViewport.Left, Scene.MainViewport.Top - i * Scene.MainViewport.Height / (handler.LineCount + 1)));
                logLabels[i]        = logGOs[i].AddComponent <GUILabel>($"", new FontFamily("Consolas"), Color.WHITE);
                logLabels[i].Width  = 2 / 3f;
                logLabels[i].Height = 2 / (float)(handler.LineCount + 1);
            }

            for (int i = 0; i < handler.LineCount; i++)
            {
                logLabels[i].Dock     = GUIDock.TopLeft;
                logLabels[i].TextDock = GUIDock.LeftCenter;
            }
            handler.Logs = logLabels;

            IGameObject consoleGO = Scene.CreateGameObject("console", gO, new Vector2(Scene.MainViewport.Left, Scene.MainViewport.Bottom));
            GUITextbox  consoleTb = consoleGO.AddComponent <GUITextbox>("", new FontFamily("Consolas"), Color.WHITE, GraphicsHandler.CreateDefaultTexture(1, 1, new Color(31, 31, 31, 127)));

            consoleTb.Width         = 2f / 3f;
            consoleTb.Height        = 2f / handler.LineCount;
            consoleTb.Dock          = GUIDock.BottomLeft;
            consoleTb.MaxTextLength = 40;
            handler.Console         = consoleTb;

            return(handler);
        }
Ejemplo n.º 2
0
        private void OnSceneLoad_Test()
        {
            //ResourceManager.LoadResource<string, TextLoadingParameters>("res_str_test_0", new TextLoadingParameters(new[] { "res_str_test_0.txt" }, Encoding.UTF8), 1, false);
            //if (!ResourceManager.TryGetResource("res_str_test_0", out string str, true))
            //    Log.WriteLine("Could not get resource 'res_str_test_0'", LogType.Message);

            ResourceManager.LoadResource <ITexture, TextureLoadingParameters>("res_tex_test_0", new TextureLoadingParameters(new[] { "res_tex_test_0.png" }, TextureWrapMode.ClampToEdge, TextureFilterMode.Nearest), 2, false);
            ResourceManager.LoadResource <ITexture, TextureLoadingParameters>("res_tex_test_1", new TextureLoadingParameters(new[] { "res_tex_test_1.png" }, TextureWrapMode.ClampToEdge, TextureFilterMode.Nearest), 2, false);
            ResourceManager.LoadResource <ITexture, TextureLoadingParameters>("res_tex_test_2", new TextureLoadingParameters(new[] { "res_tex_micha.png" }, TextureWrapMode.ClampToEdge, TextureFilterMode.Nearest), 2, false);
            ResourceManager.LoadResource <ITextureAtlas, FontLoadingParameters>($"font_Consolas_16", new FontLoadingParameters(new FontFamily("Consolas"), 16), 0, true);

            if (!ResourceManager.TryGetResource("res_tex_test_0", out ITexture tex0, true))
            {
                Log.WriteLine("Could not get resource 'res_tex_test_0'", LogType.Message);
            }

            if (!ResourceManager.TryGetResource("res_tex_test_1", out ITexture tex1, true))
            {
                Log.WriteLine("Could not get resource 'res_tex_test_1'", LogType.Message);
            }

            if (!ResourceManager.TryGetResource("res_tex_test_2", out ITexture tex2, true))
            {
                Log.WriteLine("Could not get resource 'res_tex_test_2'", LogType.Message);
            }

            IGameObject go_viewport = Scene.CreateGameObject("go_viewport");
            Viewport    viewport    = go_viewport.AddComponent <Viewport>();

            //viewport.Transform.Position = new Vector2(1, 0);
            Scene.MainViewport = viewport;
            viewport.Zoom      = 1;
            //viewport.Width *= 10f;
            //viewport.Height *= 10f;
            //viewport.Transform.Position.x += 1;
            GUIHandler guiHandler = go_viewport.AddComponent <GUIHandler>();

            ResourceManager.TryGetResource("internal_circleShader", out IShader shader, true);

            //IGameObject go_sprite = Scene.CreateGameObject("go_sprite");
            //go_sprite.Transform.Z = 0.65f;
            //Sprite sprite = go_sprite.AddComponent<Sprite>();
            //sprite.Transform.Scale = new Vector2(0.95f, 0.95f);
            //sprite.Color = new Color(255, 0, 255, 63);

            IGameObject go_cSprite = Scene.CreateGameObject("go_cSprite");

            go_cSprite.Transform.Z = 0.65f;

            // uniforms
            Color cSpriteColor = Color.WHITE;
            float innerRadius  = 0.1f;
            float startAngle   = 0.0f;
            float sweepAngle   = 1f;

            // vertex attributes
            (VertexAttribute va, float[][] data)[] vertexAttributes =
        public static TextureComponent AddTextureComponent(this IGameObject gameObject, string textureName = "", int width = -1, int height = -1)
        {
            var component = new TextureComponent(textureName, width, height);

            gameObject.AddComponent(component);
            return(component);
        }
        public static ScriptComponent AddScriptComponent(this IGameObject gameObject, params Type[] scriptActionTypes)
        {
            var component = new ScriptComponent(scriptActionTypes.Select(type => type?.FullName).ToArray());

            gameObject.AddComponent(component);
            return(component);
        }
Ejemplo n.º 5
0
        private void startAnimation()
        {
            IGameObject             block     = HasFullBlock ? fullBlock : halfBlock;
            BlockAnimationBehaviour animation = block.AddComponent <BlockAnimationBehaviour>();

            animation.SetUpAnimation(new Vector3(0, 0.5f, 2), gameObject.Find("BlockPosition").transform.localPosition);
        }
Ejemplo n.º 6
0
        private static void AddSprites(IGameObject obj)
        {
            var objectType = obj.GetType();

            if (objectType == typeof(Spaceship))
            {
                // Fire
                var movementComponent = obj.GetComponent <KeyboardControlledMovementComponent>();
                obj.AddComponent(new AnimatedSpriteComponent("F", 9, 50, 10)
                {
                    OffsetX        = 35,
                    OffsetY        = 60,
                    VisibilityFunc = () => movementComponent != null && movementComponent.IsAccelerating,
                    BlendMode      = BlendMode.Additive
                });

                // Ship
                obj.SetZIndex(100).AddComponent(new AnimatedSpriteComponent("S", 49, 50, 2, 2));
            }

            if (objectType == typeof(Asteroid))
            {
                var asteroidPrefix = new[] { "Z1", "Z2", "Z3", "Z4", "Z5", "Z6" }.GetRandomItem();
                var size = obj.GetComponent <SizeComponent>();
                obj.AddComponent(new AnimatedSpriteComponent(asteroidPrefix, 20, RandomHelper.Instance.Next(15, 40), 5,
                                                             decodePixelWidth: (int)size.Width,
                                                             decodePixelHeight: (int)size.Height));
            }

            if (objectType == typeof(Crate))
            {
                obj.SetZIndex(1)
                .AddComponent(new AnimatedSpriteComponent("C", 17, RandomHelper.Instance.Next(15, 40), 6,
                                                          decodePixelWidth: 50, decodePixelHeight: 50));
            }

            if (objectType == typeof(Rocket))
            {
                obj.SetZIndex(2).AddComponent(new AnimatedSpriteComponent("R", 19, 20, 5));
            }

            if (objectType == typeof(Bullet))
            {
                obj.SetZIndex(3).AddComponent(new AnimatedSpriteComponent("R", 1,
                                                                          decodePixelWidth: 10, decodePixelHeight: 15));
            }
        }
Ejemplo n.º 7
0
        public T CreateGameObject <T>(string name, Vector2 position) where T : IComponent, new()
        {
            IGameObject go = CreateGameObject(name);

            go.Transform.Position = position;

            return(go.AddComponent <T>());
        }
        public static ColliderComponent AddColliderComponent(this IGameObject gameObject, int x = default,
                                                             int y = default, int width = default, int height = default)
        {
            var component = new ColliderComponent(x, y, width, height);

            gameObject.AddComponent(component);
            return(component);
        }
Ejemplo n.º 9
0
        public override Component CloneTo(IGameObject gameObject)
        {
            DestroyAfterTime clone = new DestroyAfterTime(Time);

            gameObject.AddComponent(clone);

            return(clone);
        }
Ejemplo n.º 10
0
        public virtual Component CloneTo(IGameObject gameObject)
        {
            Component clone = (Component)this.MemberwiseClone();

            gameObject.AddComponent(clone);

            return(clone);
        }
Ejemplo n.º 11
0
        public override Component CloneTo(IGameObject gameObject)
        {
            ProjectileComponent clone = new ProjectileComponent(XRate, YRate);

            gameObject.AddComponent(clone);

            return(clone);
        }
Ejemplo n.º 12
0
        public static PositionComponent AddPositionComponent(this IGameObject gameObject, double x = default,
                                                             double y = default)
        {
            var component = new PositionComponent(x, y);

            gameObject.AddComponent(component);
            return(component);
        }
Ejemplo n.º 13
0
        public static AnimationComponent AddAnimationComponent(this IGameObject gameObject,
                                                               IDictionary <int, string> animations = null, int startAnimation = -1)
        {
            var component = new AnimationComponent(animations ?? new Dictionary <int, string>(), startAnimation);

            gameObject.AddComponent(component);
            return(component);
        }
Ejemplo n.º 14
0
        private void OnSceneLoad_CellularAutomata()
        {
            IGameObject go_viewport = Scene.CreateGameObject("go_viewport");
            Viewport    viewport    = go_viewport.AddComponent <Viewport>();

            Scene.MainViewport = viewport;
            GUIHandler guiHandler = go_viewport.AddComponent <GUIHandler>();

            IGameObject gO_cellularAutomata = Scene.CreateGameObject("gO_cellularAutomata");
            ForestFire  forestFire          = gO_cellularAutomata.AddComponent <ForestFire>(new CellularAutomataInitializationData(2, 100, true, NeighbourhoodMode.Moore));

            gO_cellularAutomata.Transform.Position.x -= (viewport.Width - 1) / 2f;

            IGameObject gO_pieChart = Scene.CreateGameObject("go_pieChart");
            PieChart    pieChart    = gO_pieChart.AddComponent <PieChart>(forestFire);

            gO_pieChart.Transform.Scale       = new Vector2(0.25f, 0.25f);
            gO_pieChart.Transform.Position.x += 0.25f;
        }
Ejemplo n.º 15
0
        public void Setup()
        {
            Game.Messages = new SmallEngine.Messages.QueueingMessageBus(1);
            Scene.Define("test", typeof(RigidBodyComponent), typeof(ColliderComponent));
            _scene = Scene.Load <Scene>(SceneLoadModes.Additive);

            _renderGo = _scene.CreateGameObject <GameObject>();
            _renderGo.AddComponent(new RenderComponent());

            _renderGo2 = _scene.CreateGameObject <GameObject>();
            _renderGo2.AddComponent(new RenderComponent()
            {
                ZIndex = 10
            });
        }
Ejemplo n.º 16
0
        /// <summary>
        /// Adds the component when no components of this type exist.
        /// </summary>
        /// <param name="gameObject">The game object.</param>
        /// <param name="component">The component.</param>
        /// <returns>Returns existing component, if any. Otherwise, returns provided component..</returns>
        public static T AddComponentUniqueType <T>(this IGameObject gameObject, T component) where T : class
        {
            if (gameObject == null || component == null)
            {
                throw new ArgumentNullException();
            }

            var existingComponent = gameObject.GetComponent <T>();

            if (existingComponent != null)
            {
                return(existingComponent);
            }

            gameObject.AddComponent(component);
            return(component);
        }
        public void CopyInto(IGameObject newObject)
        {
            //Copy components
            foreach (var component in AllComponents())
            {
                var newComponent = componentFactory.GetComponent(component.ConcreteType);
                newObject.AddComponent(component.InterfaceType, newComponent);
                component.CopyInto(newComponent);
            }

            //Copy metadata and layer info
            foreach (var entry in metadata)
            {
                newObject.AddMetadata(entry.Key, entry.Value);
            }

            newObject.Layer = Layer;
        }
        protected void GenerateComponents(IGameObject gameObject, XElement element)
        {
            if (element.Element("components") != null && ComponentFactory != null)
            {
                List<XElement> componentElements = element.Element("components").Elements("component").ToList();

                //Run through all the components defined in the XElement and create them.
                for (int i = 0; i < componentElements.Count; i++)
                {
                    //Instantiate the component itself.
                    IBaseComponent component = ComponentFactory.LoadComponentFromXML(componentElements[i]);

                    //Finally, add the component to the provided IGameObject.
                    if (component != null && component.InterfaceType != null)
                        gameObject.AddComponent(component.InterfaceType, component);
                }
            }
        }
Ejemplo n.º 19
0
        /// <summary> Process unity specific data. </summary>
        private void BuildObject(Tile tile, IGameObject gameObjectWrapper, Rule rule,
                                 Rectangle2d rect, Vector2d point, float minHeight)
        {
            var gameObject = gameObjectWrapper.AddComponent(GameObject.CreatePrimitive(PrimitiveType.Cube));
            var transform  = gameObject.transform;
            var elevation  = ElevationProvider.GetElevation(point);

            transform.position = new Vector3((float)point.X, elevation + minHeight, (float)point.Y);
            // TODO define size
            transform.localScale = new Vector3(2, 2, 2);

            var p0 = new Vector2((float)rect.Left, (float)rect.Bottom);
            var p1 = new Vector2((float)rect.Right, (float)rect.Bottom);
            var p2 = new Vector2((float)rect.Left, (float)rect.Top);
            var p3 = new Vector2((float)rect.Right, (float)rect.Top);

            var mesh = gameObject.GetComponent <MeshFilter>().mesh;

            // Imagine looking at the front of the cube, the first 4 vertices are arranged like so
            //   2 --- 3
            //   |     |
            //   |     |
            //   0 --- 1
            // then the UV's are mapped as follows
            //    2    3    0    1   Front
            //    6    7   10   11   Back
            //   19   17   16   18   Left
            //   23   21   20   22   Right
            //    4    5    8    9   Top
            //   15   13   12   14   Bottom
            mesh.uv = new[]
            {
                p0, p1, p2, p3,
                p2, p3, p2, p3,
                p0, p1, p0, p1,
                p0, p3, p1, p2,
                p0, p3, p1, p2,
                p0, p3, p1, p2
            };

            gameObject.GetComponent <MeshRenderer>().sharedMaterial = rule.GetMaterial(CustomizationService);

            gameObjectWrapper.Parent = tile.GameObject;
        }
Ejemplo n.º 20
0
        private void AttachBehaviours(IGameObject gameObject, Rule rule, Model model)
        {
            var behaviourTypes = rule.GetModelBehaviours(_customizationService);

            if (gameObject != null && !gameObject.IsBehaviourAttached && behaviourTypes.Any())
            {
                Observable.Start(() =>
                {
                    foreach (var behaviourType in behaviourTypes)
                    {
                        var behaviour = gameObject.AddComponent <IModelBehaviour>(behaviourType);
                        if (behaviour != null)
                        {
                            behaviour.Apply(gameObject, model);
                        }
                    }
                }, Scheduler.MainThread);
            }
        }
Ejemplo n.º 21
0
        protected void GenerateComponents(IGameObject gameObject, XElement element)
        {
            if (element.Element("components") != null && ComponentFactory != null)
            {
                List <XElement> componentElements = element.Element("components").Elements("component").ToList();

                //Run through all the components defined in the XElement and create them.
                for (int i = 0; i < componentElements.Count; i++)
                {
                    //Instantiate the component itself.
                    IBaseComponent component = ComponentFactory.LoadComponentFromXML(componentElements[i]);

                    //Finally, add the component to the provided IGameObject.
                    if (component != null && component.InterfaceType != null)
                    {
                        gameObject.AddComponent(component.InterfaceType, component);
                    }
                }
            }
        }
Ejemplo n.º 22
0
        /// <summary>
        /// Determines dependencies for the component and imports all fields marked with ImportComponentAttribute
        /// </summary>
        /// <param name="pGameObject"></param>
        public override void OnAdded(IGameObject pGameObject)
        {
            base.OnAdded(pGameObject);

            foreach (var d in _fields)
            {
                IComponent component = null;
                var        type      = d.Field.FieldType;
                if (d.Info.AllowInheritedTypes)
                {
                    //Look for any inherited types
                    foreach (var c in pGameObject.GetComponents())
                    {
                        if (type.IsInstanceOfType(c))
                        {
                            component = c;
                            break;
                        }
                    }
                }
                else
                {
                    //Get actual type
                    component = pGameObject.GetComponent(type);
                }

                if (component == null && d.Info.Required)
                {
                    //Create if required
                    component = Create(type);
                    pGameObject.AddComponent(component);
                }

                if (component == null)
                {
                    continue;
                }

                d.Field.SetValue(this, component);
            }
        }
Ejemplo n.º 23
0
 public void AddComponent(object component)
 {
     _go.AddComponent(component);
 }
Ejemplo n.º 24
0
 public void AddComponent(object component) => _gameObjectImplementation.AddComponent(component);