Reader small image

You're reading from  Hands-On Game Development without Coding

Product typeBook
Published inNov 2018
Reading LevelBeginner
PublisherPackt
ISBN-139781789538335
Edition1st Edition
Languages
Tools
Right arrow
Author (1)
Lucas Bertolini
Lucas Bertolini
author image
Lucas Bertolini

Lucas Bertolini has 10+ years' experience as a video game software developer. He has worked on three major projects: for Pollux Ltd. (Hong Kong) as a game developer and designer; for Schell Games (Pittsburgh, US) where he moved and worked as a developer until the project was completed; and for Globant as a developer. He has worked in technical education for 5+ years and has taught a variety of programming courses. He is the cofounder of NGA and Bytenarchy Studios, both digital services development companies that use Unity as their main technology. Lucas has written Hands-On Game Development without Coding, available from Packt.
Read more about Lucas Bertolini

Right arrow

Enemy Logic - Basic AI

We have been reading about how to create our level and make it interact with our player, but until now, we haven't learned anything about our enemies. It's time to change that.

We will begin by learning about the basics of enemy AI. Usually, in simple games, enemies' decisions are made by a finite-state machine, which is a simple model of an AI.

In this chapter, we will learn about the following topics:

  • Finite-state machines
  • Understanding states and their transitions
  • Comprehending enemy behaviors in each state
  • Learning how to create a basic AI

What is a finite-state machine?

In short, a finite-state machine (FSM) is a computation model that is run through an algorithm in order to simulate sequential logic. This sequential logic is represented using states, which will control the essence of the behavior.

A more detailed explanation of an FSM is that it is a computation model that describes an imaginary or hypothetical machine that is made up of one or more states. The machine will have a certain set of behavior and decision-making processes, depending on which state is active, but only one state can be active at any given time. Transitions are made between states, depending on what the FSM designer decides; this is the only way an FSM can change from one state to another.

An FSM has the job of managing and controlling the transitions from state to state, the current state, and its behaviors. Depending on the behavior...

States diagram

Every FSM can be drawn as a state diagram. Each state will be contained in a box with arrows going from one state to another representing the transitions.

We will go through the same examples that we saw before and learn how we can create the diagrams step by step.

Turnstile

In order to create the diagram that belongs to the turnstile FSM, we need to go through the following steps:

  1. First of all, we have to write down the states, as follows:
  1. Next, we have to draw the transition arrow from one state to the other state that it will transition to. In this case, we have two transitions that interact in a cyclical way, as shown in the following diagram:

That's it. By drawing a diagram, it is simpler to...

Enemy FSM

We have been learning about the pillars and basics of FSM. Now that we know how they work, we will be able to understand the logic behind the enemies' AI.

We will go through the enemies, both in the 2D Game Kit and 3D Game Kit, and conclude the chapter by learning how their AI operates. This will make it easier to understand their components in the next chapter.

2D Game Kit

We will now read which types of actions each enemy will take when in Play mode, and with this information, we will finally design the FSM for the enemy.

Chomper

The Chomper enemy type...

Summary

This chapter focused on understanding the basics of FSM, an algorithm that represents a computation model that simulates sequential logic.

We learned that this sequential logic is run by different states that the machine adopts and acts on accordingly, which is basically the pillar of our enemies' behavior.

As well as learning about the principles of finite-state machines, we have also looked at different real-life examples in order to understand the essence of this model. This is probably a new concept for us, so we want to make sure that we are able to hold on to this information, and most of all, that we are able to interpret the concept. This is why we learned how to create an FSM diagram of the traffic lights, Walkman, and washing machine FSM, as well as for other examples.

Finally, after all those concepts were good and fresh in our minds, we read about the...

lock icon
The rest of the chapter is locked
You have been reading a chapter from
Hands-On Game Development without Coding
Published in: Nov 2018Publisher: PacktISBN-13: 9781789538335
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
undefined
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at €14.99/month. Cancel anytime

Author (1)

author image
Lucas Bertolini

Lucas Bertolini has 10+ years' experience as a video game software developer. He has worked on three major projects: for Pollux Ltd. (Hong Kong) as a game developer and designer; for Schell Games (Pittsburgh, US) where he moved and worked as a developer until the project was completed; and for Globant as a developer. He has worked in technical education for 5+ years and has taught a variety of programming courses. He is the cofounder of NGA and Bytenarchy Studios, both digital services development companies that use Unity as their main technology. Lucas has written Hands-On Game Development without Coding, available from Packt.
Read more about Lucas Bertolini