Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

Dark GDK / Working on a Tower Defense

Author
Message
ErDa
16
Years of Service
User Offline
Joined: 17th Feb 2008
Location:
Posted: 7th Oct 2008 02:50
I'm working on a very small Tower Defense game for learning purposes. I've got most of the logic figured out, like creating a tilemap, moving units along the path etc. However I'm stuck with the "individual stats" of each unit, such as max hit points, hit points remaining and speed(I.E. tower that slows units when they get shot). Do I have to use an array with all the stats stored, and loop through it to check for deaths and what not? I'm sure there's an easier way. Something OOP-style-ish?
AndrewT
17
Years of Service
User Offline
Joined: 11th Feb 2007
Location: MI, USA
Posted: 7th Oct 2008 03:00
Is something like this what you're looking for?



Then you can access stats like so:



ErDa
16
Years of Service
User Offline
Joined: 17th Feb 2008
Location:
Posted: 7th Oct 2008 03:07 Edited at: 7th Oct 2008 03:59
Thanks for the quick reply!

This looks like exactly what I need, gonna read up on classes now!

Edit: Also, classes give me a wicked headache

Edit2: Okay, 2 more questions.

1. Would it be reasonable to have the units position inside the class? My original plan was just to spawn units at-
X1 = X-1*distance
X2 = X-2*distance
...etc.

Are there any other solutions?

2. About classes, if I want to have, say, 5 different kinds of units (some faster, some slower, etc.), would I need 5 different classes, then "spawning" however many individual units of each kind I would want?
AndrewT
17
Years of Service
User Offline
Joined: 11th Feb 2007
Location: MI, USA
Posted: 7th Oct 2008 03:59 Edited at: 7th Oct 2008 04:02
Ya, I used to get confused about classes too. But you have to learn them sometime, right? If you have any questions about the code I posted feel free to ask.

Edit::

1. If by that you mean could you store a units position in the class then yes

2. Nope. You just need multiple instances of the one class and you can change the speed of each individually.

ErDa
16
Years of Service
User Offline
Joined: 17th Feb 2008
Location:
Posted: 7th Oct 2008 22:44
Okay, this code:



Is giving me 3 error messages;



Any ideas? Obviously it won't accept my 'num' as a valid declaration, but I can't think of a work-around. Should I just declare a huge-ass array and only use however much of it I need to use?
AndrewT
17
Years of Service
User Offline
Joined: 11th Feb 2007
Location: MI, USA
Posted: 7th Oct 2008 22:50 Edited at: 7th Oct 2008 22:55
Hmmm, the code that I posted earlier works fine. I'll try recreating your error...

EDIT::

K, tested it and I get the same errors you got. I'll see if I can find the problem.

EDIT 2::

Have you tried vectors yet?? They're a very useful dynamic array class that could come in handy. You could try doing that.

http://www.cplusplus.com/reference/stl/vector/

That's a good place to go if you don't know how to use vectors.

ErDa
16
Years of Service
User Offline
Joined: 17th Feb 2008
Location:
Posted: 7th Oct 2008 22:55
Posting all the class-related stuff in my code, in chase it's relevant. Oh, and thanks for another very quick reply!

AndrewT
17
Years of Service
User Offline
Joined: 11th Feb 2007
Location: MI, USA
Posted: 7th Oct 2008 22:59 Edited at: 7th Oct 2008 23:00
Ok, well after searching I've found I don't think it's possible to declare variable-sized arrays. You're better off going with dynamic arrays or vectors.

ErDa
16
Years of Service
User Offline
Joined: 17th Feb 2008
Location:
Posted: 7th Oct 2008 23:12
Thanks alot for your time.

Thinking about it, it's pretty obvious that it's impossible to declare a variable-sized array. However using vectors or dynamic arrays seems awefully complicated. I'm pretty sure I'll never go above, say, 100 units. Wouldn't it be okay, in a chase like this, where I control the amount of instances that I want, to just declare Units[100]? I understand that if a user's input is is going to be stored in the array, this could lead to overflow or whatever you call it.
AndrewT
17
Years of Service
User Offline
Joined: 11th Feb 2007
Location: MI, USA
Posted: 7th Oct 2008 23:53
Quote: "Wouldn't it be okay, in a chase like this, where I control the amount of instances that I want, to just declare Units[100]?"


Ya, that's fine if your dealing with 100 units, but you shouldn't get into a habit of that.

Quote: "Thanks alot for your time."


Glad I could help.

ErDa
16
Years of Service
User Offline
Joined: 17th Feb 2008
Location:
Posted: 8th Oct 2008 01:44
I unfortunately have to bother someone again.

This:



Returns some weird errors:



And I've no idea how to decypher that.

This is the class-part of my code:



Also, am I way off?
Mahoney
16
Years of Service
User Offline
Joined: 14th Apr 2008
Location: The Interwebs
Posted: 8th Oct 2008 02:03
I believe it would be



Windows Vista Home Premium Intel Pentium Dual-Core 1.6 Ghz 1GB DDR2 RAM GeForce 8600GT Twin Turbo
ErDa
16
Years of Service
User Offline
Joined: 17th Feb 2008
Location:
Posted: 8th Oct 2008 02:13


Doesn't seem to work.
Thanks for your time though!
Lilith
16
Years of Service
User Offline
Joined: 12th Feb 2008
Location: Dallas, TX
Posted: 8th Oct 2008 03:54
I don't see cUnit::GetPositionY()in your code. Ditto for X and Speed.

Lilith, Night Butterfly
I'm not a programmer but I play one in the office
AndrewT
17
Years of Service
User Offline
Joined: 11th Feb 2007
Location: MI, USA
Posted: 8th Oct 2008 04:08 Edited at: 8th Oct 2008 04:09
ErDa:

Lilith is right, I gave you the function definitions but you have to actually write the function.

Instead of:



you should have



Sorry if I wasn't clear about that. You'll have to write all the functions to actually do something. If ya need help with that let me know.

ErDa
16
Years of Service
User Offline
Joined: 17th Feb 2008
Location:
Posted: 8th Oct 2008 04:52 Edited at: 8th Oct 2008 05:39
I knew that seemed weird! Jeez, it's so bleedingly obvious too!

Edit: I just spent ~20 minutes trying to figure out what was wrong, until I realized that "i>=ii" was supposed to be "i<=ii" ...

........................

.
AndrewT
17
Years of Service
User Offline
Joined: 11th Feb 2007
Location: MI, USA
Posted: 8th Oct 2008 13:00
Quote: "Edit: I just spent ~20 minutes trying to figure out what was wrong, until I realized that "i>=ii" was supposed to be "i<=ii" ..."


Get used to it, it'll happen a lot. I'm really bad at catching little mistakes like that, so I just wasted almost an hour solving a problem that was fixed by removing one line of code.

Hayer
18
Years of Service
User Offline
Joined: 4th Nov 2005
Location: Norway
Posted: 9th Oct 2008 01:13
Ye, but its the small mistake like that which makes us programmers

ErDa
16
Years of Service
User Offline
Joined: 17th Feb 2008
Location:
Posted: 14th Oct 2008 05:20
Okay, time for round two. dbSpriteCollision seems to not be working at all. I need some other way to check which sprite I'm clicking. My initial plan was to draw a sprite at the cursor position, then check for collision with the menu sprites and so on.

Should I write my own sprite collision function? I'm pretty sure it's just a couple of loops like

(very-pseudo-code)


I was also thinking about maybe using bitmaps, and dbPoint(mousex, mousey), but coloring the bitmaps correctly will be pretty hard.

Any other ideas? Oh and it's 2d eagle's eye for clarification.
Zuka
16
Years of Service
User Offline
Joined: 21st Apr 2008
Location: They locked me in the insane asylum.
Posted: 14th Oct 2008 05:41
It's great to see someone devoted to and willing to make a game. And even better when they're ready to take the time to learn how.
AndrewT
17
Years of Service
User Offline
Joined: 11th Feb 2007
Location: MI, USA
Posted: 14th Oct 2008 13:33
I don't have DarkGDK on this computer but I'll see what I can think of off the top of my head...



I'm not sure that function will work, so if you have any probs let me know.

Zuka
16
Years of Service
User Offline
Joined: 21st Apr 2008
Location: They locked me in the insane asylum.
Posted: 15th Oct 2008 00:05
Spongebob Buttpants!

Uh, sorry about that.

My code


No rotation, but it does have collision. And you can check if the mouse is over it easily,

Quote: "
if (dbMouseClick())
{
myrect.PointInside(dbMouseX(), dbMouseY());
}
"
ErDa
16
Years of Service
User Offline
Joined: 17th Feb 2008
Location:
Posted: 19th Oct 2008 22:28 Edited at: 20th Oct 2008 00:58
Wrote this



Based on AndrewT's code, and it works great. Thanks alot guys!
Getting closer, step by step

Edit: Here we go again.
I need to somehow check if no mouse button is pressed. Like so:



This is to not allow the player to drag the mouse everywhere while keeping the mouse button down, but having to click each action separately.

But (dbMouseClick() == 0) and (!dbMouseClick()) doesn't work. I'm all out of ideas!

EDIT2: Ooops, solved it using



.... Heh.
ErDa
16
Years of Service
User Offline
Joined: 17th Feb 2008
Location:
Posted: 20th Oct 2008 23:32
Round 3, FIGHT!

The algorithm to check distance between unit and tower, and fire on the unit if it's in range based on a timer. The firing and timer is pretty straight forward, but the distance check needs to be ran ever main loop. The only ideas I have would cause major lag (i.e. checking the x and y differences between each tower and each unit, then using hypothenus to get the distance. It's just way too many checks to run every loop, right?)
elantzb
16
Years of Service
User Offline
Joined: 10th May 2008
Location: Classified
Posted: 21st Oct 2008 00:14
I'm very curious to see the code related to the definition and display of your tilemaps

~you can call me lantz~
ErDa
16
Years of Service
User Offline
Joined: 17th Feb 2008
Location:
Posted: 21st Oct 2008 17:04 Edited at: 21st Oct 2008 17:07
Ofcourse!

This is the array that stores which tiles go where, also stores what kind of tower is where, too.


This is the function that draws the map, it's a pretty simple loop that draws a sprite depending on what number is in that spot in the above array. The variable SZ is the pixel size of the image, in this chase 48x48. This draws the map sideways, but I thought it looked better that way, so yeah. Just switch the count and count2 around to fix it.


This checks which sprite is clicked, by looping through spriteID's 1-100 (10x10 tilemap), and checking if the cursor is inside the range of the image.


This checks if the user clicked a (the only one for now) tower from the menu, and lets the user drag it around if he did.



This draws a tower if the user clicks an avaliable tile, while dragging a tower.


Sorry for the lack of //comments!

Edit: Oh and I also made this very nifty little function:

That will make an image out of text, so that you can display the text above other sprites which you can't do with dbPrint or dbText(I think)!
AndrewT
17
Years of Service
User Offline
Joined: 11th Feb 2007
Location: MI, USA
Posted: 21st Oct 2008 22:17
Lookin' good! However, on your 'maketextimage()' function, you have parameters for 'len' and 'hig'; are those to specify the width and height of the image produced?? If so, then you can get rid of those and use 'dbTextWidth()' and 'dbTextHeight()' to see how big in pixels the specified text is.

Good luck with your project.

<<---------CLICK IT
ErDa
16
Years of Service
User Offline
Joined: 17th Feb 2008
Location:
Posted: 21st Oct 2008 23:37
It is indeed for the width and height, and that's a great idea!
Just need to figure out how to do the check to see if a unit is in range of a tower, then I'm more or less done with the base engine. Ugh, it's hard too!
Zuka
16
Years of Service
User Offline
Joined: 21st Apr 2008
Location: They locked me in the insane asylum.
Posted: 22nd Oct 2008 00:32
If you're only doing comparisons and not display for the distances, you don't need to sqrt the hypotenuse.
Diggsey
18
Years of Service
User Offline
Joined: 24th Apr 2006
Location: On this web page.
Posted: 22nd Oct 2008 02:30
As long as you don't use dbSqrt, sqrt will still easily be fast enough for your needs

[b]Yuor signutare was aresed by a deslyxic mud...
BOX2D V2 HAS HELP FILES! AND A WIKI!
AndrewT
17
Years of Service
User Offline
Joined: 11th Feb 2007
Location: MI, USA
Posted: 22nd Oct 2008 03:03 Edited at: 22nd Oct 2008 03:05
While Diggsey's right that it shouldn't slow your game much, I agree with Zuka that you're better off comparing squared distances:



That should work, but I don't have time to test it so you might have to fix some stuff.

You can compare distances like so:



<<---------CLICK IT
Zuka
16
Years of Service
User Offline
Joined: 21st Apr 2008
Location: They locked me in the insane asylum.
Posted: 22nd Oct 2008 23:14
Um, AndrewT, wouldn't it make more sense to do this instead?

AndrewT
17
Years of Service
User Offline
Joined: 11th Feb 2007
Location: MI, USA
Posted: 23rd Oct 2008 13:33
Ya, but I like to make my code more spread out and uncompressed so it's easier for me to read.

<<---------CLICK IT
Zuka
16
Years of Service
User Offline
Joined: 21st Apr 2008
Location: They locked me in the insane asylum.
Posted: 23rd Oct 2008 14:39
Could be worse.

AndrewT
17
Years of Service
User Offline
Joined: 11th Feb 2007
Location: MI, USA
Posted: 23rd Oct 2008 21:38
Ya.

<<---------CLICK IT

Login to post a reply

Server time is: 2024-09-30 09:23:27
Your offset time is: 2024-09-30 09:23:27