Took a bit of time off work to work on an update to the Rocket Attack game that I made for the iPhone. Here are a few screenshots of the newer version.


Took a bit of time off work to work on an update to the Rocket Attack game that I made for the iPhone. Here are a few screenshots of the newer version.


So, I’ve been learning some Objective-C to do my iPhone programming, and am gradually getting into the more interesting parts of it. Just now I’ve been playing with protocols to define some basic structures for my next iPhone game.
I guess you could say that a protocol is very similar to an interface class in c++, excepting that you can have optional methods as well as the required methods, allowing your classes to implement only the optional methods that you wish to use.
Here’s an example of a protocol definition
@protocol GameScreen@required -(id) init; -(void) draw; -(void) update:(float)time; @optional -(void) tapeventDragBegin:(CGPoint)origin; -(void) tapeventDrag:(CGPoint)origin currentPosition:(CGPoint)current; @end
Using the @required keyword indicates the methods that will be required for any class to define, while methods after the @optional keyword will not need to be implemented.
You can implement the protocol in a class by using the following code
@interface MainScreen : NSObject{ } // required methods -(id) init; -(void) draw; -(void) update:(float)time; // optional methods -(void) tapeventDrag:(CGPoint)origin currentPosition:(CGPoint)current; @end
There is one other thing that you will need to consider when coding to handle optional methods, and that is the problem of whether the method has actually been implemented or not. Fortunately, you can use the respondsToSelector method to check if it’s been implemented prior to calling it.
if([myScreen respondsToSelector:@selector(tapeventDrag:currentPosition:)]){
[myScreen tapeventDrag:origin currentPosition:current];
}
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;
}
}