Slick Forums

Discuss the Slick 2D Library
It is currently Sat May 25, 2013 3:05 pm

All times are UTC




Post new topic Reply to topic  [ 4 posts ] 
Author Message
 Post subject: tower defense game
PostPosted: Fri May 11, 2012 6:43 am 
Offline

Joined: Fri Nov 11, 2011 7:49 am
Posts: 58
ok guys im tryin to make a tower defense game.but i couldnt figure out how to make the tower target the moving enemies.can anyone explain how to do that????

_________________
http://jakkarrl.in/


Top
 Profile  
 
 Post subject: Re: tower defense game
PostPosted: Sun May 13, 2012 10:28 am 
Offline
Slick Zombie

Joined: Wed Apr 02, 2008 1:32 pm
Posts: 1315
Location: Italy
hi!
I'm working for a similar game (Drone Defense), so I'm happy to share.

First I suppose you already have enemies moving to a single path, from start to end and modelled a tower with a range. Tower could only target one enemy, for simplicity, okay?

And remember I'm using MarteEngine to build my game, okay?

so let's start:
1) every update of my tower, I check if there is enemy in range and calculate angle to rotate my tower to target
2) if tower have a target, simply create a bullet to kill enemy. Tower shot only 1/2 second at time
3) when enemy is out of range, target become null

some code (from my game, could help you):

Code:

        // on render method i simply rotate graphic context, draw and rotate back

         // PART of update method, target is an entity, getEntities is only a list of entities
          if (target == null) {
         for (Entity e : ME.world.getEntities()) {
            if (e instanceof Enemy) {
               if (getDistance(e) < radius) {
                  if (target == null) {
                     angle = (int) calculateAngle(x, y, e.x, e.y);
                     target = (Enemy) e;
                  }
               }
            }
         }
      } else {
         angle = (int) calculateAngle(x, y, target.x, target.y);
         if (getDistance(target) > radius) {
            target = null;
         } else {
            // fire at will!
            fireTimer += delta;
            if (fireTimer > 500) {
               fireTimer = 0;
               if (isType(Tower.FOTON)) {
                  ME.world.add(new Bullet(x, y, ResourceManager
                        .getSpriteSheet("sheet").getSubImage(0, 1),
                        Bullet.LASER, target, damage));
               } else if (isType(Tower.BREEZE)) {
                  ME.world.add(new Bullet(x, y, ResourceManager
                        .getSpriteSheet("sheet").getSubImage(1, 1),
                        Bullet.PHOTON, target, damage));
               } else if (isType(Tower.PLASMA)) {
                  ME.world.add(new Bullet(x, y, ResourceManager
                        .getSpriteSheet("sheet").getSubImage(1, 1),
                        Bullet.PHOTON, target, damage));
               }

            }
            if (target.died) {
               target = null;
            }
         }
      }

   public float getDistance(Entity other) {
      return getDistance(new Vector2f(other.x, other.y));
   }

       // slick's Vector2f !
   public float getDistance(Vector2f otherPos) {
      Vector2f myPos = new Vector2f(x, y);
      return myPos.distance(otherPos);
   }

   public static float calculateAngle(float x, float y, float x1, float y1) {
      double angle = Math.atan2(y - y1, x - x1);
      return (float) (Math.toDegrees(angle) - 90);
   }



and here result:

Image

* enemies goes from top left and top right to down/right corner
* tower is only thing rotated a bit
* make a decent UI is a pain :(

_________________
Blog | Last game Gravity Duck tribute | In progress Gravity Duck tribute


Top
 Profile  
 
 Post subject: Re: tower defense game
PostPosted: Tue May 15, 2012 2:06 pm 
Offline

Joined: Fri Nov 11, 2011 7:49 am
Posts: 58
well that seems helpful and I guess it would be better if i use marte too!!actually its a joint project by me and my online friend!!so i have to discuss that wiith him.

_________________
http://jakkarrl.in/


Top
 Profile  
 
 Post subject: Re: tower defense game
PostPosted: Tue May 15, 2012 3:05 pm 
Offline

Joined: Fri Nov 11, 2011 7:49 am
Posts: 58
also dude PM me your skype ID i might need it!!!

_________________
http://jakkarrl.in/


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 4 posts ] 

All times are UTC


Who is online

Users browsing this forum: No registered users and 2 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB® Forum Software © phpBB Group