Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion assets/schema/scene.schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,17 @@
"height": {
"type": "number"
},
"environmentTileMapPath": {
"type": "string"
},
"environmentCollisionMapPath": {
"type": "string"
},
"hierarchy": {
"type": "array",
"items": {
"$ref": "gameobject.schema.json"
}
}
}
}
}
8 changes: 8 additions & 0 deletions examples/blasters/configuration.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"project": {
"name": "blasters",
"description": "A simple 2D Shoot 'em up.",
"version": "1.0.0",
"main": "blasters.php"
}
}
64 changes: 59 additions & 5 deletions src/Core/GameObject.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
use Sendama\Engine\Core\Scenes\Interfaces\SceneInterface;
use Sendama\Engine\Core\Scenes\Scene;
use Sendama\Engine\Core\Scenes\SceneManager;
use Sendama\Engine\Physics\Physics;
use Sendama\Engine\Physics\Interfaces\ColliderInterface;
use Sendama\Engine\UI\Interfaces\UIElementInterface;

/**
Expand Down Expand Up @@ -44,6 +46,8 @@ class GameObject implements GameObjectInterface
* @var Renderer $renderer The renderer for the game object.
*/
protected Renderer $renderer;
protected bool $started = false;
protected bool $starting = false;

public SceneInterface $activeScene {
get {
Expand Down Expand Up @@ -426,13 +430,26 @@ public function suspend(): void
*/
public function start(): void
{
if ($this->isActive()) {
foreach ($this->components as $component) {
if ($component->isEnabled()) {
$component->start();
if (!$this->isActive() || $this->started) {
return;
}

$this->starting = true;

for ($index = 0; $index < count($this->components); $index++) {
$component = $this->components[$index];

if ($component->isEnabled()) {
$component->start();

if ($component instanceof ColliderInterface) {
Physics::getInstance()->addCollider($component);
}
}
}

$this->starting = false;
$this->started = true;
}

/**
Expand Down Expand Up @@ -482,7 +499,15 @@ public function update(): void
*/
public function activate(): void
{
$wasActive = $this->active;
$this->active = true;

if (!$this->started) {
$this->start();
} elseif (!$wasActive) {
$this->resume();
}

$this->getRenderer()->render();
}

Expand Down Expand Up @@ -511,6 +536,10 @@ public function getRenderer(): Renderer
*/
public function deactivate(): void
{
if ($this->active) {
$this->suspend();
}

$this->active = false;
$this->getRenderer()->erase();
}
Expand Down Expand Up @@ -556,6 +585,15 @@ public function addComponent(string $componentType): Component

$component = new $componentType($this);
$this->components[] = $component;

if ($this->started && $this->belongsToActiveScene()) {
$component->start();
}

if ($component instanceof ColliderInterface && $this->belongsToActiveScene()) {
Physics::getInstance()->addCollider($component);
}

return $component;
}

Expand All @@ -569,6 +607,22 @@ public function getComponentCount(): int
return count($this->components);
}

/**
* Determines whether a newly-added runtime component should be initialized immediately.
*
* @return bool
*/
private function belongsToActiveScene(): bool
{
$activeScene = SceneManager::getInstance()->getActiveScene();

if ($activeScene === null) {
return false;
}

return in_array($this, $activeScene->getRootGameObjects(), true);
}

/**
* Gets the index of the component specified on the specified GameObject.
*
Expand Down Expand Up @@ -677,4 +731,4 @@ public function getSprite(): Sprite
{
return $this->getRenderer()->getSprite();
}
}
}
93 changes: 85 additions & 8 deletions src/Core/Rendering/Renderer.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,18 @@
use Sendama\Engine\Core\Scenes\SceneManager;
use Sendama\Engine\IO\Console\Console;
use Sendama\Engine\IO\Console\Cursor;
use Sendama\Engine\Util\Unicode;

class Renderer extends Component implements CanRender
{
/**
* @var array{x: int, y: int, width: int, height: int}|null
*/
protected ?array $lastRenderedBounds = null;
/**
* @var string[]|null
*/
protected ?array $lastRenderedBackground = null;
/**
* The console cursor.
*
Expand Down Expand Up @@ -78,22 +83,33 @@ public final function renderAt(?int $x = null, ?int $y = null): void

$xOffset = $this->getGameObject()->getTransform()->getPosition()->getX() + ($x ?? 0);
$yOffset = $this->getGameObject()->getTransform()->getPosition()->getY() + ($y ?? 0);
$width = $this->sprite->getRect()->getWidth();
$height = $this->sprite->getRect()->getHeight();
$spriteBufferedImage = $this->sprite->getBufferedImage();
$currentBounds = [
'x' => $xOffset,
'y' => $yOffset,
'width' => $width,
'height' => $height,
];

if ($this->lastRenderedBounds !== null && $this->lastRenderedBounds !== $currentBounds) {
$this->restoreLastRenderedBackground();
}

for ($row = 0; $row < $this->sprite->getRect()->getHeight(); $row++) {
if ($this->lastRenderedBounds !== $currentBounds) {
$this->lastRenderedBackground = $this->captureBackground($xOffset, $yOffset, $width, $height);
}

for ($row = 0; $row < $height; $row++) {
Console::write(
implode($spriteBufferedImage[$row] ?? []),
$xOffset,
$yOffset + $row
);
}

$this->lastRenderedBounds = [
'x' => $xOffset,
'y' => $yOffset,
'width' => $this->sprite->getRect()->getWidth(),
'height' => $this->sprite->getRect()->getHeight(),
];
$this->lastRenderedBounds = $currentBounds;
}

/**
Expand All @@ -113,20 +129,81 @@ public final function eraseAt(?int $x = null, ?int $y = null): void
return;
}

$this->restoreLastRenderedBackground();
}

/**
* Restores the previously drawn region underneath this renderer.
*
* @return void
*/
private function restoreLastRenderedBackground(): void
{
if (!$this->lastRenderedBounds) {
return;
}

$xOffset = $this->lastRenderedBounds['x'];
$yOffset = $this->lastRenderedBounds['y'];
$width = $this->lastRenderedBounds['width'];
$height = $this->lastRenderedBounds['height'];

for ($row = 0; $row < $height; $row++) {
Console::write(
$this->getBackgroundRowSegment($xOffset, $yOffset + $row, $width),
$this->lastRenderedBackground[$row] ?? $this->getBackgroundRowSegment($xOffset, $yOffset + $row, $width),
$xOffset,
$yOffset + $row
);
}

$this->lastRenderedBounds = null;
$this->lastRenderedBackground = null;
}

/**
* Captures the visible background currently underneath the sprite bounds.
*
* @param int $xOffset
* @param int $yOffset
* @param int $width
* @param int $height
* @return string[]
*/
private function captureBackground(int $xOffset, int $yOffset, int $width, int $height): array
{
$background = [];

for ($row = 0; $row < $height; $row++) {
$background[] = $this->getCurrentBackgroundRowSegment($xOffset, $yOffset + $row, $width);
}

return $background;
}

/**
* Returns the best background segment for the given row by preferring current console content
* and falling back to static world-space tiles where the buffer is blank.
*
* @param int $xOffset
* @param int $yOffset
* @param int $width
* @return string
*/
private function getCurrentBackgroundRowSegment(int $xOffset, int $yOffset, int $width): string
{
$bufferSegment = Console::readLineSegment($xOffset, $yOffset, $width);
$worldSegment = $this->getBackgroundRowSegment($xOffset, $yOffset, $width);
$bufferGlyphs = Unicode::characters($bufferSegment);
$worldGlyphs = Unicode::characters($worldSegment);
$composed = '';

for ($column = 0; $column < $width; $column++) {
$bufferGlyph = $bufferGlyphs[$column] ?? ' ';
$worldGlyph = $worldGlyphs[$column] ?? ' ';
$composed .= $bufferGlyph !== ' ' ? $bufferGlyph : $worldGlyph;
}

return $composed;
}

/**
Expand Down
Loading
Loading