Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 Advice on creating a 2D tile based game involving object orientation!!?
Index -> Programming, Turing -> Turing Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
GreatPumpkin




PostPosted: Tue Mar 11, 2014 7:32 pm   Post subject: Advice on creating a 2D tile based game involving object orientation!!?

What is it you are trying to achieve?
I've tried and re-tried and re-tried at making a simple 2d engine and I keep hitting major roadblocks.. Pretty much, what I'm going for is something like codemonkey2000's 2d tile system, except instead of having the player control a single unit which is navigated around the map, I'd like multiple units which are navigated around the map AND around each other. So they not only have hit detection against the map, but also agains eachother. I've tried and thought up a couple main ways:

Have separate 2D arrays for each separate object (the units, the tiles) in there own class's and try to make them interact with eachother

or

Have a class that holds the coordinate information of the units, and a class that holds the 2D array of int's representing tiles. There will be a third class that takes the coordinate information from the units class, the array from the tiles class, then recompiles them into a 2D array, of a custom type, this type will be tileType, which contains two peices of information, if theres a unit on that coordinate, and what type of tile is at that coordinate. After this I'd make the interactions happen within the third class.


I guess what I'm really asking for is advice on how to design a 2d tile based object oriented game engine properly. I'm committed to completing this but I'm really really really lost.
Sponsor
Sponsor
Sponsor
sponsor
nullptr




PostPosted: Tue Mar 11, 2014 11:52 pm   Post subject: Re: Advice on creating a 2D tile based game involving object orientation!!?

Your first option sounds way easier to work with -- if you want to know whether a tile is occupied, you can just write a helper function instead of making a whole new array. It could look something like this:

code:

bool isOccupied(int x, int y)
{
     return units[x][y] != NULL || tiles[x][y] != EMPTY;
}


You'd have to adapt that for the language you're using and the array organization you choose, but that's the gist of it. Since you describe your game as using 2D tiles, I assume that it's turn-based, rather than real-time? Turn-based pathfinding is pretty easy -- real-time pathfinding is not. Since you sound a little overwhelmed, have you thought about implementing a simpler type of 2D game, such as chess, before moving on to a project like this?
GreatPumpkin




PostPosted: Fri Mar 14, 2014 10:54 pm   Post subject: Re: Advice on creating a 2D tile based game involving object orientation!!?

nullptr @ Tue Mar 11, 2014 11:52 pm wrote:
have you thought about implementing a simpler type of 2D game, such as chess, before moving on to a project like this?[/size]


Probably the best advice I've received yet. I was thinking of making something similar but alot less complex.
I don't know if I have the idea of OOP down yet. If each unit is it's own object, shouldn't it have ways to detect collision with other units without relying on a universal map of all units ?

Are 2d tile based games even supposed to be used in junction with OOP, or is there some other kind of OOP method that looks like it's tile based ?

I'm obviously going to have to do some more research on OOP in turing, but where would I look for information on designing a 2d tile based game engine ?
Raknarg




PostPosted: Fri Mar 14, 2014 11:58 pm   Post subject: RE:Advice on creating a 2D tile based game involving object orientation!!?

Generally an object would not make decisions according to the outside world. An object would control the world it's in and decide whether or not is has collided. An object contains characteristics and behaviours of a function, so something will tell what behaviour to use when certain situations occur. For example, with collision, you might have your program which holds a bunch of objects, and then decide whether or not any of them have collided. If they have, the program tells these objects to change direction. The objects wouldn't decide for themselves in this case.

Of course this is a general idea, there are all sorts of implementations, it mostly depends on what you're trying to achieve.
GreatPumpkin




PostPosted: Sun Mar 16, 2014 7:45 pm   Post subject: RE:Advice on creating a 2D tile based game involving object orientation!!?

So the 2d array map which holds the location of all the units that is mutually accessible by all of them would be acceptable ?
Raknarg




PostPosted: Sun Mar 16, 2014 8:12 pm   Post subject: RE:Advice on creating a 2D tile based game involving object orientation!!?

Acceptable i all relative. Try to achieve your goal in the simplest way possible. If you can map out an organized method of doing it that way, then by all means go ahead. Most of the time in OOP information is meant to flow upward (objects are controlled by objects in a higher place in the hierarchy). That doesn't mean it has to.
GreatPumpkin




PostPosted: Sun Mar 16, 2014 10:05 pm   Post subject: Re: Advice on creating a 2D tile based game involving object orientation!!?

I'm working on a map for my game, and I'd like to redo it so that each tile on screen will be it's own object, as with each unit, instead of an object that is an array of units or an object that it an array of tiles. Is it possible to make a 2d array of pointers?

I've created an array of pointers before, but I'm having issues setting up a 2d array, here is that code I have written up:



Turing:

View.Set ("graphics:800;600,offscreenonly")


class tileClass

    export draw, add

    var tileType : int := 0
    var x, y : int := 0

    proc add (X, Y, TT : int)
        var x := X
        var y := Y
        var tileType := TT
    end add

    proc draw
        if tileType = -1 then         %borders
            Draw.FillBox (x * 10, y * 10, x * 10 + 10, y * 10 + 10, black)
        elsif tileType = 0 then         %water
            Draw.FillBox (x * 10, y * 10, x * 10 + 10, y * 10 + 10, blue)
        elsif tileType = 1 then         %sand
            Draw.FillBox (x * 10, y * 10, x * 10 + 10, y * 10 + 10, yellow)         % the +10 is there because each tile is 10 by 10
        elsif tileType = 2 then         %lowlands
            Draw.FillBox (x * 10, y * 10, x * 10 + 10, y * 10 + 10, 120)
        elsif tileType = 3 then                    %highlands
            Draw.FillBox (x * 10, y * 10, x * 10 + 10, y * 10 + 10, green)
        elsif tileType = 4 then          %mountains
            Draw.FillBox (x * 10, y * 10, x * 10 + 10, y * 10 + 10, grey)
        end if
    end draw

end tileClass




var mapTiles : array 0 .. 59, 00 .. 59 of ^tileClass
for x : 0 .. 59
    for y : 0 .. 59
        new tileClass, mapTiles (x, y)
    end for
end for

for x : 0 .. 59
    for y : 0 .. 59
        mapTiles (x, y) -> add (x, y, 1)


        mapTiles (x, y) -> draw

    end for
end for







I believe it should create 60 * 60 = 3600 elements which are all pointers to tileClass.
Then it should go through those 60 * 60 pointers to tileClass and run the add proc, which should enter in the x and y values as 0 - 59 depending on the for loop it's on and the tileType as 1 (sand/yellow), then draw each one out.

What it does is draw a single tile at 0, 0 that's tileType 0 (water/blue), and quits. If I change the default value's in the tileClass of the x, y and tileType then the output corresponds to those values.
It's as if "mapTiles (x, y) -> add (x, y, 1)" is not ran in the second set of for loops, also it appears to only create a single element, I could be wrong however as if there were more than one it could be assumed they are all drawing on the same 0, 0. Any help?
Raknarg




PostPosted: Sun Mar 16, 2014 11:02 pm   Post subject: RE:Advice on creating a 2D tile based game involving object orientation!!?

proc add (X, Y, TT : int)
x := X
y := Y
tileType := TT
end add

If you put "var" in front of these, it will create new instances of the variables x, y and tileType and override that name for the use of your class. Instead of changing your classes variable, you change a new variable you made called "x". Look at the difference.

One suggestion I have for you just in general is to differentiate your classes from your variables. You don'thave to, but having different cases for different data types is useful. here's some examples:

someVariable
SomeClass
some_function
CONSTANT_VALUE

Helps with your code's readability
Sponsor
Sponsor
Sponsor
sponsor
GreatPumpkin




PostPosted: Tue Mar 18, 2014 6:28 pm   Post subject: RE:Advice on creating a 2D tile based game involving object orientation!!?

Thanks Raknarg, looks like I missed that one.
Display posts from previous:   
   Index -> Programming, Turing -> Turing Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 9 Posts ]
Jump to:   


Style:  
Search: