Search code examples
c#wpfdata-binding

Creating a game on WPF/ grid binding


my task is creating a Railroad Ink game on WPF. About game: https://store.steampowered.com/app/1592740/Railroad_Ink_Challenge/ The First trouble I faced is binding road peices to grid cells. How can I do this?

My first (un)clever idea is about creating control points on each cell. Something like this:enter image description here. Is this a good way to realise a mechanics? How can I set coordinats on each grid cell in anyway? Please help me go through this problem, offer your solutions.

P.S. I'm looking for a huge brain mentor for collabs and my accelerated learning. If you're interested for, you can find me in instagram: arnititov or tg: aurum_lupus.


Solution

  • Option 1.

    Don't use a grid as your panel.

    Use a uniformgrid instead.

    I would use an itemscontrol with a uniformgrid as itemspanel.

    Fill that with empty cell viewmodels - EmptyCellVM() bound from a List to itemssource of your itemscontrol.

    Index of viewmodel in list corresponds to position of cell. You need to work out cells per row so you can divide by that to give row a cell is in.

    They datatemplate into a square or whatever you want to see in an empty cell.

    Replace each with a TileVM when you're allocating terrain to a cell.

    DataTemplate a TileVM out into terrain.

    Inherit from TileVM for horizontal track vm, vertical track vm Each corresponding to a TileView which of course has track in that direction.

    Option 2

    Build a map editor and allow the user to draw shapes using ink.

    Translate that into geometries you use with a series of terrain viewmodels.

    You can template these out in a similar manner using an itemscontrol with a canvas as itemspanel.

    You could define a 2d array as a 2d grid abstraction so you can tell what sort of terrain is where. Advantage of this is your tracks can run wherever you like.

    You can use a listbox and selection to drive what sort of terrain you're drawing.

    This is a fairly substantial project but probably rather simpler than my game map editor.

    https://i.sstatic.net/rY5PJ.jpg

    Assuming you don't need elevation.