Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add support for LDtk tilemap #4575

Merged
merged 49 commits into from
Dec 22, 2022
Merged
Show file tree
Hide file tree
Changes from 46 commits
Commits
Show all changes
49 commits
Select commit Hold shift + click to select a range
ab4e81d
refactor: rename TiledMap as TiledTileMap and add LDtkTileMap
daiyam Nov 21, 2022
f6bc811
feat: add level index UI
daiyam Nov 21, 2022
68222a9
feat: load sample1
daiyam Nov 22, 2022
39c170a
feat: support stack tiles
daiyam Nov 22, 2022
6169e69
feat: select level
daiyam Nov 23, 2022
eda42b3
test: pass all tests
daiyam Nov 23, 2022
8a2dc67
refactor: regroup files by action
daiyam Nov 23, 2022
b4d4177
style: reformat source files
daiyam Nov 23, 2022
39daf85
fix: collision map & test
daiyam Nov 23, 2022
7807eac
Merge branch 'master' into feat-ldtk
daiyam Nov 23, 2022
891fd84
refactor: improve variable names and comments
daiyam Nov 25, 2022
ffb1f93
feat: add tilemap resource
daiyam Nov 27, 2022
8cba0c0
feat: add background
daiyam Nov 27, 2022
15231d9
feat: add embedded resources mapping
daiyam Nov 28, 2022
9f4edbc
Merge branch 'master' into feat-ldtk
daiyam Nov 28, 2022
15b4bd9
fix: improve background and alpha
daiyam Nov 28, 2022
ed4cdc4
fix: correctly updating alpha
daiyam Nov 28, 2022
4276663
fix: correctly remove unused embedded resources
daiyam Nov 29, 2022
83d5859
enhance: use TileDefinition for tile stack
daiyam Dec 1, 2022
9622c19
fix: fixing bugs
daiyam Dec 2, 2022
a5e037f
fix: revert to bit flipping
daiyam Dec 2, 2022
9b0ac07
style: apply formatting
daiyam Dec 2, 2022
f5eae4a
fix: removeTile() correctly set to 0
daiyam Dec 2, 2022
791b1e4
feat: use json resource
daiyam Dec 3, 2022
d2e2843
fix: expose embedded resources in tilemap JSON
daiyam Dec 5, 2022
71d500f
chore: add flow to EmbeddedResourceSources.js
daiyam Dec 11, 2022
9edc755
chore: harden embedding resource loading
daiyam Dec 11, 2022
7e43d2d
Put back some methods in their initial order.
D8H Dec 13, 2022
b16de90
chore: revert TextureCache.ts to TileTextureCache.ts
daiyam Dec 13, 2022
6e13f63
style: format generated codes
daiyam Dec 13, 2022
91905e9
refactor: rename files/variables as instructed
daiyam Dec 16, 2022
9025f4a
chore: remove hash from TileDefinition
daiyam Dec 16, 2022
7d39444
chore: reformat files
daiyam Dec 16, 2022
18f38fe
Move TileTextureCache methods back to their original order.
D8H Dec 19, 2022
94b6998
fix: remove SetLevelIndex action
daiyam Dec 19, 2022
23a2735
refactor: rename setImage to setLevelBackgroundTexture
daiyam Dec 19, 2022
ddd424f
Merge branch 'master' into pr/4575
4ian Dec 21, 2022
50741d3
Adapt to the latest merge + various fixes
4ian Dec 21, 2022
69bb489
Fix renaming a resource not renaming the mapping to this resource in …
4ian Dec 21, 2022
bea66c8
Remove leftover debug code
4ian Dec 21, 2022
d65f67c
Fix flow
4ian Dec 22, 2022
7ef3ac2
Revert some files renaming.
D8H Dec 22, 2022
0291e59
Rebuild the library
D8H Dec 22, 2022
57d7ca5
Move back some files.
D8H Dec 22, 2022
04b10c8
Rebuild the library.
D8H Dec 22, 2022
0f77574
Scope the LevelIndex condition.
D8H Dec 22, 2022
4f2bf21
Fix type
D8H Dec 22, 2022
cb2349d
Fix useless parameter
4ian Dec 22, 2022
ca3042a
Simplify various data types
4ian Dec 22, 2022
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
Original file line number Diff line number Diff line change
Expand Up @@ -726,6 +726,7 @@ gd::String EventsCodeGenerator::GenerateParameterCodes(
metadata.GetType() == "fontResource" ||
metadata.GetType() == "imageResource" ||
metadata.GetType() == "jsonResource" ||
metadata.GetType() == "tilemapResource" ||
metadata.GetType() == "videoResource" ||
// Deprecated, old parameter names:
metadata.GetType() == "password" || metadata.GetType() == "musicfile" ||
Expand Down
68 changes: 68 additions & 0 deletions Core/GDCore/IDE/Project/ArbitraryResourceWorker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,11 @@ void ArbitraryResourceWorker::ExposeJson(gd::String& jsonName){
// do.
};

void ArbitraryResourceWorker::ExposeTilemap(gd::String& tilemapName){
// Nothing to do by default - each child class can define here the action to
// do.
};

void ArbitraryResourceWorker::ExposeVideo(gd::String& videoName){
// Nothing to do by default - each child class can define here the action to
// do.
Expand Down Expand Up @@ -92,6 +97,65 @@ void ArbitraryResourceWorker::ExposeResources(
}
}

void ArbitraryResourceWorker::ExposeEmbeddeds(gd::String& resourceName) {
if (resourcesManagers.empty()) return;
gd::ResourcesManager* resourcesManager = resourcesManagers[0];
daiyam marked this conversation as resolved.
Show resolved Hide resolved

gd::Resource& resource = resourcesManager->GetResource(resourceName);

if (!resource.GetMetadata().empty()) {
gd::SerializerElement serializerElement =
gd::Serializer::FromJSON(resource.GetMetadata());

if (serializerElement.HasChild("embeddedResourcesMapping")) {
bool anyEmbeddedResourceNameWasRenamed = false;
gd::SerializerElement& embeddedResourcesMappingElement =
serializerElement.GetChild("embeddedResourcesMapping");

for (const auto& child :
embeddedResourcesMappingElement.GetAllChildren()) {
const gd::String& targetResourceName =
child.second->GetValue().GetString();

if (resourcesManager->HasResource(targetResourceName)) {
std::cout << targetResourceName << std::endl;
gd::Resource& targetResource =
resourcesManager->GetResource(targetResourceName);
const gd::String& targetResourceKind = targetResource.GetKind();

gd::String potentiallyUpdatedTargetResourceName = targetResourceName;

if (targetResourceKind == "audio") {
ExposeAudio(potentiallyUpdatedTargetResourceName);
} else if (targetResourceKind == "bitmapFont") {
ExposeBitmapFont(potentiallyUpdatedTargetResourceName);
} else if (targetResourceKind == "font") {
ExposeFont(potentiallyUpdatedTargetResourceName);
} else if (targetResourceKind == "image") {
ExposeImage(potentiallyUpdatedTargetResourceName);
} else if (targetResourceKind == "json") {
ExposeJson(potentiallyUpdatedTargetResourceName);
} else if (targetResourceKind == "tilemap") {
ExposeTilemap(potentiallyUpdatedTargetResourceName);
} else if (targetResourceKind == "video") {
ExposeVideo(potentiallyUpdatedTargetResourceName);
}

if (potentiallyUpdatedTargetResourceName != targetResourceName) {
// The resource name was renamed. Also update the mapping.
child.second->SetStringValue(potentiallyUpdatedTargetResourceName);
anyEmbeddedResourceNameWasRenamed = true;
}
}
}

if (anyEmbeddedResourceNameWasRenamed) {
resource.SetMetadata(gd::Serializer::ToJSON(serializerElement));
}
}
}
}

void ArbitraryResourceWorker::ExposeResource(gd::Resource& resource) {
if (!resource.UseFile()) return;

Expand Down Expand Up @@ -154,6 +218,10 @@ class ResourceWorkerInEventsWorker : public ArbitraryEventsWorker {
gd::String updatedParameterValue = parameterValue;
worker.ExposeJson(updatedParameterValue);
instruction.SetParameter(parameterIndex, updatedParameterValue);
} else if (parameterMetadata.GetType() == "tilemapResource") {
gd::String updatedParameterValue = parameterValue;
worker.ExposeTilemap(updatedParameterValue);
instruction.SetParameter(parameterIndex, updatedParameterValue);
}
});

Expand Down
10 changes: 10 additions & 0 deletions Core/GDCore/IDE/Project/ArbitraryResourceWorker.h
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,11 @@ class GD_CORE_API ArbitraryResourceWorker {
*/
virtual void ExposeJson(gd::String &jsonName);

/**
* \brief Expose a Tilemap, which is always a reference to a "tilemap" resource.
*/
virtual void ExposeTilemap(gd::String &tilemapName);

/**
* \brief Expose a video, which is always a reference to a "video" resource.
*/
Expand All @@ -96,6 +101,11 @@ class GD_CORE_API ArbitraryResourceWorker {
*/
virtual void ExposeFile(gd::String &resourceFileName) = 0;

/**
* \brief Expose the embedded resources of the specified resource.
*/
virtual void ExposeEmbeddeds(gd::String &resourceName);

protected:
const std::vector<gd::ResourcesManager *> &GetResources() {
return resourcesManagers;
Expand Down
6 changes: 6 additions & 0 deletions Core/GDCore/IDE/Project/ResourcesInUseHelper.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,13 +40,15 @@ class ResourcesInUseHelper : public gd::ArbitraryResourceWorker {
std::set<gd::String>& GetAllAudios() { return GetAll("audio"); };
std::set<gd::String>& GetAllFonts() { return GetAll("font"); };
std::set<gd::String>& GetAllJsons() { return GetAll("json"); };
std::set<gd::String>& GetAllTilemaps() { return GetAll("tilemap"); };
std::set<gd::String>& GetAllVideos() { return GetAll("video"); };
std::set<gd::String>& GetAllBitmapFonts() { return GetAll("bitmapFont"); };
std::set<gd::String>& GetAll(const gd::String& resourceType) {
if (resourceType == "image") return allImages;
if (resourceType == "audio") return allAudios;
if (resourceType == "font") return allFonts;
if (resourceType == "json") return allJsons;
if (resourceType == "tilemap") return allTilemaps;
if (resourceType == "video") return allVideos;
if (resourceType == "bitmapFont") return allBitmapFonts;

Expand All @@ -68,6 +70,9 @@ class ResourcesInUseHelper : public gd::ArbitraryResourceWorker {
virtual void ExposeJson(gd::String& jsonResourceName) override {
allJsons.insert(jsonResourceName);
};
virtual void ExposeTilemap(gd::String& tilemapResourceName) override {
allTilemaps.insert(tilemapResourceName);
};
virtual void ExposeVideo(gd::String& videoResourceName) override {
allVideos.insert(videoResourceName);
};
Expand All @@ -80,6 +85,7 @@ class ResourcesInUseHelper : public gd::ArbitraryResourceWorker {
std::set<gd::String> allAudios;
std::set<gd::String> allFonts;
std::set<gd::String> allJsons;
std::set<gd::String> allTilemaps;
std::set<gd::String> allVideos;
std::set<gd::String> allBitmapFonts;
std::set<gd::String> emptyResources;
Expand Down
3 changes: 3 additions & 0 deletions Core/GDCore/IDE/Project/ResourcesRenamer.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,9 @@ class ResourcesRenamer : public gd::ArbitraryResourceWorker {
virtual void ExposeJson(gd::String& jsonResourceName) override {
RenameIfNeeded(jsonResourceName);
};
virtual void ExposeTilemap(gd::String& tilemapResourceName) override {
RenameIfNeeded(tilemapResourceName);
};
virtual void ExposeVideo(gd::String& videoResourceName) override {
RenameIfNeeded(videoResourceName);
};
Expand Down
9 changes: 5 additions & 4 deletions Core/GDCore/Project/CustomObjectConfiguration.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ gd::ObjectConfiguration &CustomObjectConfiguration::GetChildObjectConfiguration(
return badObjectConfiguration;
}
const auto &eventsBasedObject = project->GetEventsBasedObject(GetType());

if (!eventsBasedObject.HasObjectNamed(objectName)) {
gd::LogError("Tried to get the configuration of a child-object:" + objectName
+ " that doesn't exist in the event-based object: " + GetType());
Expand Down Expand Up @@ -78,7 +78,7 @@ bool CustomObjectConfiguration::UpdateProperty(const gd::String& propertyName,
}
const auto &eventsBasedObject = project->GetEventsBasedObject(GetType());
const auto &properties = eventsBasedObject.GetPropertyDescriptors();

return gd::CustomConfigurationHelper::UpdateProperty(
properties,
objectContent,
Expand Down Expand Up @@ -125,8 +125,7 @@ void CustomObjectConfiguration::DoUnserializeFrom(Project& project,
}
}

void CustomObjectConfiguration::ExposeResources(
gd::ArbitraryResourceWorker& worker) {
void CustomObjectConfiguration::ExposeResources(gd::ArbitraryResourceWorker& worker) {
std::map<gd::String, gd::PropertyDescriptor> properties = GetProperties();

for (auto& property : properties) {
Expand All @@ -148,6 +147,8 @@ void CustomObjectConfiguration::ExposeResources(
worker.ExposeVideo(newPropertyValue);
} else if (resourceType == "json") {
worker.ExposeJson(newPropertyValue);
} else if (resourceType == "tilemap") {
worker.ExposeTilemap(newPropertyValue);
} else if (resourceType == "bitmapFont") {
worker.ExposeBitmapFont(newPropertyValue);
}
Expand Down
4 changes: 2 additions & 2 deletions Core/GDCore/Project/CustomObjectConfiguration.h
Original file line number Diff line number Diff line change
Expand Up @@ -87,9 +87,9 @@ class CustomObjectConfiguration : public gd::ObjectConfiguration {
/**
* Initialize configuration using another configuration. Used by copy-ctor
* and assign-op.
*
*
* Don't forget to update me if members were changed!
*
*
* It's needed because there is no default copy for childObjectConfigurations
* and it must be a deep copy.
*/
Expand Down
1 change: 1 addition & 0 deletions Core/GDCore/Project/ObjectConfiguration.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

#include "GDCore/Project/Behavior.h"
#include "GDCore/Project/EffectsContainer.h"
#include "GDCore/Project/ResourcesManager.h"
#include "GDCore/Project/VariablesContainer.h"
#include "GDCore/String.h"
#include "GDCore/Tools/MakeUnique.h"
Expand Down
4 changes: 3 additions & 1 deletion Core/GDCore/Project/Project.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -982,8 +982,10 @@ void Project::ExposeResources(gd::ArbitraryResourceWorker& worker) {
// (this time for effects). Ideally, this method could be moved outside of
// gd::Project.

gd::ResourcesManager* resourcesManager = &GetResourcesManager();

// Add project resources
worker.ExposeResources(&GetResourcesManager());
worker.ExposeResources(resourcesManager);
platformSpecificAssets.ExposeResources(worker);

// Add layouts resources
Expand Down
36 changes: 36 additions & 0 deletions Core/GDCore/Project/ResourcesManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,8 @@ std::shared_ptr<Resource> ResourcesManager::CreateResource(
return std::make_shared<VideoResource>();
else if (kind == "json")
return std::make_shared<JsonResource>();
else if (kind == "tilemap")
return std::make_shared<TilemapResource>();
else if (kind == "bitmapFont")
return std::make_shared<BitmapFontResource>();

Expand Down Expand Up @@ -650,6 +652,40 @@ bool JsonResource::UpdateProperty(const gd::String& name,
return true;
}

void TilemapResource::SetFile(const gd::String& newFile) {
file = NormalizePathSeparator(newFile);
}

void TilemapResource::UnserializeFrom(const SerializerElement& element) {
SetUserAdded(element.GetBoolAttribute("userAdded"));
SetFile(element.GetStringAttribute("file"));
DisablePreload(element.GetBoolAttribute("disablePreload", false));
}

void TilemapResource::SerializeTo(SerializerElement& element) const {
element.SetAttribute("userAdded", IsUserAdded());
element.SetAttribute("file", GetFile());
element.SetAttribute("disablePreload", IsPreloadDisabled());
}

std::map<gd::String, gd::PropertyDescriptor> TilemapResource::GetProperties()
const {
std::map<gd::String, gd::PropertyDescriptor> properties;
properties["disablePreload"]
.SetValue(disablePreload ? "true" : "false")
.SetType("Boolean")
.SetLabel(_("Disable preloading at game startup"));

return properties;
}

bool TilemapResource::UpdateProperty(const gd::String& name,
const gd::String& value) {
if (name == "disablePreload") disablePreload = value == "1";

return true;
}

void BitmapFontResource::SetFile(const gd::String& newFile) {
file = NormalizePathSeparator(newFile);
}
Expand Down
41 changes: 41 additions & 0 deletions Core/GDCore/Project/ResourcesManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -373,6 +373,47 @@ class GD_CORE_API JsonResource : public Resource {
gd::String file;
};

/**
* \brief Describe a tilemap file used by a project.
*
* \see Resource
* \ingroup ResourcesManagement
*/
class GD_CORE_API TilemapResource : public Resource {
public:
TilemapResource() : Resource(), disablePreload(false) { SetKind("tilemap"); };
virtual ~TilemapResource(){};
virtual TilemapResource* Clone() const override {
return new TilemapResource(*this);
}

virtual const gd::String& GetFile() const override { return file; };
virtual void SetFile(const gd::String& newFile) override;

virtual bool UseFile() const override { return true; }

std::map<gd::String, gd::PropertyDescriptor> GetProperties() const override;
bool UpdateProperty(const gd::String& name, const gd::String& value) override;

void SerializeTo(SerializerElement& element) const override;

void UnserializeFrom(const SerializerElement& element) override;

/**
* \brief Return true if the loading at game startup must be disabled
*/
bool IsPreloadDisabled() const { return disablePreload; }

/**
* \brief Set if the tilemap preload at game startup must be disabled
*/
void DisablePreload(bool disable = true) { disablePreload = disable; }

private:
bool disablePreload; ///< If "true", don't load the tilemap at game startup
gd::String file;
};

/**
* \brief Describe a bitmap font file used by a project.
*
Expand Down
Loading