Tutorial 3 — Un juego de plataformas: nivel, colisiones, cámara
Un nivel escrito en líneas
Para empezar, un nivel se escribe directamente en el código: un carácter por tesela de 16 × 16 píxeles.
private const LEVEL = [
'..........................C.C.C.........',
'.........................=======........',
'...P....................................',
'#########............C......######......',
'#########..........#####...########.....',
'########################################',
];
$this->map = ArrayLoader::fromRows(self::LEVEL,
legend: ['#' => 1, '=' => 2, '/' => 3, '\\' => 4], // las teselas
objects: ['P' => 'start', 'C' => 'coin'], // objetos colocados en el nivel
tileWidth: 16, tileset: 'generated:tiles');
$tileset = $this->map->tilesets[0];
$tileset->masks[1] = CollisionMask::oneWay(); // '=' : se atraviesa saltando desde abajo
$tileset->masks[2] = CollisionMask::slope(16, 16, 1, 16); // '/' : una pendiente
Cuando el juego crezca, dibuja los niveles con Tiled:
TiledJsonLoader::load($this->engine->config->path('assets/maps/niveau1.tmj')).
Las colisiones
Inyecta los servicios en el constructor de la escena (se proporcionan automáticamente):
public function __construct(
private TileMapRenderer $tiles,
private CollisionSystem $collisions,
private EntityManager $entities,
) {
}
En cada fotograma, se ajusta la velocidad y luego se mueve con el sistema de colisiones: avanza píxel a
píxel, se detiene en las paredes, se posa en el suelo, sube las pendientes y rellena $player->onGround.
$this->player->control($move, $jump, $held, $dt);
$this->player->update($dt);
$this->collisions->move($this->player, $this->map, $dt);
Caer fuera del nivel: $this->collisions->on('fell', fn () => $this->respawn()); (en enter(), después de
$this->collisions->removeAllListeners()).
Las monedas
Los objetos del nivel se convierten en entidades:
$this->entities->clear();
$this->entities->register('coin', Coin::class);
$this->entities->spawnFromMap($this->map);
// en update():
$this->entities->update($dt, $this->camera);
foreach ($this->entities->overlapping($this->player->getBoundingBox()) as $entity) {
if ($entity instanceof Coin) {
$entity->kill(); // se retira automáticamente
$this->coins++;
$this->sound()->playSfx('coin');
}
}
La cámara
$this->camera = new Camera($this->width(), $this->height());
$this->camera->setBounds(0, 0, $this->map->pixelWidth(), $this->map->pixelHeight());
$this->camera->follow($this->player, 0.15, 0.3); // una zona muerta en el centro de la pantalla
// update(): $this->camera->update($dt);
// render(): se dibuja todo desplazado según la cámara
$cx = $this->camera->getX();
$cy = $this->camera->getY();
$this->tiles->renderBackground($r, $this->map, $cx, $cy, $this->time);
$this->entities->render($r, $this->camera);
$this->player->render($r, $cx, $cy);
$this->tiles->renderForeground($r, $this->map, $cx, $cy, $this->time);
$this->camera->shake(4, 0.3) hace temblar la pantalla. El juego completo es el esqueleto de JumPHP
(src/Scene/PlayScene.php): bin/jumphp new mon-jeu te lo da, listo para modificar.