
-----------------------------------
Beastinonyou
Sat Jun 11, 2011 1:23 pm

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.

-----------------------------------
2F_Zelda
Sat Jun 11, 2011 1:34 pm

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
Sat Jun 11, 2011 1:46 pm

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.


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.


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
Sat Jun 11, 2011 3:08 pm

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.

-----------------------------------
2F_Zelda
Sat Jun 11, 2011 4:00 pm

Re: 2D Tile Game Component Help
-----------------------------------

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
Sat Jun 11, 2011 7:13 pm

Re: 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.


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)

-----------------------------------
Raknarg
Sat Jun 11, 2011 7:49 pm

RE:2D Tile Game Component Help
-----------------------------------
I haven't tried this before, but my idea would be to use an array of records:


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
Sat Jun 11, 2011 10:26 pm

Re: RE:2D Tile Game Component Help
-----------------------------------
I haven't tried this before, but my idea would be to use an array of records:


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.

-----------------------------------
Tony
Sat Jun 11, 2011 11:02 pm

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.

-----------------------------------
Beastinonyou
Sun Jun 12, 2011 12:40 pm

Re: 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.

That does seem like the easiest approach..

How would I go about making those waypoints / what would I have to use?

-----------------------------------
Tony
Sun Jun 12, 2011 1:21 pm

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.

-----------------------------------
Beastinonyou
Sun Jun 12, 2011 3:31 pm

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
Mon Jun 13, 2011 8:16 pm

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



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 