AI for enemy in SpriteKit
Asked Answered
D

2

5

I have been making a game in sprite kit for some time now. I have added enemies and am wondering how I can control them around the map using AI(just like in any other game).

What I want is for the enemy to wonder around the TMX map, turning corner depending on random number. I have tried to do this but have run into many problems. Does anyone know of any articles that would help me with this? I have done some research. "PathFinding" and "A*" come up, but with know explanation or sample code on how to do it. Any help would be greatly appreciated.

Dismantle answered 6/4, 2014 at 15:46 Comment(0)
K
5

Welcome to SO. Let me start by saying that I too am currently searching for exactly the same thing. Unfortunately the pickings have been kinda weak, at least what I found so far.

I did find a couple of very interesting reads:

  1. An article on the ghosts behavior of PacMan. Simple but very effective.

  2. Amit’s Game Programming Information. A more general discussion about making games.

  3. Gamasutra. An excellent resources for all things game design.

  4. Designing AI Algorithms For Turn-Based Strategy Games. A Gamasutra article which is extremely useful in explaining turn based AI in plain english.

All these are very useful in their own ways and make you think. However, nothing I have come across provides a plain worded explination on a bad guy's logic (the PacMan article came close). Searching Amazon for Game AI books yields a bunch of books which are extremely expensive and read more like advanced quantum theory.

I ended up deciding on a simple approach for my game. I have my bad guy decide between 2 possible states. Patrol Mode and Attack Mode.

In Patrol Mode he sits idle for a few seconds, walks left or right until he hits a wall or other object, runs (same rules as walking), climbs up and down ladders, and does the occasional jump. I use arc4random() to decide what he does next when his current action is completed. This provides a truly random behavior and makes the bad guy completely unpredictable.

Attack Mode happens when the player is within X distance of the bad guy. The bad guy now has a different set of actions to choose from. Run towards player, swing sword at player, jump up, and so on. Again I use the random function to make the bad guy unpredictable. The results so far have been really excellent. My bad guy behaves like one of those hard to beat 12 year kids playing Halo.

The bad guy will continue to do battle until he dies, the player dies or the player runs away and is no longer within the X distance required for Attack Mode. You can of course fine tune bad guy's behavior by limiting jumping, time between attacks and so on.

Hope this helps and got your creative juices flowing.

Kosciusko answered 6/4, 2014 at 16:20 Comment(0)
P
2

The Wikipedia page for the A* Search Algorithm here contains a psuedocode example. All you really need is an understanding of how the algorithm works then you should be able to implement it. Googling for iOS and A* brings up several tutorials.

You have a tile map so it should be relatively easy to do what you want without A* like this bit of psuedocode.

// An Update function for your Enemy
function Update
    paths = GetAllPathways()
    if paths.length == 0
        TurnAround()
    else
        randomPath = paths.Random()
        TurnTowards(randomPath)
    endif
    MoveForward()
end function

function GetAllPathways()
    paths = new Array()
    if CanGoForward()
        paths.push(forward)
    end if
    if CanGoLeft()
        paths.push(left)
    end
    if CanGoRight()
        paths.push(right)
    end
    return paths
end function

An algorithm like A* is really for more complex things (not random decision making) where you can have a complex or dynamic map and want your enemies to target something or the player dynamically which is where A* comes into play, determining a way through the world to find the path to it's target.

Preciosity answered 6/4, 2014 at 16:4 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.