Book Image

JMonkeyEngine 3.0 Cookbook

By : Rickard Eden
Book Image

JMonkeyEngine 3.0 Cookbook

By: Rickard Eden

Overview of this book

Table of Contents (17 chapters)
jMonkeyEngine 3.0 Cookbook
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Creating a reusable AI control class


In this recipe, we will create a control that is going to steer an AI character. Using Control to do this is beneficial since it can add the AI functionality and be used together with other Controls in the game. We can use GameCharacterControl from Chapter 2, Cameras and Game Controls for both the player and AI characters by adding AIControl to its spatial. To get a quick and visual result, we'll apply it to the bullet-based BetterCharacterControl class in this recipe.

How to do it...

We need to perform the following steps to get a basic, but functional attacking (or following) AI:

  1. We begin by creating a new class called AIControl, extending AbstractControl. The core of the recipe will be based around an enum (enumeration) called state. For now it only needs two values: Idle and Follow.

  2. Add fields for BetterCharacterControl, called physicsCharacter, Booleans forward and backwards, a Vector3f field for walkDirection, and another for viewDirection. If it's...