Book Image

Cocos2d Game Development Blueprints

By : Jorge Jordán
Book Image

Cocos2d Game Development Blueprints

By: Jorge Jordán

Overview of this book

Table of Contents (15 chapters)
Cocos2d Game Development Blueprints
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Taking human decisions


Our enemies should have a little Artificial Intelligence (AI) for them to decide when is the best moment to walk, search for the zombie, or attack him.

We're going to implement the enemies' AI by implementing a method that will be called constantly to take decisions every frame, so we will need to add the following lines at the end of the update method in GameScene.m:

    // Take decisions
    for (Human *human in _arrayOfHumans) {
        // If the human is doing nothing
        if ((int)human.state == humanStateStill) {
            [self takeDecisionWithHuman:human];
        }
    }

Each enemy will call to the method in which they will decide what action to take. Implement this method by adding the following code:

-(void) takeDecisionWithHuman:(Human *)human {
    float distance = ABS(human.humanSprite.position.x - _zombie.position.x);

    if ((distance > (human.contentSize.width / 2) && distance < _screenSize.width
         && !((int)human.state...