enemy help
Im working on a text based rpg for my first game and was wondering how I could make it so enemies move around the map
I made a public class like this
class world
{
char name[20];
char description[200];
//////////test if direction is accesible///
int n;
int e;
int s;
int w;
};
world rooms;
and I want to put enemies in them and npc''s that can move room to room can anyone help?
You''d probably want to start by making an Enemy class. They could have a position in the world, and the class could contain methods for making them move with each turn/game loop. If you want to make your enemies move intelligently, you''d have to look into some sort of AI.
good luck!
- Daniel
my homepage
good luck!
- Daniel
my homepage
- DanielMy homepage
Hmm, some thoughts:
Call your class for a room, Room. Or Location A world contains several places, so your class name is misleading. It''ll just come back to haunt you in the end
Use an enum for your directions:
enum Directions { NORTH, EAST, SOUTH, WEST, NUM_DIRS };
Store an array of directions rather than individual exits, like so:
int exit[NUM_DIRS];
Now, to make a creature wander, you can do this:
int direction_to_travel = rand() % NUM_DIRS;
You need to have some way to find out which rooms link to which. The simplest way is to uniquely number your rooms, and have the exit array contain the number of the target rooms. Example: if this room''s exit[NORTH] = 20, going north takes you to room 20.
Using the above code, you''d do:
int target_room = exit[direction_to_travel];
theCreature.MoveTo(target_room);
You will probably need a linked list of all the creatures in each room. This will probably be a list of pointers as you probably need a global list of creatures elsewhere. An alternative is to ''simulate'' a global list by using an iterator that goes through each room for you. Either way, you should probably have a routine to take a creature out of one room and place them in another. If you''re happy with inheritance etc., you might want a base class called Entity, store a list of Entitys in each room, and derive Creature and Object from Entity. That way, you can use 1 set of functions for moving Creatures and Objects around, rather than writing it all twice (like I did! *sigh*)
Hope that''s given you some ideas.
Call your class for a room, Room. Or Location A world contains several places, so your class name is misleading. It''ll just come back to haunt you in the end
Use an enum for your directions:
enum Directions { NORTH, EAST, SOUTH, WEST, NUM_DIRS };
Store an array of directions rather than individual exits, like so:
int exit[NUM_DIRS];
Now, to make a creature wander, you can do this:
int direction_to_travel = rand() % NUM_DIRS;
You need to have some way to find out which rooms link to which. The simplest way is to uniquely number your rooms, and have the exit array contain the number of the target rooms. Example: if this room''s exit[NORTH] = 20, going north takes you to room 20.
Using the above code, you''d do:
int target_room = exit[direction_to_travel];
theCreature.MoveTo(target_room);
You will probably need a linked list of all the creatures in each room. This will probably be a list of pointers as you probably need a global list of creatures elsewhere. An alternative is to ''simulate'' a global list by using an iterator that goes through each room for you. Either way, you should probably have a routine to take a creature out of one room and place them in another. If you''re happy with inheritance etc., you might want a base class called Entity, store a list of Entitys in each room, and derive Creature and Object from Entity. That way, you can use 1 set of functions for moving Creatures and Objects around, rather than writing it all twice (like I did! *sigh*)
Hope that''s given you some ideas.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement
Recommended Tutorials
Advertisement