Ok, so now we have a player who can run around the map.  Let's give him something to run from.  Lets code Death!

First I created a new class called Death.hx in my source folder.  I gave Death a base class of FlxSprite because I wanted to use the base functionality of a FlxSprite but I think Death is different enough that I don't want him having any of the code that I end up with in my Entity class.  I may regret this decision later, but it will work for now.

class Death extends FlxSprite
{
	
	//Death movement variables
	var currentSpeed:Int = 100;
	
	//Variable that holds who death should pursue.
	var target:Entity;
	
	public function new(X:Float=0, Y:Float=0, ?SimpleGraphic:Dynamic) 
	{
		super(X, Y);
		makeGraphic(60, 60, FlxColor.PURPLE);
	}
	
	public function setTarget(target:Entity) {
		this.target = target;
	}
	
	override public function update():Void 
	{
		super.update();
		
		//If we have a target...
		if (target != null) {
			acceleration = FlxAngle.rotatePoint(currentSpeed, 0, 0, 0, FlxAngle.angleBetween(this, target, true));
			
		}
		
	}
	
}

Death is pretty basic.  We've seen the new function before and created a temporary graphic for Death.  This time it's purple!  Death is a little bigger than my normal entities who are about 40x40 but he is a more imposing presence (well, as imposing as a purple box can be I guess) so that's fine.  Here's some things to note:

//Death movement variables
var currentSpeed:Int = 100;

This is the variable that will be used to control Death's speed.  How I code Death's movement will be as important as the player's movement to how the game is played, so I want to get it right.  The player will not only be avoiding Death the entire time they are playing the game, but also using Death to attack enemies.  Death needs to be maneuverable enough to be able to set him up to hit enemies but not so maneuverable that he becomes impossible to dodge.  When I picture it in my head, Death floats around the board and has some real momentum to him so when the player dodges him it will take him a second to turn around and start back after the player.  As a result, I'm going to move Death by affecting his acceleration instead of his velocity

It made sense for the player's velocity to be affected directly for the added control that brings, but Death's floating presence will be better represented by acceleration change rather than velocity.  The important thing to note is that the currentSpeed variable is actually the magntude of the acceleration vector so we are going to have to do some math to get it to work.  Well, we are going to make the computer do math, but we have to know what math to tell it to do.

public function setTarget(target:Entity) {
this.target = target;
}

This code is going to set the poor fool that Death will chase.  I'll always be passing it the player for now, but there might be something later on that needs me to change it. 

//If we have a target...
if (target != null) {
	acceleration = FlxAngle.rotatePoint(currentSpeed, 0, 0, 0, FlxAngle.angleBetween(this, target, true));
			
}

Here's where the meat of the actual movement code is.  The end result is that we want to get Death to accelerate at the target but we have to do some math to get there.  I'm using two functions in the FlxAngle class to do this.  First is the FlxAngle.angleBetween() function which takes two FlxSprites and calculates the angle between them with 0 degrees being directly to the right.  This function takes three parameters.  The first two are the sprites to calculate the angle between and the last is a Boolen that determines if the result is in degrees or not (if not, you get it in radians). 

The second function I'm using is the FlxAngle.rotatePoint() function.  It takes 5 parameters.  The first two are the X and Y values of the point to rotate.  The third and fourth are the point to rotate around, also called the pivot.  The fifth is the angle that it should be rotated.  I know I can get the angle between the two sprites, so if I rotate a point facing to the right around 0,0 it will point at the second sprite.  Maybe a crudely mouse drawn diagram will help?

 

With that quick code we are able to get Death to continually accelerate towards the player. 

Add comment


Security code
Refresh