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
1 change: 0 additions & 1 deletion examples/collector/assets/Scenes/SettingsScene.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ public function awake(): void
$screenWidth = $sceneManager->getSettings('screen_width') ?? DEFAULT_SCREEN_WIDTH;
$screenHeight = $sceneManager->getSettings('screen_height') ?? DEFAULT_SCREEN_HEIGHT;

Debug::log(var_export($this->settings, true));
$leftMargin = round($screenWidth / 2 - $settingsMenuWidth / 2);
$topMargin = round($screenHeight / 2 - $settingsMenuHeight / 2);
$settingsMenuBorderPack = new BorderPack(Path::join(Path::getVendorAssetsDirectory(), 'border-packs', 'slim.border.php'));
Expand Down
Binary file modified logo.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 0 additions & 1 deletion src/Core/Behaviours/SimpleBackListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ public function onUpdate(): void
$this->nextPrintTime = time() + 1;
}
if (Input::isAnyKeyPressed($this->backKeys)) {
Debug::log('Going back');
loadPreviousScene();
}
}
Expand Down
5 changes: 0 additions & 5 deletions src/Core/Scenes/SceneManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -111,11 +111,6 @@ public function hasScene(int|string $index): bool
{
$sceneList = $this->scenes->toArray();

Debug::log(var_export([
"index" => $index,
"total" => $this->scenes->count(),
"scenes" => array_map(fn(SceneInterface $scene) => $scene->getName(), $sceneList)
], true));
foreach ($sceneList as $i => $scene) {
if (is_int($index) && $i === $index) {
return true;
Expand Down
3 changes: 3 additions & 0 deletions src/Core/Scenes/TitleScene.php
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,7 @@ public function awake(): void
private function getMenuLeftMargin(): int
{
$screenWidth = $this->resolveScreenWidth();
Debug::log("Screen width: $screenWidth");
return (int)round($screenWidth / 2) - (int)round($this->menuWidth / 2);
}

Expand Down Expand Up @@ -263,10 +264,12 @@ public function addMenuItems(MenuItemInterface ...$item): self
* Resolves the screen width even while the scene is still in awake() and local scene settings are empty.
*
* @return int
* @throws Exception
*/
private function resolveScreenWidth(): int
{
return $this->resolveDimension(
get_screen_width(),
$this->screenWidth,
$this->sceneManager->getSettings('screen_width'),
$this->getSettings('screen_width'),
Expand Down
100 changes: 50 additions & 50 deletions src/Core/Traits/DimensionTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,58 +7,58 @@
*/
trait DimensionTrait
{
/**
* The width of the object.
*
* @var int
*/
protected int $width = 0;
/**
* The height of the object.
*
* @var int
*/
protected int $height = 0;
/**
* The width of the object.
*
* @var int
*/
protected int $width = 0;
/**
* The height of the object.
*
* @var int
*/
protected int $height = 0;

/**
* Get the width of the object.
*
* @return int The width of the object.
*/
public function getWidth(): int
{
return $this->width;
}
/**
* Get the width of the object.
*
* @return int The width of the object.
*/
public function getWidth(): int
{
return $this->width;
}

/**
* Set the width of the object.
*
* @param int $width The width of the object.
* @return void
*/
public function setWidth(int $width): void
{
$this->width = $width;
}
/**
* Set the width of the object.
*
* @param int $width The width of the object.
* @return void
*/
public function setWidth(int $width): void
{
$this->width = $width;
}

/**
* Get the height of the object.
*
* @return int The height of the object.
*/
public function getHeight(): int
{
return $this->height;
}
/**
* Get the height of the object.
*
* @return int The height of the object.
*/
public function getHeight(): int
{
return $this->height;
}

/**
* Set the height of the object.
*
* @param int $height The height of the object.
* @return void
*/
public function setHeight(int $height): void
{
$this->height = $height;
}
/**
* Set the height of the object.
*
* @param int $height The height of the object.
* @return void
*/
public function setHeight(int $height): void
{
$this->height = $height;
}
}
31 changes: 26 additions & 5 deletions src/Game.php
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ class Game implements ObservableInterface
*/
private UIManager $uiManager;
/**
* @var Cursor $consoleCursor
* @var Cursor|null $consoleCursor
*/
private ?Cursor $consoleCursor = null;
/**
Expand All @@ -139,15 +139,36 @@ class Game implements ObservableInterface
private ?SplashScreen $splashScreen = null;
private bool $consoleInitialized = false;

public int $screenWidth {
get {
if (is_null($this->resolvedScreenWidth)) {
return exec('tput cols') ?: 80;
}

return $this->resolvedScreenWidth;
}
}

public int $screenHeight {
get {
if (is_null($this->resolvedScreenWidth)) {
return exec('tput cols') ?: 80;
}

return $this->resolvedScreenHeight;
}
}

/**
* Game constructor.
*
* @param string $name The name of the game.
* @param int $screenWidth The width of the game screen.
* @param int $screenHeight The height of the game screen.
* @throws Exception
* @param int|null $resolvedScreenWidth The width of the game screen.
* @param int|null $resolvedScreenHeight The height of the game screen.
* @param string|null $workingDirectory
* @throws IOException
*/
public function __construct(private readonly string $name, private readonly int $screenWidth = DEFAULT_SCREEN_WIDTH, private readonly int $screenHeight = DEFAULT_SCREEN_HEIGHT, private readonly ?string $workingDirectory = null)
public function __construct(private readonly string $name, private readonly ?int $resolvedScreenWidth = null, private readonly ?int $resolvedScreenHeight = null, private readonly ?string $workingDirectory = null)
{
try {
$this->initializeObservers();
Expand Down
30 changes: 30 additions & 0 deletions src/IO/Console/Console.php
Original file line number Diff line number Diff line change
Expand Up @@ -277,6 +277,36 @@ public static function getSize(bool $force = false): Rect
return new Rect(new Vector2(1, 1), new Vector2($width, $height));
}

/**
* Returns the current logical viewport size (game screen dimensions).
*
* @return Rect The logical viewport size as a Rect starting at (1,1).
*/
public static function getLogicalSize(): Rect
{
return new Rect(new Vector2(1, 1), new Vector2(self::$logicalWidth, self::$logicalHeight));
}

/**
* Returns the logical viewport width.
*
* @return int
*/
public static function getLogicalWidth(): int
{
return max(1, self::$logicalWidth);
}

/**
* Returns the logical viewport height.
*
* @return int
*/
public static function getLogicalHeight(): int
{
return max(1, self::$logicalHeight);
}

/**
* Refreshes the logical viewport within the current terminal size.
*
Expand Down
4 changes: 3 additions & 1 deletion src/UI/GUITexture/GUITexture.php
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,9 @@ public function setSize(Vector2 $size): void

public function render(): void
{
$this->renderAt($this->position->getX(), $this->position->getY());
if ($this->isActive()) {
$this->renderAt($this->position->getX(), $this->position->getY());
}
}

public function renderAt(?int $x = null, ?int $y = null): void
Expand Down
2 changes: 2 additions & 0 deletions src/UI/UIElement.php
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,7 @@ public function setTag(string $tag): void
*/
public function activate(): void
{
$this->render();
$this->active = true;
}

Expand All @@ -159,6 +160,7 @@ public function activate(): void
*/
public function deactivate(): void
{
$this->erase();
$this->active = false;
}

Expand Down
42 changes: 21 additions & 21 deletions src/Util/Constants.php
Original file line number Diff line number Diff line change
@@ -1,29 +1,29 @@
<?php

/* Screen */
const MAX_SCREEN_WIDTH = 160;
const MAX_SCREEN_HEIGHT = 40;
const DEFAULT_SCREEN_WIDTH = MAX_SCREEN_WIDTH;
const DEFAULT_SCREEN_HEIGHT = MAX_SCREEN_HEIGHT;
defined('MAX_SCREEN_WIDTH') || define('MAX_SCREEN_WIDTH', 160);
defined('MAX_SCREEN_HEIGHT') || define('MAX_SCREEN_HEIGHT', 40);
defined('DEFAULT_SCREEN_WIDTH') || define('DEFAULT_SCREEN_WIDTH', MAX_SCREEN_WIDTH);
defined('DEFAULT_SCREEN_HEIGHT') || define('DEFAULT_SCREEN_HEIGHT', MAX_SCREEN_HEIGHT);
/* Dialogs */
const DEFAULT_DIALOG_WIDTH = 50;
const DEFAULT_DIALOG_HEIGHT = 3;
const DEFAULT_SELECT_DIALOG_WIDTH = 30;
const DEFAULT_DIALOG_X_POSITION = 0;
const DEFAULT_DIALOG_Y_POSITION = 0;
const DEFAULT_FPS = 60;
const DEFAULT_ASSETS_PATH = 'assets';
const DEFAULT_LOGS_DIR = 'logs';
const DEFAULT_LOG_LEVEL = 'debug';
defined('DEFAULT_DIALOG_WIDTH') || define('DEFAULT_DIALOG_WIDTH', 50);
defined('DEFAULT_DIALOG_HEIGHT') || define('DEFAULT_DIALOG_HEIGHT', 3);
defined('DEFAULT_SELECT_DIALOG_WIDTH') || define('DEFAULT_SELECT_DIALOG_WIDTH', 30);
defined('DEFAULT_DIALOG_X_POSITION') || define('DEFAULT_DIALOG_X_POSITION', 0);
defined('DEFAULT_DIALOG_Y_POSITION') || define('DEFAULT_DIALOG_Y_POSITION', 0);
defined('DEFAULT_FPS') || define('DEFAULT_FPS', 60);
defined('DEFAULT_ASSETS_PATH') || define('DEFAULT_ASSETS_PATH', 'assets');
defined('DEFAULT_LOGS_DIR') || define('DEFAULT_LOGS_DIR', 'logs');
defined('DEFAULT_LOG_LEVEL') || define('DEFAULT_LOG_LEVEL', 'debug');
/* Splash */
const DEFAULT_SPLASH_TEXTURE_PATH = DEFAULT_ASSETS_PATH . '/splash.texture';
const DEFAULT_SPLASH_SCREEN_DURATION = 3;
defined('DEFAULT_SPLASH_TEXTURE_PATH') || define('DEFAULT_SPLASH_TEXTURE_PATH', DEFAULT_ASSETS_PATH . '/splash.texture');
defined('DEFAULT_SPLASH_SCREEN_DURATION') || define('DEFAULT_SPLASH_SCREEN_DURATION', 3);
/* Windows */
const DEFAULT_WINDOW_WIDTH = 80;
const DEFAULT_WINDOW_HEIGHT = 30;
defined('DEFAULT_WINDOW_WIDTH') || define('DEFAULT_WINDOW_WIDTH', 80);
defined('DEFAULT_WINDOW_HEIGHT') || define('DEFAULT_WINDOW_HEIGHT', 30);
/* Grid */
const DEFAULT_GRID_WIDTH = 10;
const DEFAULT_GRID_HEIGHT = 10;
defined('DEFAULT_GRID_WIDTH') || define('DEFAULT_GRID_WIDTH', 10);
defined('DEFAULT_GRID_HEIGHT') || define('DEFAULT_GRID_HEIGHT', 10);
/* Menu */
const DEFAULT_MENU_WIDTH = 16;
const DEFAULT_MENU_HEIGHT = 3;
defined('DEFAULT_MENU_WIDTH') || define('DEFAULT_MENU_WIDTH', 16);
defined('DEFAULT_MENU_HEIGHT') || define('DEFAULT_MENU_HEIGHT', 3);
50 changes: 50 additions & 0 deletions src/Util/Functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -434,6 +434,31 @@ function get_screen_width(): int
{
return Console::getSize()->getWidth();
}

if (!function_exists('get_terminal_width')) {
/**
* Returns the actual terminal width (freshly polled).
*
* @return int
* @throws Exception
*/
function get_terminal_width(): int
{
return Console::getSize(force: true)->getWidth();
}
}

if (!function_exists('get_logical_width')) {
/**
* Returns the game's logical viewport width (may be different than terminal width).
*
* @return int
*/
function get_logical_width(): int
{
return Console::getLogicalWidth();
}
}
}

if (!function_exists('get_configured_screen_width')) {
Expand All @@ -459,6 +484,31 @@ function get_screen_height(): int
{
return Console::getSize()->getHeight();
}

if (!function_exists('get_terminal_height')) {
/**
* Returns the actual terminal height (freshly polled).
*
* @return int
* @throws Exception
*/
function get_terminal_height(): int
{
return Console::getSize(force: true)->getHeight();
}
}

if (!function_exists('get_logical_height')) {
/**
* Returns the game's logical viewport height.
*
* @return int
*/
function get_logical_height(): int
{
return Console::getLogicalHeight();
}
}
}

if (!function_exists('get_configured_screen_height')) {
Expand Down
Loading