Giter VIP home page Giter VIP logo

ldtkmonogame's Introduction

Discord Link ย 

LDtkMonogame is an level importer for the LDtk level editor

Getting Started

The easiest way to start using LDtkMonogame is to import it into the project using NuGet package.

Make sure to import the namespace at the top

using LDtk;
// Optional
using LDtk.Renderer;

LDtk.Renderer is a premade renderer for the levels, you can create your own if you have more specific needs LDtkRenderer.cs is an example of how to make one. Or you can inherit it and extend it.

To get started loading ldtk files load the file in Initialize.

LDtkFile file = LDtkFile.FromFile("World", Content);
LDtkFile file = LDtkFile.FromFile("Data/World.ldtk");

Then load the world right after for now ldtk only supports one file but make sure to enable the multiworlds flag in the project settings under advanced.

LDtkWorld world = file.LoadWorld(Worlds.World.Iid);

The Worlds.World.Iid is generated from the ldtkgen tool and is recommended that you use it for static typing of entities and levels.
It is a class within in a class that represents the world name and the levels name and holds the iid you can use to load that specific level.

Create the renderer in Initialize.

LDtkRenderer renderer = new LDtkRenderer(spriteBatch, Content);
LDtkRenderer renderer = new LDtkRenderer(spriteBatch);

Prerender Levels

foreach (LDtkLevel level in world.Levels)
{
    renderer.PrerenderLevel(level);
}

Now to render the level and entities we loaded in Draw

GraphicsDevice.Clear(world.BgColor);

spriteBatch.Begin(samplerState: SamplerState.PointClamp);
{
    foreach (LDtkLevel level in world.Levels)
    {
        renderer.RenderPrerenderedLevel(level);
    }
}
spriteBatch.End();

Showcase

Unnamed

screenshot

by Fypur

Play the game on Itch

Example Game

screenshot

by IrishBruse

Source code here

ldtkmonogame's People

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

ldtkmonogame's Issues

Help: How to pre-render individual layers?

I have multiple layers, one intended for the foreground and one intended for the background. The player need to render between these layers. Is there a way to pre-render individual layers, and subsequently draw individual layers?

Would the simplest way be making new instances of LDtkLevel for each layer?

LDtkLevelData generation seems wrong and some features/documentation seems missing

There are 2 issues relating to LDTKLevelData.

  1. LevelData is being generated as a class that implements ILDtkEntity. This makes no sense and also doesn't work. Below was generated:
public class LDtkLevelData : ILDtkEntity
{
    public string Identifier { get; set; }
    public System.Guid Iid { get; set; }
    public int Uid { get; set; }
    public Vector2 Position { get; set; }
    public Vector2 Size { get; set; }
    public Vector2 Pivot { get; set; }
    public Rectangle Tile { get; set; }
    public Color SmartColor { get; set; }
    public LevelType LevelType { get; set; }
}
  1. The docs don't explain how you are supposed to load level data with Generated types. I would assume we would want something like getLevelData<T>(level) or something but I don't see anything similar anywhere in the docs or repo.

Allow relative paths to assets

I've noticed that LDTK saves a relative path to the tileset files, i.e. "..\images\tileset.png".
When using this directly this line will fail:

return Texture2D.FromFile(graphicsDevice, Path.Combine("Content", path));

A solution would be to set LDtkLevel.FilePath and use that to allow relative loading. PR incoming.

Optional Fields on Enums

If an enum is declared as optional and you don't give it a value, then level.GetEntities() will throw a NullReferenceException.

Like I have this SpikeDirection enum in my Platform entity:
image

this happens:
image

Error when calling GetEntity or GetEntities for an Entity with an EntityReference property

I'm using source generation for my LDtk entities to have classes generated from their json objects, but when I try to get an object with an Entity reference as one of it's parameters, the game crashes with this error. If I remove the Entity Reference parameter, it works just fine.

System.NotSupportedException: JsonTypeInfo metadata for type 'LDtk.EntityReference' was not provided by TypeInfoResolver of type 'LDtk.LDtkJsonSourceGenerator'. If using source generation, ensure that all root types passed to the serializer have been annotated with 'JsonSerializableAttribute', along with any types that might be serialized polymorphically.
   at System.Text.Json.ThrowHelper.ThrowNotSupportedException_NoMetadataForType(Type type, IJsonTypeInfoResolver resolver)
   at System.Text.Json.JsonSerializerOptions.GetTypeInfoInternal(Type type, Boolean ensureConfigured, Nullable`1 ensureNotNull, Boolean resolveIfMutable, Boolean fallBackToNearestAncestorType)
   at System.Text.Json.JsonSerializerOptions.GetTypeInfoForRootType(Type type, Boolean fallBackToNearestAncestorType)
   at System.Text.Json.JsonSerializer.Deserialize(String json, Type returnType, JsonSerializerOptions options)
   at LDtk.LDtkFieldParser.ParseCustomFields[T](T classFields, FieldInstance[] fields, LDtkLevel level)
   at LDtk.LDtkFieldParser.ParseCustomEntityFields[T](T entity, FieldInstance[] fields, LDtkLevel level)
   at LDtk.LDtkLevel.GetEntityFromInstance[T](EntityInstance entityInstance)
   at LDtk.LDtkLevel.GetEntities[T]()

Feature Request: Add a toggle to use old-style Namespaces

I propose a flag that allows me to use old-style namespaces. If you use older versions of C# for targeting console, the code produced by the generator doesn't work as it's not valid code. I would be willing to make a PR.

Examples:

Currently Generated :

namespace MyNamespace;
public void MyClass{}

Desired Generation or Optional Formet:

namespace MyNamespace {
    public void MyClass{}
}

Loading the same tileset everytime LDtkRenderer draws a tile

hi, I'm trying to write a LDtkRenderer for my game using nez based on LDtkRenderer.cs and I notice that when I tried to use RenderLevel instead of PrerenderLevel my game began to drop fps(down to 30fps). After a bit of digging, I found that GetTexture is just loading the same tileset that had been loaded before into the memory everytime the Renderer tried to draw a sprite. I think maybe you can use the "tilesets" field in there and create a list of tilesets and make it faster maybe. I'm trying to write my own too. and sorry for the broken english but hope this help

Support for xnb files

Please add support for the xnb files instead of exposing png files. An example would be use the tilesetrelpath but remove the extension and try to content.load(...) where the current tileset png path would be.

@ World.cs : LoadAllLayers(...) =>
texture = Texture2D.FromFile(GraphicsDevice, Path.Combine(this.projectFolder, jsonLayerInstance.TilesetRelPath));

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.