Class PlatformView

All Implemented Interfaces:
Animation, Editable, StyleListener, Iterable<Component>

public class PlatformView extends GameView

Plays a PlatformWorld on screen: steps it at a fixed 60 Hz from the player's input, draws it through a PlatformArt, follows the player with a GameCamera's follow mode and plays each tick's events through Feedback.

import com.codename1.gaming.platformer.*;
import com.codename1.gaming.platformer.view.*;

LevelData level = LevelData.parse("{\"tileSize\":16,\"spawn\":[1,1],\"tiles\":[\"........\",\"........\",\"########\"]}");
PlatformWorld world = level.createWorld(1);
Form form = new Form("Level", new BorderLayout());
PlatformView view = new PlatformView(world, new ProceduralArt(level.getTileSize()));
form.add(BorderLayout.CENTER, view);
form.show();
view.start();

Drawing is interpolated between the last two ticks, so motion is smooth at any display rate while the simulation stays exactly 60 Hz. Tiles are baked into chunk images once and re-baked only where the grid changes. Positions are in world pixels multiplied by a scale chosen so #setVisibleRows rows of tiles fill the height of the view.

#setDepth(boolean) turns on 2.5D: the same world, the same rules and the same replay, drawn with a perspective camera. Solid ground becomes extruded blocks and actors, entities and backdrops become billboards at depth, so backdrops get real parallax. The simulation cannot tell the difference.

Override #onEvent for game rules the view should react to (a HUD, a win screen), or set an InputSource to drive the world from a replay or a network peer instead of the local player.

  • Constructor Details

    • PlatformView

      public PlatformView(PlatformWorld world, PlatformArt art)

      Creates a view.

      Parameters
      • world: the world to play
      • art: draws it
  • Method Details

    • setWorld

      public final void setWorld(PlatformWorld world, PlatformArt art)

      Replaces the world, as when the next level starts or a level restarts.

      Parameters
      • world: the new world
      • art: draws it
    • setDepth

      public void setDepth(boolean depth)

      Draws in 2.5D: a perspective camera over extruded blocks and billboard sprites. Call before #start() or at any time after.

      Parameters
      • depth: true for 2.5D, false for flat 2D
    • isDepth

      public boolean isDepth()

      Whether the view draws in 2.5D.

      Returns

      true in 2.5D

    • setDepthLook

      public void setDepthLook(float fovDegrees, float tilt, float blockDepth)

      Tunes the 2.5D look.

      Parameters
      • fovDegrees: the vertical field of view, 40 by default
      • tilt: how far above the action the camera sits, as a fraction of its distance; 0.18 by default
      • blockDepth: how deep solid ground extrudes, in tiles; 1.6 by default
    • setDepthTextures

      public void setDepthTextures(Image solid, Image ledge, Image hazard, Image ladder)

      Textures the depth mode's blocks with tile art instead of flat colour. Every face of a block shows the whole image, so a tile with grass along its top reads as a grassy block from the front and the side. Any argument may be null to keep that kind's colour. Call before the view starts.

      Parameters
      • solid: solid ground and slopes
      • ledge: one-way ledges
      • hazard: spikes
      • ladder: ladders
    • setDepthColors

      public void setDepthColors(int solid, int ledge, int hazard, int ladder)

      The 2.5D block colors.

      Parameters
      • solid: ground and slopes
      • ledge: one-way ledges
      • hazard: spikes
      • ladder: ladders
    • onSetup

      protected void onSetup(GraphicsDevice device)
      Description copied from class: GameView

      Override to allocate GPU resources (meshes, textures, Models) once the GPU device is ready. Invoked once on the render thread before the first frame -- the only place you can call com.codename1.gpu.Primitives / com.codename1.gpu.GltfLoader, which need the device. The default does nothing.

      Parameters
      • device: the GPU device for creating meshes, textures and buffers
      Overrides:
      onSetup in class GameView
    • addBackground

      public void addBackground(Image image, float factor, double y)

      Adds a parallax backdrop behind the level, repeated across the view.

      Parameters
      • image: the layer
      • factor: how much it scrolls with the world; 0 pinned, 1 with the level
      • y: its top in world pixels
    • setArtScale

      public void setArtScale(double scale)

      How many art pixels the PlatformArt draws per world pixel. A level with 16 pixel tiles and art drawn at 64 pixels a tile uses 4: tiles, sprites and backdrops are then drawn at their full resolution instead of being scaled up from the level's own pixel size. Call before #start().

      Parameters
      • scale: art pixels per world pixel, 1 by default
    • refreshTiles

      public void refreshTiles()
      Draws every tile again from the art on the next frame. Call it when the art changes what a tile looks like -- a new palette, a new tileset -- since the view keeps the tiles it has drawn.
    • getArtScale

      public double getArtScale()

      Art pixels per world pixel.

      Returns

      the art scale

    • setVisibleRows

      public void setVisibleRows(double rows)

      How many rows of tiles fill the view's height; sets the scale.

      Parameters
      • rows: rows, 12 by default
    • setMinVisibleColumns

      public void setMinVisibleColumns(double cols)

      Keeps at least this many tile columns in view, so a narrow screen (a phone held upright) zooms out instead of hiding what is ahead. The view shows setVisibleRows rows or this many columns, whichever needs the smaller tiles. 0, the default, fits rows only.

      Parameters
      • cols: columns, or 0
    • setPlayerMappers

      public void setPlayerMappers(InputMapper... mappers)

      Reads one mapper per player, for a shared-screen game (PlatformWorld#setPlayerCount(int)): each tick player i gets what mappers[i] reads, typically InputMapper#forPlayer(int). An InputSource, when set, still has the last word: it receives player one's bits as local and can read the rest from getPlayerInputs().

      Parameters
      • mappers: one per player, or null for a single player
    • getPlayerInputs

      public int[] getPlayerInputs()

      What each player's mapper read this tick.

      Returns

      the live array, or null without setPlayerMappers

    • setModels

      public void setModels(PlatformModels models)

      Draws actors as 3D models in depth mode. See PlatformModels.

      Parameters
      • models: the models, or null for billboards
    • setZoom

      public void setZoom(double zoom)

      Zooms the view: 2 shows half as much at twice the size. The change is eased over a few frames.

      Parameters
      • zoom: above 0
    • getZoom

      public double getZoom()

      The zoom set by setZoom.

      Returns

      the zoom

    • setFrameAllPlayers

      public void setFrameAllPlayers(boolean all)

      Whether, with more than one player (PlatformWorld.setPlayerCount), the camera frames all of them and zooms out as they spread. On by default; off follows the first player only.

      Parameters
      • all: true to frame every player
    • getFeedback

      public Feedback getFeedback()

      The feedback, to change what events look and sound like.

      Returns

      the feedback

    • setFeedback

      public void setFeedback(Feedback feedback)

      Replaces the feedback.

      Parameters
      • feedback: the feedback
    • setFeedbackSink

      public void setFeedbackSink(Feedback.Sink sink)

      Receives the sounds and particle bursts of the feedback.

      Parameters
      • sink: the sink, may be null
    • getInputMapper

      public InputMapper getInputMapper()

      The input mapper, to remap keys.

      Returns

      the mapper

    • setInputMapper

      public void setInputMapper(InputMapper mapper)

      Replaces the input mapper.

      Parameters
      • mapper: the mapper
    • setInputSource

      public void setInputSource(PlatformView.InputSource source)

      Drives the world from somewhere other than the local devices.

      Parameters
      • source: the source, or null for the local player
    • getWorld

      public PlatformWorld getWorld()

      The world being played.

      Returns

      the world

    • getArt

      public PlatformArt getArt()

      The art the view draws with.

      Returns

      the art

    • getLastInput

      public int getLastInput()

      The Input bits the local devices produced on the last tick, for a recorder.

      Returns

      the bits

    • update

      protected void update(double deltaSeconds)
      Description copied from class: GameView
      Advance the game by the given amount of time. Called once per frame (or repeatedly at a fixed interval when #setFixedTimestep(double) is used).
      Specified by:
      update in class GameView
    • onTick

      protected void onTick(int localInput)

      Called after every tick, before the events are played.

      Parameters
      • localInput: what the local devices read
    • onEvent

      protected void onEvent(WorldEvent e)

      Called for every event of every tick, after its feedback played.

      Parameters
      • e: the event
    • addGhost

      public void addGhost(PlatformWorld ghostWorld, int[] inputs, int alpha)

      Races a recorded run alongside this one: ghostWorld is stepped with inputs on every tick the live world steps, and its hero is drawn translucent. Nothing it does reaches the live world, the feedback or the events -- it is a picture of another run, which is exactly what a replayed log reproduces. Build ghostWorld from the same level and seed the run was recorded with, and add it before the first tick.

      Parameters
      • ghostWorld: a fresh world for the ghost
      • inputs: its input for every tick, as InputLog.toArray gives it
      • alpha: how opaque to draw it, 0 to 255
    • clearGhosts

      public void clearGhosts()
      Removes every ghost.
    • getGhostWorlds

      public List<PlatformWorld> getGhostWorlds()

      The ghosts' worlds, in the order they were added, so a HUD can say who is ahead.

      Returns

      a new list

    • frame

      public void frame(double deltaSeconds)
      Positions everything for this frame, between the last two ticks.
      Overrides:
      frame in class GameView
    • createParticleImage

      protected Image createParticleImage()

      The image a particle is drawn with: a soft white dot, tinted per burst. Override for sparks, leaves or embers.

      Returns

      the particle image