Sign in to follow this  
Antonym

Asteroids

Recommended Posts

I am working on an asteroids like game and was wondering if there was an efficient/easy/proper way of doing the 'destroy asteroid two smaller ones spawn'. Right now I have it setup so there's an object class containing all properties and functions for those properties which derives into a 'ship, missile, asteroid, effect' classes. Each with a different constructor(Which sets all the appropriate stats), 'update','handle_collision' and 'handle_death' functions. I was wondering if I could make the most of the single asteroid class instead of having to create a 'big' 'small' 'regular' etc ones. I am using c++. Any advice/info would be appreciated. Thanks

Share this post


Link to post
Share on other sites
Quote:
Original post by Antonym
I was wondering if I could make the most of the single asteroid class instead of having to create a 'big' 'small' 'regular' etc ones.
You definitely can make the most of it but you will still need big, small and regular versions of something; it depends on what an asteroid is in term of its representation, some examples:

// An asteroid as an image
Asteroid smallAsteroid( loadImage("small.bmp") );

// An asteroid as a collection of vertices #1
Asteroid bigAsteroid( BIG_ASTEROID_VERTS );

// An asteroid as a collection of vertices #2
Asteroid regularAsteroid( generateVertices(REGULAR_VERT_COUNT, REGULAR_RADIUS, REGULAR_VARIANCE) );

// An asteroid as a 'circle'
Asteroid comedySizedAsteroid( comedyRadius );


[Edited by - dmatter on January 16, 2009 10:08:53 AM]

Share this post


Link to post
Share on other sites
I suppose I would only have to pass different arguments to the constructor my only problem is the 'handle_death' function which is the one that spawns the new asteroids(once the current one dies)

This is how the asteroid class looks right now

#include "Asteroid.h"
#include "Object.h"
#include "Application.h"
#include "Game.h"

//Set
Asteroid::Asteroid(D3DXVECTOR2 new_position, float new_rotation, Application &application)
:
Object(new_position,
new_rotation,
application.get_sprite("asteroid"),
32,
6, //radius
2, //mass
2, //impact damage
2, //hit_points
.2, //speed
ASTEROID), //type
rotate_amount(0)
{}

//Update
void Asteroid::update(Game &game)
{
DWORD time = timeGetTime();
if((time - get_time_since_last_shot()) > get_cooldown())
{
set_time_since_last_shot(time);
rotate_amount = game.random_number((float)-1,(float)1);
}

rotate(rotate_amount);
}

//Handle Collision
void Asteroid::handle_collision(Game &game, Object &other)
{
if(other.get_object_type() != ASTEROID)
{
set_hit_animation(true);
damage(other.get_impact_damage());
}
}

//Handle Death
void Asteroid::handle_death(Game &game)
{
game.create_asteroid(get_position(), get_rotation());
game.create_asteroid(get_position(), get_rotation());
}


Share this post


Link to post
Share on other sites
Mike has written an Asteroids sample for SlimDX's samples collection. It handles this very problem -- perhaps you might want to take a look at it? It's in C#, but the logic is essentially the same and I think you should be able to follow the syntax of C# well enough to understand it.

You have the basic idea down. The size of your asteroid is a property of the asteroid (in the SlimDX example, I believe this is an enumeration of the three different levels of asteroid size). On death, if the size is not the smallest, two asteroids of the next-smallest size are created. Otherwise (when the size is the smallest) the asteroid is just destroyed without spawning new ones.

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