coding cocoa and quite clueless

thomcaster

Registered
Im new to cocoa and obj-C and am generally a relatively inexperienced programmer. I use Xcode and am not a total numbskull about programming. I'd like to know how to set up a 'map', if you will, where the map is a grid of coordinate points. Each coordinate pair is a piece of 'land' with its own set of data like what buildings are on it, etc. Thanks!
 
How will you be using this map? Will you be calling on those grid coordinates programmatically, like move grid 1,2 to 1,3? Or will you just need to store x,y = building?
 
I'm not sure I really understand your question. What exactly do you want to do. You can just make a matrix and each element has your info in it. Are you wondering how you would then go about displaying it visually? You could simply create a 'NSView' and set up 'NSRect's of what ever size you want to represent each element on that view. Can you be more specific on what you are trying to accomplish?
 
My thought was that he was looking to create array, perhaps a 3 dimensional array x,y,z, where z can hold the various buildings in x,y. 0,0,0='firehouse' 0,0,1='police station' etc.... I hope he replies.
 
Since I guess I was unclear I will elaborate; I wanted to make a map which the computer can use to find the distances between locations, and move stuff between them. Each square might have buildings or resources or other natural 'traits'.

For example, an army of 1000 soldiers moving from 1,1 to 5,3 will take 1 hour to get there. On the square, there is a fortress and a natural supply of wood. Thats pretty much it. I thought to use a 2-dimensional array of a class called Land, with the class containing what is on it. A controller could use the distance formula to calculate the distance between the points. I think this will work but I don't know how to do this.
 
So you're making some type of game then it sounds like. Generally what you do is, have objects that contain all the information you need about that thing, ie. soldier contains it's position.

You will have the display managed separately, displaying all the characters or whatever you want. There is no way for the computer to automatically tell the distances between objects on the screen. This is something you would program into the game.

You will basically have to setup your grid yourself, setting how far on the screen represents what distance, etc.

There's no real grid that I know of. It'll basically be in your code that you analize the position of the object on the screen through it's data and from there do whatever actions necessary.
 
The grid class would be the simplest object to create, but it would be very dynamic as you may have objects move in and out of as well as static objects that never move but may be captured, destroyed, etc...
The more complicated part begins with listing all the various objects that you will need to interact wth the grid, that'll be your soldiers, horses, jeeps, trucks, buildings, etc... As for movement that would be a seperate class that would take into consideration what object it's moving to adjust the speed accordingly. When I get a chance I'll see if I can toss up some quick skeleton code to see how it compares to what you have and what progress you've already made. Stay in touch.
 
telling the computer how to find the distance between two points will not be very difficult. The distance formula (the square root of (X1-X2)+(Y1-Y2)) will find the distance between any two points on the grid. However, since I am such a newb to Xcode, cocoa, and obj-C this is rather a daunting task. I am thinking of using a controller which takes the two points' locations and does the distance formula on them. This means it is using four variables. Actually creating the grid is another obstacle, even though someone in a previous post said it was easy. I do declare the array in code, right, or is there some NSArrayController thing to make my life easier?
 
Basically just create a land class that holds all your possible information ie. num troops, building1, etc... you could even make an array for this.

Then you want a matrix to hold this land class. There is no need to use a cocoa matrix or anything like that so just create a matrix:

id grid[gridwidth][gridlenght];

id is an objective C object pointer of anytype. This means I could put anytype of class as the elements for my grid. Then you just create your inital land set ups and put them in the grid spots you want. For instance:

Land *tempLand = [[tempLand alloc] init];
grid[0][0] = tempLand;

When ever you need to calculate the distance between to land point just do the distance formula on the grid locations.

When you are displaying the grid here is basically what you would put in your drawRect method for you custom view:

NSRect curRect;
NSSize curSize;
NSPoint curPoint;
int i, j;

curSize.width = widthForLandBlock;
curSize.height = heightForLandBlock;

curRect.size = curSize;


//rows
for(i = 0; i < gridwidth; i++)
{

//cols
for(j = 0; j < gridlength; j++)
{

curPoint.x = (j * widthForLandBlock);
curPoint.y = (i * heightForLandBlock);

curRect.origin = curPoint;


//insert the code for how you draw a piece of land
//such as: [grid[j] drawMe];
//if you just wanted to draw the rectangle border do the below

[[NSColor blackColor] set];
[NSBezierPath strokeRect:curRect];


}

}


I'm not sure if this is exactly what you were looking for but I hope it helps.
 
YAY thanks! Thats almost exactly the kind of thing I want! There are other features and stuff that I think I can handle on my own like clicking on the land to bring up a window that lets you view or change options about it and do stuff. That shouldn't be that hard. But I do have one question that needs answering: I would love a toolbar with buttons in it that open different drawers. I am having a great deal of difficulty making toolbars because I am not totally sure how NSToolbars work, since you can't create them in IB you have to do it in code and I have no resources to tell me how. Thanks! You've all been incredibly helpful so far!
 
Back
Top