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
284 changes: 147 additions & 137 deletions composer.lock

Large diffs are not rendered by default.

117 changes: 112 additions & 5 deletions src/Core/GameObject.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
namespace Sendama\Engine\Core;

use InvalidArgumentException;
use ReflectionObject;
use Sendama\Engine\Core\Behaviours\Attributes\SerializeField;
use Sendama\Engine\Core\Interfaces\CanCompare;
use Sendama\Engine\Core\Interfaces\CanEquate;
use Sendama\Engine\Core\Interfaces\ComponentInterface;
Expand Down Expand Up @@ -236,6 +238,8 @@ public static function findAllWithTag(string $gameObjectTag): array
*/
public function __serialize(): array
{
$sprite = $this->renderer->getSprite();

return [
"hash" => $this->hash,
"name" => $this->name,
Expand All @@ -245,7 +249,7 @@ public function __serialize(): array
"scale" => $this->scale,
"transform" => $this->transform,
"render" => $this->renderer,
"sprite" => $this->sprite,
"sprite" => $sprite,
];
}

Expand Down Expand Up @@ -282,15 +286,117 @@ public function getScene(): SceneInterface
public function __clone(): void
{
$this->hash = md5(__CLASS__) . '-' . uniqid($this->name, true);
$this->started = false;
$this->starting = false;

$originalComponents = $this->components;
$position = clone $this->transform->getPosition();
$rotation = clone $this->transform->getRotation();
$scale = clone $this->transform->getScale();
$parent = $this->transform->getParent();
$currentSprite = $this->renderer->getSprite();
$sprite = $currentSprite ? clone $currentSprite : null;

$this->position = clone $position;
$this->rotation = clone $rotation;
$this->scale = clone $scale;
$this->sprite = $sprite;
$this->transform = new Transform($this, $position, $scale, $rotation, $parent);
$this->renderer = new Renderer($this, $sprite);
$this->components = [$this->transform, $this->renderer];

$this->transform = clone $this->transform;
$this->renderer = clone $this->renderer;
foreach ($originalComponents as $component) {
if ($component instanceof Transform || $component instanceof Renderer) {
continue;
}

if ($this->sprite) {
$this->sprite = clone $this->sprite;
$this->components[] = $this->cloneComponentForInstance($component);
}
}

/**
* Rebuild a component for a cloned game object and copy its serializable state.
*
* @param ComponentInterface $component
* @return ComponentInterface
*/
private function cloneComponentForInstance(ComponentInterface $component): ComponentInterface
{
$componentClass = $component::class;
/** @var ComponentInterface $componentClone */
$componentClone = new $componentClass($this);
$reflection = new ReflectionObject($componentClone);

foreach ($this->extractSerializableComponentData($component) as $propertyName => $value) {
if (!$reflection->hasProperty($propertyName)) {
continue;
}

$property = $reflection->getProperty($propertyName);
$property->setValue($componentClone, self::duplicateComponentValue($value));
}

if (!$component->isEnabled()) {
$enabledProperty = new \ReflectionProperty(Component::class, 'enabled');
$enabledProperty->setValue($componentClone, false);
}

return $componentClone;
}

/**
* Read serializable component data while skipping virtual accessors like activeScene/scene.
*
* @param ComponentInterface $component
* @return array<string, mixed>
*/
private function extractSerializableComponentData(ComponentInterface $component): array
{
$data = [];
$reflection = new ReflectionObject($component);

foreach ($reflection->getProperties() as $property) {
$isSerializable = $property->isPublic() || $property->getAttributes(SerializeField::class);

if (!$isSerializable) {
continue;
}

if (method_exists($property, 'isVirtual') && $property->isVirtual()) {
continue;
}

$data[$property->getName()] = $property->getValue($component);
}

return $data;
}

/**
* Duplicate nested values so prefab instances do not share mutable component state.
*
* @param mixed $value
* @return mixed
*/
private static function duplicateComponentValue(mixed $value): mixed
{
if (is_array($value)) {
$duplicate = [];

foreach ($value as $key => $item) {
$duplicate[$key] = self::duplicateComponentValue($item);
}

return $duplicate;
}

if (is_object($value)) {
return clone $value;
}

return $value;
}

/**
* Returns the transform of the game object.
*
Expand Down Expand Up @@ -721,6 +827,7 @@ public function setSpriteFromTexture(Texture|array|string $texture, Vector2 $pos
*/
public function setSprite(Sprite $sprite): void
{
$this->sprite = $sprite;
$this->getRenderer()->setSprite($sprite);
}

Expand Down
18 changes: 3 additions & 15 deletions src/Core/Prefab.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
use Sendama\Engine\Core\Interfaces\GameObjectInterface;
use Sendama\Engine\Core\Interfaces\PrefabCallbackInterface;
use Sendama\Engine\Core\Interfaces\PrefabInterface;
use Sendama\Engine\Exceptions\FileNotFoundException;
use Sendama\Engine\Core\Scenes\SceneManager;

/**
* Class Prefab is a class that represents a prefab in the engine.
Expand Down Expand Up @@ -84,19 +84,7 @@ public function instantiate(): GameObject
*/
public function load(string $path): void
{
// TODO: Implement load() method.
// Check if the file exists
if (! file_exists($path)) {
throw new FileNotFoundException("The file does not exist: $path");
}

// Load the file
$data = require($path);

// Deserialize the file


// Set the game object
$this->gameObject = SceneManager::loadPrefabFromPath($path);
}

public function __serialize(): array
Expand All @@ -118,4 +106,4 @@ public function pool(int $size): array
{
return GameObject::pool($this->gameObject, $size);
}
}
}
5 changes: 4 additions & 1 deletion src/Core/Rendering/SplashScreen.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,10 @@ public function show(): void
// Check if a splash texture can be loaded
if (!file_exists($this->getSettings('splash_texture'))) {
Debug::warn("Splash screen texture not found: {$this->settings[SettingsKey::SPLASH_TEXTURE->value]}");
$this->settings[SettingsKey::SPLASH_TEXTURE->value] = Path::join(Path::getVendorAssetsDirectory(), DEFAULT_SPLASH_TEXTURE_PATH);
$this->settings[SettingsKey::SPLASH_TEXTURE->value] = Path::join(
Path::getVendorAssetsDirectory(),
basename(DEFAULT_SPLASH_TEXTURE_PATH)
);
}

Debug::info("Loading splash screen texture");
Expand Down
Loading
Loading