# About making damage between units

I am making my own Tower Defence game engine. I havent built one before and I don't know lot of stuff how games work.

I will take Starcraft as an example. You have a marine shooting a zergling from a certain distance. But you dont see the bullets traveling trough the air because the marine and the zergling are either too small. So if there are no bullets at all how does the game engine does damage to the zergling under the hood (Use hitTestObject() in flash with what :) )?

I have and idea to add in my game engine and it work this way as explained in the example but I dont know how to implement it trough code.

thanks

• possible duplicate of How are bullets simulated in video games? – Laurent Couvidou Jun 13 '13 at 11:45
• I don't think so, because I am building a 2D game where I just want to make damage from one distant unit to another. I will surelly not use raycasting or whatever 3D graphics techniques. I was asking for a code mechanism only. I guess it has something with how towers in tower defence games work i.e when some units gets into range of the tower what it does to aim and shoot. – Vlad Jun 13 '13 at 15:47
• Raycasting is not a 3D-only technique. It is the most straightforward way to find if a shot hits a target, even in 2D. – Laurent Couvidou Jun 13 '13 at 15:56
• Well probably. I didn't know that. But I would go with easier things like showing a bullet line and checking if that hits the object, but a better solution would be to use @Phillipp 's answer making damage in code only, not showing on the screen, because I might have intersection problems with other minions :) – Vlad Jun 13 '13 at 15:59
• "showing a bullet line and checking if that hits the object": Well you've just described what raycasting is. Except you don't necessarily have to display the line. – Laurent Couvidou Jun 13 '13 at 16:05

When you don't have actual projectile physics, an entity would just damage another entity directly. The update algorithm which is called every game tick would, in pseudocode, look like this:

for each object in the game
if it has a target
if the target is in range and weapon is ready to fire
reduce the hit points of the target
else
stand around waiting

for each object in the game
when its HP are <= 0
remove the object


To understand this, you need to understand the basic architecture of any computer program. Typically you want split your application into tiers. One of the tiers should be the presentation tier and then you can have one or more tiers that take care of the rest, again usualy a logic tier and a data tier.

Now everything that happens in the game is happening in the logic tier. Your logic takes a marine and calls marine.shoot(zergling) that can look like this:

procedure shoot(target)
begin
shootInDirection(target)
showGunEffect()
if target.hit() then
calculateDamage(target)
showDamage(target)
else
showMiss()
end


Methods like showGunEffect(), showDamage() and showMiss() are calls to the presentation tier to actually show the graphics.

Now let's make the program more complex:

procedure shoot(target)
begin
bullet = shootInDirection(target)
showNewObject(bullet)
move(bullet, target)
registerEventListener(bullet)
showGunEffect()
end

procedure onBulletEvent(event)
begin
if (event == hit)
calculateDamage(target)
showDamage(target)
else if (event == miss)
showMiss()
end


Now for the first program you simply need to show the gun effects, hits and misses. For the second one you actually need some kind of collision engine that will take care of the event like bullet colliding with target. The first one is of course very simple and as you said - the entities are so small, that it doesn't make sense to actually show bullets for each shot, not even talking about calculating the trajectories, hits etc.

But for both programs, everything that happens is just a calculation. And only the calls to presentation layer will make something visible. In the first case, the bullets probably fly instantly. So you don't have to show anything, or you can simply draw a line from marine to zergling to show the bullet trajectory.

In the second case, some game loop will be updating the bullets position and calling the rendering to show the bullet.