Dusk Engine

Sure! First, here’s the snippets from tilelayer.lua that are relevant:

Here’s how every tile is instantiated, as of when I got it:

local tile = display\_newSprite(imageSheets[sheetIndex], imageSheetConfig[sheetIndex]) tile:setFrame(tileGID)

What this does is make a sprite object using the tilesheet and specific GID using setFrame(). This means that we can change the frame to any other tile in the tilesheet! What I decided to do was add some custom properties that would let me know what other GIDs to use. For my purposes (specifically, opening a door by swtiching between “closed” and “open” states), I just want to specify one “alternateGID”. I tracked down the spot where properties are added in functions.lua, and added this code:

elseif key:match("^props:") then insertionTable = p.props k = key:sub(6) --insert check for animation here elseif key:match("^animation:") then insertionTable = p.props; k = key:sub(0); else

What this does is look for any property that starts with “animation:” (for my purposes, animation:alternateGID) and throws that into the tile’s .props table.

After the space marked – Add Properties and Add Tile to Layer, I added the following code:

-- Add animation -- Custom edit if(tile.props.animation) then if(tile.props.animation.alternateGID) then tile.props.animation.alternateGID = tonumber(tile.props.animation.alternateGID) + 1;                 --for some reason, the values of GIDs are off by 1 in Dusk tile.setAlternateGID = function(self) local currentGID = self.GID; self:setFrame( self.props.animation.alternateGID ); self.props.animation.alternateGID = currentGID; end end end

This adds a function, tile:setAlternateGID(), that lets me alternate between GIDs at will by accessing the property I added and calling :setFrame(). Other options might be to store a sequence of frames as a table and use that animation. (It could start when prompted by some trigger or just as soon as it the tile is created.)

I hope this helps!

I have a persistent problem that I’ve noticed others mention here, but haven’t been able to find a solution for: whenever I make an object in Tiled using an image, it is never created in the right spot along the y-axis. I think I’ve narrowed it down to this line inside objectlayer.lua:

obj.x, obj.y = o.x + (mapData.stats.tileWidth \* 0.5) + screen.left, o.y - (mapData.stats.tileHeight \* 0.5);

This looks like it directly controls the position of objects. If the aspect ratio of the screen is different than what my build settings are specified for (entirely likely, since I’m working on a video game right now), image objects tend to be several tile-heights above where I’ve drawn them in Tiled. It only works perfectly if I run it simulating an OUYA, whose screen dimensions I’m using. I’m exporting using .json, and the x- and y- values in the file match what Tiled says they are.

Does anyone have any advice? (I’m new to Git and have made a few custom edits to Dusk, so I’ve never updated Dusk since I started using it; if it’s already been fixed, I never got an update.) I can upload screenshots if it’d help.

OH-YEAH! (as Vector from Despicable Me says)

The early-early-early-early version of animated tiles is now complete! I just pushed a new version to GitHub. Dusk now allows animated tiles, and synchronizes them with culling, so they don’t get messed up from being removed and then created.

You can now specify anim:enabled = true in your properties. Then make your animation options with anim:options, and pass a table (Bang or JSON) for the animation options (the second table in Corona’s display.newSprite()). Check out maps/square_anim.json in the Git repository to see what I mean. Again, it’s in a very, very early stage, so I’ve only tested it with start, count, and time parameters in the animation options table. Custom frames (frames = {1, 5, 6, 6, 2} or similar) aren’t supported right now.

But it’s pretty cool! And AWESOME! Like when Luke flies through that canal thingy in the Death Star in Star Wars! Come to think of it, not quite like that…

Ahem.

  • Caleb

By the way, @jack.brown.255:

That doesn’t sound good. Can you file a GitHub issue please? I’ll look into it when I can.

  • Caleb

Thank you for the animation stuff! I can’t wait to try it :slight_smile:

One other thing: Is there an easy way to change tiles into other tiles, like for example I want to change a grass tile into some rock or dirt. Is this possible?

It would also be great to use this for some kind of “Fog of War”, when a character is moving around. Tacking it’s position and changing the “Fog Of War” layer tiles.

Thank you!

Daniela

Currently, no. Tiles are created with display.newImageRect, so they can’t change their frame. I’ve been thinking of a special property (!isSprite! or similar) to allow the user to specify which tiles should be created as sprites; you could then say !isSprite! on your grass tile and then Dusk would build them as sprites and you could cycle through them and set the frame. Would that work for you?

  • Caleb

That would be awesome!

When you do it this way:

I know it’s much to ask but can you please give a small example where to use the property settings (in which file exactly) and how to cycle through sprites?

And one more question:

Can this be used to access individual grass tiles by it’s coordinates for example, so only one grass tile can be changed to stone, dirt etc. (sprite frame)? This way I guess it will make possible all the features to change tiles on a map and using the changes with actions, like for example a character can open a door if he has the key, a bridge can be destroyed if he walks over it and so on.

Thank you for all your great help and feedback! I really appreciate it!

  • Daniela

When I refer to a “property”, that means the property in Tiled. So to use the !isSprite! property, you add !isSprite! = true to the tile properties.

When a tile is created when !isSprite! = true, you can then access the tile and just call :setFrame() on it, just like any other Corona sprite.
[lua]
map.layer[“tiles”].tile(1, 1):setFrame(5)
[/lua]

Oh, and there’s a simple sprite example in maps/square_animated.json. Look at the tile properties for the first tile.

By the way, something really cool I just posted on my tech website: http://gymbylcoding.github.io/posts/the-answer-to-the-evil-flickering-tile-lines-of-doom.html

  • Caleb

AWESOME! Both the info and the cool fix for the flickering lines. I fixed my tileset to avoid the problem, but now it even works with the ‘old’ tiles which is awesome!

Really great stuff Caleb! :slight_smile:

Best,

Daniela

Done! Thanks for taking a look at it. It’s minor, but it’s been plaguing me for a while now.

I can’t figure out how to work with actual screen coordinates using dusk. Can someone please give some hints to the right direction!?

When touching the screen (map) I need the touch coordinates in pixels (not tiles). I’m using the camera following a character on screen and I can use the finger to move the map around. What I want to achieve here is: I want to touch the screen and then draw a line from the current character position (in pixels, not tiles) to the touch event. Somehow I can’t figure out to do this because the coordinates seem to be off. I know about the contentToLocal and localToContent, but I don’t know how to use it here to make the line drawing work. What exactly is the group here? The map itself is not working as group used with the localToContent.

Any ideas?

Thank you!

UPDATE:

I just saw I didn’t have access to the map in my part of the code. With older versions I got some errors (using Glider), but somehow with the new versions of Corona I don’t get this kind of error messages anymore.

I just noticed there still are some vertical 1px lines between tiles on a Retina iPad4

Maybe this is an easy fix because you already fixed the horizontal gaps?

I can work around the problem by putting an extra pixel around each tile (copying the outer border of the tile) and only using the inner area, as you mentioned before some posts ago… but it would be nice to have Dusk working without a graphical workaround :wink:

Best,

Daniela

Even though camera rounding does almost entirely eliminate the problem, I still recommend that you take two more preventative steps to fix it.

First of all, add a spacing of 1-2 pixels between your tiles. This has the dual purpose of making your tilesets easier to see for you and keeping Corona from doing some tile-bleeding stuff with OpenGL (read the first answer from http://gamedev.stackexchange.com/questions/74420/seamless-tilemap-rendering-borderless-adjacent-images)..)

Secondly, make sure these two lines of code are at the top of main.lua:
[lua]
display.setDefault(“minTextureFilter”, “nearest”)
display.setDefault(“magTextureFilter”, “nearest”)
[/lua]

Unfortunately, not having access to Corona’s graphics code, there isn’t a way I (as the developer of Dusk) can completely solve this problem. Apparently (from the StackExchange topic I linked above), to completely destroy the Flickering Tile Lines of Doom, you have to twiddle around with OpenGL.

By the way, is the question you asked just before this one still active? From your update, I couldn’t quite tell if you’d resolved it :).

  • Caleb

Is there a build-in save function, to save changes of object layers (or others)  and then override the original map data on a reload?

What do you mean? As in save to file? Or edit original data to load a different map with? Or what?

  • Caleb

resolved! :wink:

Everything works fine for me now. The 1px spacing is not shown when I’m using the bigger tiles, so Dusk is now perfect for the game we want to make. We just finished a first prototype and greenlit the game which will be an adventure-rpg type game. But I think we will work on a smaller project first with Dusk to figure out some more details on how to do things.

Great stuff Caleb! And thx again for your fast feedback!

Regarding torbenratzlaff post I think he means if there is a function to overwrite the json table (map) showing the map tile numbers.

Torbenratzlaff: I think you should look into Coronas Api to learn about such kind of table/file manipulation. A good place to start you can find here: http://coronalabs.com/blog/2014/10/14/tutorial-saving-and-loading-lua-tables-with-json/

I wish you all a Merry X-Mas!

Daniela

I mean something like a function that outputs the differences between the current state of an object layer and the original data in the map from Tiled. So I can save those changes easyly.

And on the other hand a loading function, that takes the saved changes as an input, and when a map is built, the loaded data is used instead of the original map.

@d.mach:
Glad everything’s working for you, good luck on your game!

@torbenratzlaff:
There is no built-in function for that in Dusk, but you can first load a map in a separate variable, then build it:
[lua]
local mapData = dusk.loadMap(“map.json”)
local map = dusk.buildMap(mapData)
[/lua]
Now that you have access to both objects, the map and its data, It shouldn’t be to hard to find the differences between them.

Optionally, you can use map.data.mapData - it’s the map’s “constructor data”. You need to be careful, though - the map.data.mapData has two added fields, _dusk and stats, for Dusk’s internal use. If you use a simple difference function, you’ll pick that up as a difference.

Merry Christmas to everyone!

  • Caleb

Thanks alot for your suggestions :slight_smile:

Merry Christmas to you too.

Is there a map method to get the position of the edges of the current screen in pixels i.e. top, bottom, left, right in pixels ?