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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Custom level layouts in config #3

Merged
merged 3 commits into from Aug 29, 2021
Merged

Custom level layouts in config #3

merged 3 commits into from Aug 29, 2021

Conversation

kroltan
Copy link
Contributor

@kroltan kroltan commented Aug 29, 2021

I imagine this is something you might want to do yourself, but if you need some inspiration you can use this PR as a reference! Do not feel like you need to merge this. 馃槄


Basically, I removed the grid_width, grid_height and corner_walls fields from Config, and instead created an enum called Map, which can be one of two variants:

  • Box layout, which has the aforementioned properties and uses the existing level generation code;
  • Custom layout, which allows whoever is writing config.toml to draw a map with ASCII.

To be able to do that, I had to refactor the coordinate conversion function to use a new unified resource for the grid dimensions, since it can be obtained from possibly many locations now.

Here's how config.toml feels like now, using a custom map (before you ask, yes, it is. 馃槃):

theme = "solarized_light"
tick_length = 0.15
food_ticks = 8

[map]
type = "custom"
data = """
#################
#               #
#  >      >  <  #
#  #      #  #  #
#  #      #  #  #
#               #
#  > <   >      #
#  # #   #      #
#  # #   # ##v  #
#               #
#################
"""

Or using the generation algorithm:

theme = "solarized_light"
tick_length = 0.15
food_ticks = 8

[map]
type = "box"
width = 17
height = 13
corner_walls = true

@@ -63,3 +108,67 @@ impl Default for Theme {
}
}
}

fn deserialize_map_data<'de, D>(deserializer: D) -> Result<MapData, D::Error>
Copy link
Contributor Author

@kroltan kroltan Aug 29, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a bit hairy, I agree, but I really really wanted to use an authoring-friendly format for custom maps, instead of something cursed like:

[map.data]
0 = [ "wall", "wall", "empty", ...]
1 = ...

So, I followed the Serde docs on how to create a custom deserialization for a type. In this case I could just make this method instead using the decorator you see in line 57.

Basically we write this dummy struct that implements visitor. It has 1 required method, expecting, for the syntax error message, and optional visit_{type} methods. In this case I wanted to build the type from a string, so I implemented visit_str, and call deserializer.deserialize_str with the visitor. It is a bit clunky for something so simple but it kind of makes sense for more general visitors.

In the visit method, I split the input into lines and collect the characters into a MapData, which later will be used to spawn the actual entities.

@kroltan
Copy link
Contributor Author

kroltan commented Aug 29, 2021

There currently is a strange bug where the screen is sometimes for some reason wider than it should be, but I can't repro consistently, might be something funky on my PC.

@kroltan kroltan changed the title WIP: Custom level layouts in config Custom level layouts in config Aug 29, 2021
@kroltan kroltan changed the title Custom level layouts in config Custom level layouts in config Aug 29, 2021
@ElnuDev
Copy link
Owner

ElnuDev commented Aug 29, 2021

(before you ask, yes, it is. 馃槃)

Is the map supposed to have a message or something? 馃 I must be dumb

@kroltan
Copy link
Contributor Author

kroltan commented Aug 29, 2021

Is the map supposed to have a message or something? 馃 I must be dumb

Yeah 馃榿

@ElnuDev
Copy link
Owner

ElnuDev commented Aug 29, 2021

I imagine this is something you might want to do yourself, but if you need some inspiration you can use this PR as a reference! Do not feel like you need to merge this. sweat_smile

I'll merge this in! I was planning on later down the line getting support for more complex maps, LDtk seems really neat and I think it has support for Bevy, though I haven't looked into it too much. That's going to be in a while though so having the option for faster to make ASCII maps is a good idea.

@ElnuDev ElnuDev merged commit d8b9c24 into ElnuDev:main Aug 29, 2021
@ElnuDev
Copy link
Owner

ElnuDev commented Aug 29, 2021

Yeah grin

I actually haven't seen this meme before! I'll add it to my collection 馃槄

@ElnuDev ElnuDev added the enhancement New feature or request label Sep 2, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants