Sign in to follow this  
Analizer

making a bitmap follow a character

Recommended Posts

Hi.
 
I have created a function for an enemy and I'm trying to make the bitmap follow my character and when they interact the character dies and the game ends.
 
Can someone give me heads up?
 
Here is the class code:
 
#ifndef _ENEMY_H_
#define _ENEMY_H_

#include <allegro.h>

class Enemy
{
public:
int x;
int y;
BITMAP *enemy;
Enemy();
~Enemy();
void blitenemy(BITMAP*buffer);
};

#endif

 

 

 
and here is the cpp code:
 
#include <allegro.h>
#include "Enemy.h"

Enemy::Enemy()
{
srand(time(NULL));
x = rand() % 400 + 1;
y = rand() % 300 + 1;
enemy = load_bitmap("enemy.bmp",NULL); 
}

Enemy::~Enemy()
{
destroy_bitmap(enemy);
}

void Enemy::blitenemy(BITMAP*buffer)
{
masked_blit(enemy,buffer,0,0,x,y,800,600);
}

 

 

Share this post


Link to post
Share on other sites

Didn't really got the question.

 

You want the enemy to get to the character? If that is the case, on an open world (in other words, a scenario with no obstacles) all you have to do is update the enemy position by this formula:

 

    angle = atan2f(playerY - monsterY, playerX - monsterX);
    nextX = monsterX + (cos(angle) * moveSpeed * dt);
    nextY = monsterY + (sin(angle)  * moveSpeed * dt);
    setMonsterPosition(nextX, nextY);

 

moveSpeed is the monster speed and dt is the amount of time passed since the last frame (your library probably gives you that with some function).

 

 

If the world has obstacles, then you must search for pathfinding, particularly A*.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this