• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
FantasyVII

[Solved]Tower which enemy to shoot at?

9 posts in this topic

Hello everyone,

I have a tower and 10 enemies. now if an enemy get in the tower range, the tower will shoot a bullet and the bullet will keep following the enemy until it hits him.

Now if the first enemy get in the tower range, the tower will shoot a bullet and the bullet class will calculate the enemy position and keep following him. However if the second enemy get in range while the bullet didn't reach the first enemy, the bullet will leave the first enemy and calculate the second enemy position.

I want if the tower shoot a bullet, the bullet should keep following the first enemy even if another enemy get in the tower range. and If the second enemy get in range the tower should shoot a new bullet with the second enemy position.

Here is the code:-

[CODE]
class Bullet
{
public void ShootAt(GameTime gameTime, Vector2 SourcePosition, Vector2 TargetPosition)
{
if (Alive)
{
ElapsedGameTimeSpeed = (float)Speed * (float)gameTime.ElapsedGameTime.TotalSeconds * 10.0f;
DeltaPosition.X = SourcePosition.X - TargetPosition.X;
DeltaPosition.Y = SourcePosition.Y - TargetPosition.Y;
double VectorLength = Math.Sqrt((DeltaPosition.X * DeltaPosition.X) + (DeltaPosition.Y * DeltaPosition.Y));
Direction.X = (float)DeltaPosition.X / (float)VectorLength;
Direction.Y = (float)DeltaPosition.Y / (float)VectorLength;
}
}
}
[/CODE]


[CODE]
class Tower
{
int ShootAtEnemyIndex;

public void IsInRange(List<enemy> enemies)
{
for (int i = 0; i < enemies.Count; i++)
{
Distance.X = enemies[i].Position.X - TowerPosition.X;
Distance.Y = enemies[i].Position.Y - TowerPosition.Y;
DistanceSquare = (Distance.X * Distance.X) + (Distance.Y * Distance.Y);
if (DistanceSquare <= Radius * Radius)
{
ShootAtEnemyIndex = i;
InRange = true;
}
else
InRange = false;
}
}

public void Update(GameTime gameTime,List<enemy> enemies)
{
IsInRange(enemies);

if (InRange)
{
bulletManager.Fire(gameTime, enemies[ShootAtEnemyIndex], Damage, AttackSpeed);
}

//Calculate the distance between the bullet position and the enemy position.
for (int i = 0; i < bulletManager.Bullets.Count; i++)
bulletManager.Bullets[i].ShootAt(gameTime,enemies[ShootAtEnemyIndex].Position, bulletManager.Bullets[i].GetPosition() + bulletManager.Bullets[i].GetVelocity());

bulletManager.Update();
}
[/CODE] Edited by FantasyVII
0

Share this post


Link to post
Share on other sites
[quote name='IADaveMark' timestamp='1351317769' post='4994351']
So what is your problem?
[/quote]

the problem was the when a bullet was fired at enemy 1 and enemy 2 get in the tower range the bullet will stop moving toward enemy 1 and start following enemy 2.

but now i got it fixed thanks.
0

Share this post


Link to post
Share on other sites
Not sure what you did to solve this, but what I would do is have the ShootAt function only called at the time of shooting and the details of what it should shoot at stored in a bullet class. Then just loop over all bullets and call an update function where each one is heading towards its stored off target.
2

Share this post


Link to post
Share on other sites
[quote name='rpiller' timestamp='1351351644' post='4994433']
Not sure what you did to solve this, but what I would do is have the ShootAt function only called at the time of shooting and the details of what it should shoot at stored in a bullet class. Then just loop over all bullets and call an update function where each one is heading towards its stored off target.
[/quote]

Yeah, that was my idea... but that was too easy of a solution so I wanted to clarify. Looks like it is taken care of.

(I forgot to ask my usual first question... "did you trace your code?")
2

Share this post


Link to post
Share on other sites
[quote name='rpiller' timestamp='1351351644' post='4994433']
Not sure what you did to solve this, but what I would do is have the ShootAt function only called at the time of shooting and the details of what it should shoot at stored in a bullet class. Then just loop over all bullets and call an update function where each one is heading towards its stored off target.
[/quote]
I just can't get my mind around it. Starting to hate this project because of this.

[quote name='IADaveMark' timestamp='1351358051' post='4994458']
Yeah, that was my idea... but that was too easy of a solution so I wanted to clarify. Looks like it is taken care of.

(I forgot to ask my usual first question... "did you trace your code?")
[/quote]

the problem is I can't use the ShootAt function only when I shoot because I need to keep track of where is the enemy position. So I need to call it every update. The solution I found was to create a bullet list for each enemy and when the enemy is in rage let that list only shoot at him. the problem is that its very messy and looks very bad. I need another solution but I don't know what.

[CODE]
List<Bullet> Bullets1;
List<Bullet> Bullets2;
List<Bullet> Bullets3;
List<Bullet> Bullets4;
List<Bullet> Bullets5;
List<Bullet> Bullets6;
List<Bullet> Bullets7;
List<Bullet> Bullets8;
List<Bullet> Bullets9;
List<Bullet> Bullets10;
[/CODE]

And I do this 10 times which is very very wrong and bad and messy.
[CODE]
if(enemyManager.enemy.Count > 0)
{
if (IsInRange(enemyManager, 0))
{
Time = (float)gameTime.TotalGameTime.TotalMilliseconds;
deltaTime = Time - LastTime;
if (deltaTime > (1000 / AttackSpeed))
{
Bullets1.Add(new Bullet(Damage));
for (int i = 0; i < Bullets1.Count; i++)
Bullets1[i].Initialize(graphics);
for (int i = 0; i < Bullets1.Count; i++)
Bullets1[i].LoadContent(Content);
LastTime = Time;
}
for (int i = 0; i < Bullets1.Count; i++)
Bullets1[i].SetBulletPosition(Position);
}
for (int i = 0; i < Bullets1.Count; i++)
Bullets1[i].ShootAt(gameTime, enemyManager.enemy[0].Position, Bullets1[i].GetPosition() + Bullets1[i].GetVelocity());

for (int i = 0; i < Bullets1.Count; i++)
Bullets1[i].Update();
}
[/CODE] Edited by FantasyVII
0

Share this post


Link to post
Share on other sites
I know. I did that exact thing. But I need a way to make every bullet separate from each other and make each bullet behaive differently. The problem is I go through all the bullets in the List and I update them and change their position. I need to update each bullet separately. I need a way to keep track of each bullet in the list. And thats what i don't know how to do. going to try this again tonight.
0

Share this post


Link to post
Share on other sites
Nooo.... you didn't do that exact thing. What I'm saying is make a bullet class. Each bullet you fire is a new object of that class. Each of these objects has its own data. That data includes the target that it is intended for (among other things). Therefore, each bullet object has, in its own data, where it is supposed to be going. It doesn't matter if you have 1, 10, 100, or 1000 bullets... each one will have its own target embedded in it. As you iterate through the list, you take each bullet's position and update it towards THAT bullet's target.
2

Share this post


Link to post
Share on other sites
[quote name='IADaveMark' timestamp='1351605753' post='4995410']
Nooo.... you didn't do that exact thing. What I'm saying is make a bullet class. Each bullet you fire is a new object of that class. Each of these objects has its own data. That data includes the target that it is intended for (among other things). Therefore, each bullet object has, in its own data, where it is supposed to be going. It doesn't matter if you have 1, 10, 100, or 1000 bullets... each one will have its own target embedded in it. As you iterate through the list, you take each bullet's position and update it towards THAT bullet's target.
[/quote]

Thank you very much. Sorry for being such a newbie :)

It worked :)

If can give more than 1 Reputation, I would stay all night pressing that button :D

thank you again :)
0

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0