2D Tile Game Component Help
Author |
Message |
Beastinonyou
|
Posted: Sat Jun 11, 2011 1:23 pm Post subject: 2D Tile Game Component Help |
|
|
So I've been the Silent Wanderer across the forums here, browsing frequently daily, and never really had any questions.
With my ISP in mind, I've already got the basic layout of the mechanics down. I've got all my sprites, the Map (Tower Defense) and the Grid.
Now, The only thing I'm wondering how to approach, is to place the actual sprites down, and for later work, make the Enemy sprites follow the road outlined.
I used a Text Document for the map, using 1's as the border, 0's as place-able spots, and 2's as the road.
If someone could give me some pseudo code, or even an approach/ implementation idea. I do wish to learn how to do it, so you don't have to do it for me. Just give me a head-start, or some kind of approach I can use to do either of those components. I'm sure the following the road path with the 2's would be easy, but I'm just wondering about the place-able sprites because it is a Tower Defense.
I've attached my work so far on it, which is pretty basic.
Description: |
Comes with my Turing File, and my Sprites I threw up in Photoshop. |
|
Download |
Filename: |
Pac-Defense.rar |
Filesize: |
13.63 KB |
Downloaded: |
161 Time(s) |
|
|
|
|
|
|
Sponsor Sponsor
|
|
|
2F_Zelda
|
Posted: Sat Jun 11, 2011 1:34 pm Post subject: Re: 2D Tile Game Component Help |
|
|
You could use a click procedure that checks if the clicked spot is a valid place for a tower. If it is valid, you could draw the sprite at the clicked location.
|
|
|
|
|
|
Beastinonyou
|
Posted: Sat Jun 11, 2011 1:46 pm Post subject: Re: 2D Tile Game Component Help |
|
|
2F_Zelda @ Sat Jun 11, 2011 1:34 pm wrote: You could use a click procedure that checks if the clicked spot is a valid place for a tower. If it is valid, you could draw the sprite at the clicked location.
The only problem with that solution, is since it's in a Click procedure, When the user Clicks, it will draw the Sprite.. But immediately after they release, the click statement becomes false, therefore not drawing the sprite at the location. Since this is a Tower Defense game, the Towers (Sprites) need to be placed and stay there, till eventually removed or ended
Thanks for aiding me anyway.
Turing: |
procedure placeTower
if mouseclick = 1 then
for y : 0 .. 19
for x : 0 .. 19
if tiles (x, y) = 0 then
drawTower
end if
end for
end for
end if
end placeTower
|
That was what I came up with based on your solution.
|
|
|
|
|
|
Tony
|
Posted: Sat Jun 11, 2011 3:08 pm Post subject: RE:2D Tile Game Component Help |
|
|
Imagine that you already have a few towers placed (somehow... lets say a map starts with those towers). How would you represent that in code (what data-structures)? You'd need to figure that out before attempting to add more towers.
|
Tony's programming blog. DWITE - a programming contest. |
|
|
|
|
2F_Zelda
|
Posted: Sat Jun 11, 2011 4:00 pm Post subject: Re: 2D Tile Game Component Help |
|
|
Beastinonyou @ Sat Jun 11, 2011 1:46 pm wrote:
The only problem with that solution, is since it's in a Click procedure, When the user Clicks, it will draw the Sprite.. But immediately after they release, the click statement becomes false, therefore not drawing the sprite at the location.
The sprite shouldn't go away when the variable being checked by the draw condition changes unless you're telling it to.
But you should listen to Tony before me. He's much more experienced.
|
|
|
|
|
|
Beastinonyou
|
Posted: Sat Jun 11, 2011 7:13 pm Post subject: Re: RE:2D Tile Game Component Help |
|
|
Tony @ Sat Jun 11, 2011 3:08 pm wrote: Imagine that you already have a few towers placed (somehow... lets say a map starts with those towers). How would you represent that in code (what data-structures)? You'd need to figure that out before attempting to add more towers.
Alright.. After awhile of trial and error, I took a break, re-thought of how I could approach that, then thought.. Well, lets say the whole map starts out with towers, and find out how to Remove them by clicking in that square. I figured that out, and then just reversed it, so that when I clicked, it draws the sprite in that location, and it stays there.
Now, I'm going to work on getting an enemy to move across the road.
If somebody could shoot me some ideas of how to give the Towers their properties like Range, Damage, Speed, etc, How would I go about implementing that. At the moment, I've just figured out how to place the Sprite image.
An example would benefit me greatly, since I learn better with Examples or Instructions.. (pseudo Code possibly)
Description: |
Map with a Set Place-Able Sprite. Need Tower Properties |
|
Download |
Filename: |
Pac-Defense.rar |
Filesize: |
19.19 KB |
Downloaded: |
143 Time(s) |
|
|
|
|
|
|
Raknarg
|
Posted: Sat Jun 11, 2011 7:49 pm Post subject: RE:2D Tile Game Component Help |
|
|
I haven't tried this before, but my idea would be to use an array of records:
Turing: |
type towerData :
record
range, dmg, rateOfFire : int
end record
var towers : flexible array 1 .. 0 of towerData
|
Now lets say you wanted to call the 4th tower and use range, you would use: tower (4).range
So every time the user buys a tower, you resize the array. (If you wanted to make it easier on the computer, you could have a limited size array and a set amount of towers instead.)
Now if it was me, I'd have an initializing procedure to set the tower's statistics when the user placed it, too.
|
|
|
|
|
|
Beastinonyou
|
Posted: Sat Jun 11, 2011 10:26 pm Post subject: Re: RE:2D Tile Game Component Help |
|
|
Raknarg @ Sat Jun 11, 2011 7:49 pm wrote: I haven't tried this before, but my idea would be to use an array of records:
Turing: |
type towerData :
record
range, dmg, rateOfFire : int
end record
var towers : flexible array 1 .. 0 of towerData
|
Now lets say you wanted to call the 4th tower and use range, you would use: tower (4).range
So every time the user buys a tower, you resize the array. (If you wanted to make it easier on the computer, you could have a limited size array and a set amount of towers instead.)
Now if it was me, I'd have an initializing procedure to set the tower's statistics when the user placed it, too.
After looking into that, It seems like the best option..
On the other hand, Would anybody give some insight as to how you would go about moving enemies to specific waypoints / coordinates, or even just follow a path that would be outlined in my text document. Considering the road is composed of '2's, how would I go about getting them to follow that path?
Again, any help is appreciated.
|
|
|
|
|
|
Sponsor Sponsor
|
|
|
Tony
|
Posted: Sat Jun 11, 2011 11:02 pm Post subject: RE:2D Tile Game Component Help |
|
|
The easiest approach would be to have a series of line-of-sight waypoints that describe the path. Each enemy would have a sort of "goal direction", and they'd keep on moving in that direction until the next waypoint, which could tell them the next direction to go.
This works well for static "follow the road" maps.
|
Tony's programming blog. DWITE - a programming contest. |
|
|
|
|
Beastinonyou
|
Posted: Sun Jun 12, 2011 12:40 pm Post subject: Re: RE:2D Tile Game Component Help |
|
|
Tony @ Sat Jun 11, 2011 11:02 pm wrote: The easiest approach would be to have a series of line-of-sight waypoints that describe the path. Each enemy would have a sort of "goal direction", and they'd keep on moving in that direction until the next waypoint, which could tell them the next direction to go.
This works well for static "follow the road" maps.
That does seem like the easiest approach..
How would I go about making those waypoints / what would I have to use?
|
|
|
|
|
|
Tony
|
Posted: Sun Jun 12, 2011 1:21 pm Post subject: RE:2D Tile Game Component Help |
|
|
That is entirely up to you. You could encode all the information in the map file, in a way that you think would be easy to work with.
|
Tony's programming blog. DWITE - a programming contest. |
|
|
|
|
Beastinonyou
|
Posted: Sun Jun 12, 2011 3:31 pm Post subject: Re: 2D Tile Game Component Help |
|
|
Alright, I figured the easiest way to create the waypoints, were to create co-ordinates, and I did that for each corner where the direction would change. (Using an Array for X and Y)
Now that I have way-points, how would I go about Moving the Enemy towards one Way-point, then once they Reach that way-point, Move towards the next, etc. (Pseudo Code Appreciated)
|
|
|
|
|
|
Beastinonyou
|
Posted: Mon Jun 13, 2011 8:16 pm Post subject: Re: 2D Tile Game Component Help |
|
|
Ok ... I have waypoints for each of them, and I've been able to move an object (Solid Circle) throughout the entire map through the road.
Unfortunately, It's like 19 if statements.
Simplified, it's basically like this
Turing: |
var wayp : array 0 .. 19 of boolean
var start : boolean
var RX : array 0 .. 19 of int := init (waypoint1xCoord, waypoint2xCoord, etc )
var RY : array 0 .. 19 of int := init (waypoint1yCoord, waypoint2yCoord, etc )
start := false
procedure wayPoints
if enemyY + yChange <= RY (waypoint1y ) and start = false then
yChange + = 1 % (road goes upwards first)
if enemyY + yChange = RY (waypoint1y ) then
wayp (waypoint1 ) := true
start := true
end if
end if
if wayp (waypoint1 ) = true then
xChange - = 1 % (road then moves left from waypoint 2)
if enemyX + xChange = RX (waypoint2 ) then
wayp (waypoint1 ) := false % so the initial if statement becomes invalid
wayp (waypoint2 ) := true % sets up the next if statement for next waypoint
end if
end if
drawfilloval (RX (waypoint0 ) + xChange + 12, RY (waypoint0 ) + yChange + 12, 9, 9, yellow) % waypoint 0 is starting square, tiles are 25 x 25,
% ... to waypoint 19 (end)
end wayPoints
|
obviously I left some variables and such out, but thats basically what I've done.. and I have 19 if statements.. I Know there must be a shorter and more efficient way to do this.
Maybe using whatdotcolour to check to see if another tile of grey is above/below/beside, then adjust the enemy variable accordingly.
Also, how would I go about having multiple enemies do this, rather than just one / multiple enemies piled on-top of one another.
|
|
|
|
|
|
Tony
|
Posted: Mon Jun 13, 2011 8:32 pm Post subject: Re: 2D Tile Game Component Help |
|
|
Instead of placing the direction change into the code, why not load it from the map, then have something like
code: |
var waypoint : array 0 .. 19 of waypointType
|
where the type contains x,y positions and the direction to the next one (with special flags for start/end)
consider a map such as
code: |
..>+
..+.
>+^.
....
|
Then it's mostly the same code, but used just once, not 19 times.
(Programming pro-tip: if you have 19 blocks of code that look almost the same, they most likely can be replaced with a single block of the same layout, with differences replaced by use of new variables)
|
Tony's programming blog. DWITE - a programming contest. |
|
|
|
|
|
|