

Creating an Initial Game Board | Jamie Dixon's Home
source link: https://jamessdixon.com/2018/11/30/creating-an-initial-game-board/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

Creating an Initial Game Board
November 30, 2018 2 Comments
(Part 5 of the Panzer General Portable Project)
The first game-like task I wanted to accomplish was to set up the base game board – like I did here seven years ago. I fired up the F# Xamarin Forms (XF) project I was working on last post and open the app.fs file. Following the “Full-On Monty” Elm pattern, I am not going to do any of this app in markup so there are no XAML files to deal with.
Inside the app.fs file, I first created a record type to keep track of the tiles and units for each hex (later in this series, I will create a proper domain model – for now this is a just a spike to see what it takes to render the game board – I named it ContentData). Notice that ContentData always had a Tile, Row and Column, but it does not always have a unit on it – so I used an option type. for Unit. I also needed a type for the Tile, the Unit, and the Equipment – starting with image then moving out to additional data to make the game work. Enter type providers – all I needed to do was to point to the files that were already part of the solution to get the types
I then created three functions to get the data for each instance of those types
You first thought is “this screams refactoring to a high order function” and I would agree. I will do that in next iteration
In any event, I needed a way to create the actual image from the file path and then a way of hydrating the ContentData
With this function ready to return the image, I needed a way to put it in the correct place on the board. In XF, this is accomplished via the Rectangle type
With the individual hex functions ready, I was ready to put them onto the screen. In XF, there is a type called layout that seems what I need to position images in absolute positions
With the layout ready, I could create a function that takes in the layout, an individual hex’s contentdata, find it in the image files, and put it on the board. I used the Children property of the Layout type to push multiple images on the same hex – I am not sure about Z-ordering yet, so I put the unit image after the tile
With the individual hex functions ready, I could iterate through the first scenario and populate its initial board. Notice that I put the Content control inside a ScrollView type that then put into a ContentPage that is then put into the Page. I think the common convention is right – UX is very much OO in nature
So when I fired up the emulator, I got some pretty good first results
Out of the box with Win Phone, I could pinch and spread a location and the screen images would automatically adjust and get bigger/smaller. No such luck with XF – so I need to see how to do that. In any event, zooming into the bottom left corner, all Panzer General fans will immediate recognize this layout.
And it looks pretty good compared to the winphone
and the original game
Lots to do, but I am happy with the progress
Gist is here
Recommend
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK