I’ve not forgotten this thread that I started, just got distracted with My first published game. Anyways, now that that is over with, I thought that I should finish this off a little before going back to something iPwn related. So here we go…
Articles
Part 1 - Stuff to download, getting into the development
Part 2 - Setting the Scene, and Model Drawing
Part 3 - Animation
Part 4 - Loading Maps and Moving around
Downloads
Source Code
New Feature: Mac OSX support
Ok, so maybe not that new a feature.. considering that the guts of Mac OSX looks just like linux and has all the compiler tools etc etc.. I just tried compiling the linux version on my new MacBook and it turns out that it runs perfectly. I added a couple of #ifdef thingames and put in another makefile so that those Apple users among us can compile from a folder called MacOSX. Nothing really new in the code here though.
Next Step: Loading in a Premade Map
If anyone out there knows me, they would know that I am pretty lazy when it comes down to it, and here’s some proof. I’ve decided I don’t want to be bothered with writing anything to write new maps in the game.. I’m sure it’s quite easy to add in functions that will allow me to place tiles from within the game, but I simply dont want to at this point in time. So we are stuck with our good friend “notepad.exe”.
The file format will be a simple one that simply defines the size of the map, the starting position, and what kind of blocks are where. A 0 will be used for walkable areas, and a 1 for non-walkable areas. This can easily be extended to allow for UP TO 9 DIFFERENT BLOCK TYPES!!!!.. anyhows, the file will look something like this in the text editor:
50 50
25 25
1111111111111111111111111111111111111111111111111110000000000000000000000000000
000000000000000000001
… and so on….
So here’s some code to load the map from a text file.. please no complaints about fscanf(). I know it’s dodgy, but then so is using a text file.
void MapNode::Load(const char *fileName){
FILE *fp = NULL;
long nWidth, nHeight;
long x,y;
int i,j;
unsigned char ch;
// open the file
fp = fopen(fileName, "rt");
// Load the size info
fscanf(fp, "%d %d\n", &nWidth, &nHeight);
fscanf(fp, "%d %d\n", &x, &y);
// Set up the array
m_StartX = x; m_StartY = y;
m_Width = nWidth;
m_Height = nHeight;
m_Indexes = new int*[nHeight];
for(i = 0; i < nHeight; i++){
m_Indexes[i] = new int[nWidth];
}
// load the map data
for(i = 0; i < nWidth; i++){
for(j = 0; j < nHeight; j++){
ch = fgetc(fp);
m_Indexes[j][i] = (ch == '0') ? 0 : 1;
}
}
// close the file
fclose(fp);
}
And Then: Running Around
Now that we have a funny looking map, we can start adding in some stuff to interact with. For now I’ve booted the zombie out of the game so that we can concentrate on getting our dwarf running around freely. It was quite easy to do really, just a little code here (in the main loop):
case SDL_MOUSEBUTTONDOWN: m_PickingNode->HandleClick((event.button.button == SDL_BUTTON_LEFT) ? 1 : 2); break;
and a little code there (in the picker object):
void PickingNode::SetPlayerModel(ISceneNode *pModel){
m_PlayerModel = pModel;
}
void PickingNode::HandleClick(unsigned int nButton){
if(abs(m_WorldY) <= 0.5){
if(m_PlayerModel) ((ModelNode *)m_PlayerModel)->MoveToPosition(
(float)m_WorldX, (float)m_WorldY, (float)m_WorldZ
);
}
}
and finally, some code in the ModelNode.cpp, to make the magic happen:
void ModelNode::MoveToPosition(float x, float y, float z){
float x1,y1,z1;
float x2,y2,z2;
float vx,vy,vz;
float dist;
float dotprod;
m_TargetPosition[0] = x;
m_TargetPosition[1] = y;
m_TargetPosition[2] = z;
x1 = m_Position[0]; y1 = m_Position[1]; z1 = m_Position[2];
x2 = x; y2 = y; z2 = z;
dist = sqrt((x2 - x1)*(x2 - x1) + (y2 - y1)*(y2 - y1) + (z2 - z1)*(z2 - z1));
vx = (x2 - x1) / dist;
vy = (y2 - y1) / dist;
vz = (z2 - z1) / dist;
m_Rotation = (float)(acos(vz) * 180.0 / M_PI);
if(vx < 0) m_Rotation = -m_Rotation;
m_Rotation += 180.0f;
SetCurrentAnimation(MODEL_ANIMATION_WALKING);
}
plus a bit more in the model’s update routine
// do the movement stuff
float x1,y1,z1;
float x2,y2,z2;
float dist, vx, vy, vz;
x1 = m_Position[0];
y1 = m_Position[1];
z1 = m_Position[2];
x2 = m_TargetPosition[0];
y2 = m_TargetPosition[1];
z2 = m_TargetPosition[2];
if(x1 != x2 || y1 != y2 || z1 != z2){
// normalize the vector
dist = sqrt((x2 - x1)*(x2 - x1) + (y2 - y1)*(y2 - y1) + (z2 - z1)*(z2 - z1));
vx = (x2 - x1) / dist;
vy = (y2 - y1) / dist;
vz = (z2 - z1) / dist;
if(dist < m_MovementSpeed * fTime){
m_Position[0] = x2;
m_Position[1] = y2;
m_Position[2] = z2;
SetCurrentAnimation(MODEL_ANIMATION_IDLE);
} else {
m_Position[0] += vx * fTime * m_MovementSpeed;
m_Position[1] += vy * fTime * m_MovementSpeed;
m_Position[2] += vz * fTime * m_MovementSpeed;
}
}



You may have noticed that it’s been a little quiet around here of late. The main reason for this is that I’ve gone and jumped on the bandwagon and got myself an iPhone. Then, to be able to write stuff for it, I went out and got a MacBook. Then, I found out that it needed to use a slightly different programming language (objective-c). So, after learning the required language, paying the ability to write software, and spending so much time.. I kind of felt the need to make good on my expense and write something to publish. So, I went and wrote a small game called Rocket Attack, which is now finished and up to be added to the iPhone AppStore 