65.938 articles65.9K
CodeProject is changing. Read more.
Home
  • home
  • articles
    • Browse topics>
      • Artificial Intelligence>
        • Automation
        • big-data
        • machine-learning
      • Containers>
        • docker
        • virtual-machine
      • Database Development>
        • Elasticsearch
        • Lucene
        • mongodb
        • MySQL
        • NoSQL
        • PostgreSQL
        • Redis
        • SQL Server
      • Desktop Programming>
        • ATL
        • Cocoa
        • MFC
        • QT
        • Swing
        • system
        • UWP
        • Win32
        • WinForms
        • WPF
        • WTL
        • X11
        • XAML
      • DevOps>
        • Agile
        • Continuous-build
        • Continuous-delivery
        • Deployment
        • Git
        • load-testing
        • testing
        • TFS
        • unit-testing
      • Game Development>
        • Unity
        • Unreal
        • XBox
      • High Performance Computing>
        • GPU
        • optimization
      • Internet of Things>
        • wearable
      • Mobile Apps>
        • Android
        • AWS
        • Azure
        • Cordova
        • ExtJS
        • google-cloud
        • iOS
        • nativescript
        • PhoneGap
        • storage
        • Windows Mobile
        • Xamarin
      • Multimedia>
        • ASM
        • ASP
        • audio
        • audio video
        • Bash
        • Basic
        • C#
        • C++
        • cobol
        • Compression
        • Dart
        • dbase
        • Design/Graphics
        • DirectX
        • Emulation
        • Exceptions
        • F#
        • file
        • GDI
        • GDI+
        • Image-Processing
        • Internet
        • Java
        • Javascript
        • localization
        • Lua
        • MSIL
        • Objective C
        • OpenGL
        • Pascal
        • Perl
        • PHP
        • PowerShell
        • printing
        • Python
        • Razor
        • regular expression
        • Ruby
        • scala
        • Shell
        • Sorting
        • SQL
        • string
        • Swift
        • threads
        • usability
        • VB
        • VB.NET
        • VBScript
        • Video
        • XML
      • Security>
        • blockchain
        • cryptography
        • Encryption
      • Web Development>
        • Apache
        • ASP.NET
        • CSS
        • CSS3
        • HTML
        • HTML5
        • IIS
        • Kestrel
        • LESS
        • Node.js
        • React
        • Spring
        • XHTML
    • Latest articles
    • Top articles
  • features
    • News
    • The Insider Newsletter
    • The Daily Build Newsletter
    • CodeProject Stuff
  • help
    • What is 'CodeProject'?
    • General FAQ
    • About Us
2dGDI+Design / GraphicsIntermediateDev.NETVisual Basic

WestWorld v5

Terence Wallace
emptyStarIconemptyStarIconemptyStarIconemptyStarIconemptyStarIcon

0/5 (0 vote)

Sep 23, 2019

CPOL

3 min read

viewsIcon

4853

downloadIcon

187

A continuing series demonstration of Finite State Machines (FSM) where agents inhabit a small town called WestWorld.

 
  • Download source code (WestWorld5) - 906.5 KB
  • Download demo (WestWorld5) - 935.1 KB

Note: This is Part 5 of a CodeProject Article Series entitled WestWorld. To get more background information for this article:

  • WestWorld Series 1-3
  • WestWorld Series 4
  • WestWorld Series 5 (this article)

WestWorld Screen Capture

Introduction

A practical example of how to create agents that utilize finite state machines. In this article, I focus path finding in a tile based 2D Map. We are going to look at the game dynamics of the AStar PathFinding Algorithm. There are two inhabitants of WestWorld — a gold miner named Miner Bob and his Wife, Amanda.

In the history of this series we have covered the following topics:

  • WestWorld1 - Basics of Finite State Machine(FSM)
  • WestWorld2 - FSM with two agents (Miner & Wife)
  • WestWorld3 - Messaging between two FSM Agents
  • WestWorld4 - Intro of WindowsForms GDI+
  • WestWorld5- (this article) 2D Map Rendering

Background

The aim of this tutorial is to show you how to create a simple game, from scratch, without the help of higher level APIs like XNA or DirectX, or Unity which automate the process for you. All we will be using is Windows Form GDI+ to perform basic map rendering.

Map Generation

In this series of the article I found it beneficial to utilize Tiled the open source and free map editor.

Tiled is a general purpose tile map editor. It is a free tool that allows the easy creation of tile map layouts. It is versatile enough to allow abstract things such as collision areas, enemy spawn positions, or power-up positions. It saves all of this data in a convenient, standardized *.tmx format.

Object Layer

For the purpose of this article we will only need to utilize an Object Layer and a couple of Tile Layers. The Object Layer is where we will specify special gameplay elements. Using objects you can add a great deal of information to your map for use in your game. The Objects in Object Layer can replace tedious alternatives like hardcoding coordinates (like spawn points, exits, entrances, etc.) in your source code. You can also maintain additional data about your Object tiles for storing gameplay elements.

Tile Layer

To keep the Tile Layer simple this project uses an open source graphics file. One of many can be located here: OpenPixels on Github.

Custom Properties

One of the major strengths of using Tiled is that it allows setting custom properties on all of its basic data structures. For the purpose of WestWorld I am using Custom Properties to store information that will be used to match an Enum value in game.

TiledBasic

To read the *.tmx file I am using a modified version of TileSharp called TiledBasic. This implementation allows me to read into my class and game info. TiledBasic is used for parsing and importing TMX files generated by Tiled. Each Tile in a *.tmx Map file is read into a Map class.

Tile Rendering

Once all of the tiles are read that can then be rendered on screen by one of the Draw() methods of the Scene class.

PathFinding

Pathfinding in the game helps to find the shortest route between two points. It is like a practical way to solve a maze. A* grid-based pathfinding works well for games because the characters can move along both the x- and y-axes. The adapted algorithm utilized in WestWorld allows the AI character to follow the path chosen based on the route to their goal.

In this version (WestWorld v.5) we have defined a 'hard-coded' route for the NPC. However, in a future and final version of the series we will discuss a more clever way to allow the Non-Playable Character(NPC) to 'think' and to choose (i.e. Decision Tree) which goal to pursue.

References

  • Alastair Aitchison
  • Several CodeProject articles
  • Apress, Learn VB .NET Through Game Programming (August 14, 2003)
  • Developer Reference: Programming Microsoft Visual Basic
  • Advertise
  • Privacy
  • Cookies
  • Terms of Use
Copyright © CodeProject, 1999-2025
All Rights Reserved.