/// <summary> /// Display all Entities that are Visible using thier RenderLayer order /// </summary> public virtual void Render() { //------------------------------------------------------------------------------- // Get all RenderComponent, sort them, low -> high //------------------------------------------------------------------------------- List <RenderComponent> ComponentsToRender = new List <RenderComponent>(); // // Find all Entities (in case some were removed/added) // GameEntities = EntityContext.GetEntities().Where(e => e.EntityType == 0).ToList(); SceneEntities = EntityContext.GetEntities().Where(e => e.EntityType == 1).ToList(); foreach (Entity ent in GameEntities) { if (!(ent.Get <Transform>().Enabled&& ent.Get <Transform>().Visiable)) { continue; } // // Ask Entitas for all components attached to "ent" entity // Entitas.IComponent[] allComp = ent.GetComponents(); //this entity's component foreach (Entitas.IComponent comp in allComp) //get the renderable ones { if (comp is IRenderable) { RenderComponent myComp = (RenderComponent)comp; ComponentsToRender.Add(myComp); } } } //------------------------------------------------------------------------------- // CAMERA DISPLAY BeginMode2D //------------------------------------------------------------------------------- if (CameraEnabled && CameraEntityToFollow != null) { // need to get the entity that is target of camera -> target Get<Transform>().Position // Any component that is in view of the camera, is drawn first switch (CameraType2D) { case Camera2DType.FollowPlayer: UpdateCameraCenter(); break; case Camera2DType.FollowInsideMap: UpdateCameraInsideMap(); break; case Camera2DType.FollowCenterSmooth: UpdateCameraCenterSmoothFollow(); break; } Raylib.BeginMode2D(Camera); } //------------------------------------------------------------------------------- // RENDER ORDER sorting (low to high) then render //------------------------------------------------------------------------------- foreach (RenderComponent myComp in ComponentsToRender.OrderBy(e => e.RenderLayer)) { myComp.Render(); //call draw method } //------------------------------------------------------------------------------- // CAMERA DISPLAY EndMode2D //------------------------------------------------------------------------------- if (CameraEnabled && CameraEntityToFollow != null) { if (Global.DebugRenderEnabled) { // // display camera position // int screenHeight = Global.SceneHeight; int screenWidth = Global.SceneWidth; int tx = (int)CameraEntityToFollow.Get <Transform>().Position.X; int ty = (int)CameraEntityToFollow.Get <Transform>().Position.Y; Raylib.DrawLine(tx, -screenHeight * 10, tx, screenHeight * 10, Color.GREEN); //Verticval Raylib.DrawLine(-screenWidth * 10, ty, screenWidth * 10, ty, Color.GREEN); //Horizontal //tx = (int)Global.WindowCenter.X; //ty = (int)Global.WindowCenter.Y; //Raylib.DrawLine(tx, -screenHeight * 10, tx, screenHeight * 10, Color.RED); //Verticval //Raylib.DrawLine(-screenWidth * 10, ty, screenWidth * 10, ty, Color.RED); //Horizontal } Raylib.EndMode2D(); } //------------------------------------------------------------------------------- // U I ENTITIES , they are drawn on top of all other game entities //------------------------------------------------------------------------------- foreach (Entity ent in SceneEntities) { if (!ent.Get <Transform>().Enabled) { continue; } if (!ent.Get <Transform>().Visiable) { continue; } Entitas.IComponent[] allComp = ent.GetComponents(); //this entity's component foreach (Entitas.IComponent comp in allComp) { if (comp is IRenderable) { RenderComponent myComp = (RenderComponent)comp; myComp.Render(); //call draw method } } } //----------------- // Scene debug //----------------- if (Global.DebugRenderEnabled) { Raylib.DrawText(Raylib.GetMousePosition().ToString(), 10, 10, 20, Color.WHITE); if (CameraEnabled && CameraEntityToFollow != null) { Raylib.DrawFPS(10, 30); Raylib.DrawText("Zoom" + Camera.zoom.ToString(), 10, 50, 20, Color.WHITE); } else { Raylib.DrawFPS(10, 30); } } }
public void AddComponent(RenderComponent _uiElement) { // convert relative position to absolute _uiElement.CompPosition = new Vector2(_uiElement.CompPosition.X + CompPosition.X, _uiElement.CompPosition.Y + CompPosition.Y); PanelComponents.Add(_uiElement); }
/// <summary> /// mmGame calls this Update() method every frame /// </summary> public virtual void Update() { //------------------- // Z O O M //------------------- if (Global.DebugRenderEnabled && CameraEnabled) { // Camera zoom controls Camera.zoom += ((float)Raylib.GetMouseWheelMove() * 0.05f); if (Camera.zoom > 3.0f) { Camera.zoom = 3.0f; } else if (Camera.zoom < 0.1f) { Camera.zoom = 0.1f; } if (Raylib.IsKeyPressed(KeyboardKey.KEY_R)) // Camera reset (zoom and rotation) { Camera.zoom = 1.0f; Camera.rotation = 0.0f; } } deltaTime = Raylib.GetFrameTime(); Global.DeltaTime = deltaTime; // // Find all Entities // GameEntities = EntityContext.GetEntities().Where(e => e.EntityType == 0).ToList(); SceneEntities = EntityContext.GetEntities().Where(e => e.EntityType == 1).ToList(); //----------------------------------------------------------------------- // Update game entities (Todo: Update order ? like the RenderLayer) //----------------------------------------------------------------------- foreach (Entity ent in GameEntities) { if (!ent.Get <Transform>().Enabled) { continue; } Entitas.IComponent[] allComp = ent.GetComponents(); //all entity components foreach (Entitas.IComponent comp in allComp) { if (comp is IRenderable) { // // Renderable components get the transform component of the Entity // RenderComponent myComp = (RenderComponent)comp; myComp.CompEntity = ent; myComp.Update(deltaTime); } else { Component myComp = (Component)comp; myComp.CompEntity = ent; myComp.Update(deltaTime); } } } // // Update scene UI entities // foreach (Entity ent in SceneEntities) { if (!ent.Get <Transform>().Enabled) { continue; } Entitas.IComponent[] allComp = ent.GetComponents(); //this entity's component foreach (Entitas.IComponent comp in allComp) { if (comp is IRenderable) { // // Renderable components need to know what happened to Entity Transform // RenderComponent myComp = (RenderComponent)comp; myComp.CompEntity = ent; myComp.Update(deltaTime); } else { Component myComp = (Component)comp; myComp.CompEntity = ent; myComp.Update(deltaTime); //force it to update() } } } //SceneSystems.Execute(); //SceneSystems.Cleanup(); EntitySystems.Execute(); //run IExecuteSystems // // If user wants out of this scene // if (ForceEndScene) { Global.StateOfGame = GameState.ForcedExit; } }