bad RPG AI
This commit is contained in:
@@ -1,6 +1,11 @@
|
||||
(defvar &inline :Float SPEED 140)
|
||||
|
||||
(defprop &mut :EnemyType type null)
|
||||
(defprop &mut :FSM brain null)
|
||||
(defprop &mut :Float idleTimer null)
|
||||
(defprop &mut :Float moveDirection null)
|
||||
(defprop &mut :Bool seesPlayer false)
|
||||
(defprop &mut :FlxPoint playerPosition null)
|
||||
|
||||
(defmethod new [:Float x :Float y :EnemyType type]
|
||||
(super x y)
|
||||
@@ -24,6 +29,9 @@
|
||||
(set height 14)
|
||||
(set offset.x 4)
|
||||
(set offset.y 2)
|
||||
(set brain (new FSM idle))
|
||||
(set idleTimer 0)
|
||||
(set playerPosition (FlxPoint.get))
|
||||
(return))
|
||||
|
||||
(defmethod &override update [:Float elapsed]
|
||||
@@ -46,7 +54,35 @@
|
||||
(animation.play "u"))
|
||||
|
||||
(FlxObject.DOWN
|
||||
(animation.play "d")))
|
||||
(animation.play "d"))))
|
||||
|
||||
(super.update elapsed))
|
||||
(brain.update elapsed)
|
||||
(super.update elapsed)
|
||||
(return))
|
||||
|
||||
(defmethod idle [:Float elapsed]
|
||||
(cond
|
||||
(seesPlayer
|
||||
// TODO (the FSM) here should not be necessary!
|
||||
(set .activeState (the FSM brain) chase))
|
||||
((<= idleTimer 0)
|
||||
(if (FlxG.random.bool 1)
|
||||
{
|
||||
(set moveDirection -1)
|
||||
(set velocity.x (set velocity.y 0))
|
||||
}
|
||||
{
|
||||
(set moveDirection (* 45 (FlxG.random.int 0 8)))
|
||||
(velocity.set (* SPEED 0.5) 0)
|
||||
(velocity.rotate (FlxPoint.weak) moveDirection)
|
||||
})
|
||||
(set idleTimer (FlxG.random.int 1 4)))
|
||||
(true
|
||||
(-= idleTimer elapsed)))
|
||||
(return))
|
||||
|
||||
(defmethod chase [:Float elapsed]
|
||||
(if !seesPlayer
|
||||
(set brain.activeState idle)
|
||||
(FlxVelocity.moveTowardsPoint this playerPosition (Std.int SPEED)))
|
||||
(return))
|
4
projects/flixel/rpg-tutorial/source/FSM.hx
Normal file
4
projects/flixel/rpg-tutorial/source/FSM.hx
Normal file
@@ -0,0 +1,4 @@
|
||||
package;
|
||||
|
||||
@:build(kiss.Kiss.build("source/FSM.kiss"))
|
||||
class FSM {}
|
9
projects/flixel/rpg-tutorial/source/FSM.kiss
Normal file
9
projects/flixel/rpg-tutorial/source/FSM.kiss
Normal file
@@ -0,0 +1,9 @@
|
||||
(defprop &mut :Float->Void activeState null)
|
||||
|
||||
(defmethod new [:Float->Void initialState]
|
||||
(set activeState initialState)
|
||||
(return))
|
||||
|
||||
(defmethod update [:Float elapsed]
|
||||
(activeState elapsed)
|
||||
(return))
|
@@ -39,6 +39,17 @@
|
||||
(super.update elapsed)
|
||||
(FlxG.collide player walls)
|
||||
(FlxG.overlap player coins playerTouchCoin)
|
||||
(FlxG.collide enemies walls)
|
||||
(enemies.forEachAlive checkEnemyVision)
|
||||
(return))
|
||||
|
||||
(defmethod checkEnemyVision [:Enemy enemy]
|
||||
(if (walls.ray (enemy.getMidpoint) (player.getMidpoint))
|
||||
{
|
||||
(set enemy.seesPlayer true)
|
||||
(set enemy.playerPosition (player.getMidpoint))
|
||||
}
|
||||
(set enemy.seesPlayer false))
|
||||
(return))
|
||||
|
||||
(defmethod playerTouchCoin [:Player player :Coin coin]
|
||||
|
@@ -7,6 +7,7 @@ import flixel.FlxSprite;
|
||||
import flixel.util.FlxColor;
|
||||
import flixel.input.keyboard.FlxKey;
|
||||
import flixel.math.FlxPoint;
|
||||
import flixel.math.FlxVelocity;
|
||||
import flixel.FlxObject;
|
||||
import flixel.addons.editors.ogmo.FlxOgmo3Loader;
|
||||
import flixel.group.FlxGroup;
|
||||
|
Reference in New Issue
Block a user