Hello Guest

Author Topic: Surface Example  (Read 10944 times)

0 Members and 1 Guest are viewing this topic.

Kyle

  • Administrator
  • Member
  • Posts: 258
    • View Profile
Surface Example
« on: April 29, 2015, 04:40:34 PM »
http://pastie.org/10121082

Code: [Select]
using Otter;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SurfaceExample {
    class Program {
        static void Main(string[] args) {
            // Create a new game to play.
            var game = new Game();

            // Set the scroll of the surfaces to 0 so they never scroll on the camera.
            Global.SurfaceGameplay.Scroll = 0;
            Global.SurfaceHud.Scroll = 0;

            // Start the game with a new GameScene.
            game.Start(new GameScene(1000, 1000));

        }
    }

    class GridBackground : Entity {
        public GridBackground() : base() {
            Layer = 10;
            Surface = Global.SurfaceGameplay;
        }

        public override void Added() {
            base.Added();

            // Just render a simple grid background so when the camera scrolls we can see it.
            AddGraphic(new Grid(Scene.Width, Scene.Height, 40, 40, Color.Cyan));
            Graphic.Alpha = 0.4f;
        }
    }

    class Player : Entity {
        public Player(float x, float y) : base(x, y) {
            // Add a basic red box for the graphic.
            AddGraphic(Image.CreateRectangle(20, 20, Color.Red));
            Graphic.CenterOrigin();

            // Assign this entity to render to Global.SurfaceGameplay instead of the default game surface.
            Surface = Global.SurfaceGameplay;

            // Set up a collider for movement.
            var collider = AddCollider(new BoxCollider(20, 20));

            // Set up the rest of the movement stuff.
            var axis = AddComponent(Axis.CreateWASD());
            var movement = AddComponent(new BasicMovement(400, 400, 30));
            movement.Axis = axis;
            movement.Collider = collider;
        }
    }

    class PlayerHud : Entity {
        public PlayerHud() : base() {
            // Basic hud test with just some text.
            AddGraphic(new Text("This is the HUD!", 20));

            Surface = Global.SurfaceHud;
        }
    }

    class GameScene : Scene {
        public GameScene(int width, int height) : base(width, height) {
            // Add the entities to the scene when the scene is created.
            Add(new Player(HalfWidth, HalfHeight));
            Add(new GridBackground());
            Add(new PlayerHud());
        }

        public override void UpdateLast() {
            base.UpdateLast();

            // Set the scene's camera to follow the player.
            var player = GetEntity<Player>();
            CenterCamera(player.X, player.Y);

            // Set the surface's camera to follow the scene's camera.
            Global.SurfaceGameplay.CameraX = CameraX;
            Global.SurfaceGameplay.CameraY = CameraY;

            // Zoom the surface up and down with the arrow keys.
            if (Input.KeyDown(Key.Down)) {
                Global.SurfaceGameplay.CameraZoom -= 0.005f;
            }
            if (Input.KeyDown(Key.Up)) {
                Global.SurfaceGameplay.CameraZoom += 0.005f;
            }
        }

        public override void Render() {
            base.Render();

            // Render the surfaces.
            Draw.Graphic(Global.SurfaceGameplay);
            Draw.Graphic(Global.SurfaceHud);
        }
    }

    class Global {
        // Keep global references to the surfaces used for rendering the game.
        public static Surface SurfaceHud = new Surface(640, 480);
        public static Surface SurfaceGameplay = new Surface(640, 480, Color.Gray);
    }
}

dzScritches

  • Member
  • Posts: 1
    • View Profile
Re: Surface Example
« Reply #1 on: January 09, 2016, 01:46:02 PM »
Sorry for replying to an old thread but - is the Surface component a good choice to use for rendering things like alternative camera views? The only game example that comes to mind is Transport Tycoon, where you can view an image of a train that's somewhere off in the distance, far away from where the main view is, in a little window. Is that kind of what Surface is for?

Kyle

  • Administrator
  • Member
  • Posts: 258
    • View Profile
Re: Surface Example
« Reply #2 on: January 09, 2016, 10:23:56 PM »
Yeah if you want to render a completely different view of your scene then having all of those things render to a surface is most likely what you want to do.