Skip to main content
gamedev.net
🔒 Locked

How should I do this?

Started by starfleetrp Mar 23, 2006 at 2:49 PM 6 replies 900+ views
Original Post
starfleetrp
starfleetrp
I do not know exactly how I should lay out these classes. Player (will contain sprite) Sprite (will contain X number of guns) Guns (Will contain the projectiles in play). I do not know how I would go about create these projectiles. Would they be in a dynamic array (if so how?) which would create a new item for each projectile launched, since they could be 1 to 21 at a time depending on the gun) Projectile (Will contain pos and collision detection?) Now once I am done with this how could I remove it from the Guns class. Since its no longer in use?
Daishim
Daishim
Why would the player contain a "sprite" which holds the "guns". I'm assuming by sprite you mean a graphical sprite? If so, you typically don't want to bind non-graphical code to graphical objects/code.

My approach would be to have a character class. The character contains the bounding (size, texture ID) for the sprite. The actual sprite would be located in a texture manager/array in the renderer or graphics object that handles the drawing.

The character also holds an array of guns. Perhaps dynamic, depending on your needs. Or just a static array of guns with an enable on whether the character has them or not. The guns would contain bounding information for their sprite or sub-bounding information if they're reside in the same sprite as the character. Though, I would create a character sprite and a seperate guns sprite.

Unless you need to maintain "ownership" of the projectiles, I would actually just use the gun information on the character to define a projectile that is launched and hold them in a seperate structure/dynamic array outiside of the character(s). Perhaps a structure for atmospheric entities or just a projectiles structure. Even if you needed to maintain "ownership", you can simply add a tag to the projectile object to signify who/what launched it.

I would create a seperate collision detection function/object to sit outside the character, again, to perform collision calculations based on projectile information and character/enemy positions that you just pass to the function/object method. This way, you don't tie non-specific code to specific objects or data.

I know only that which I know, but I do not know what I know.
starfleetrp
starfleetrp
Yeah, that kinda makes sence. However I am not used to this type of coding... With dynamic arrrays. Once I am done with a projectile how could I remove it from an array/structure of projectiles? Would it be another function, because as far as I know an object can not delete it self. If so how would you recommend doing it?
Daishim
Daishim
For simplicity, if you're using C++, you could use an STL vector to hold the objects. Or you could roll your own container, however, if you're not used to dynamic arrays, I would read up on dynamic memory before venturing down that road.

You can find more about the STL vector for C++ at www.cppreference.com.

I know only that which I know, but I do not know what I know.
RobAU78
RobAU78
Quote:
Original post by starfleetrp
I do not know exactly how I should lay out these classes.

Player (will contain sprite)

Sprite (will contain X number of guns)

Guns (Will contain the projectiles in play). I do not know how I would go about create these projectiles. Would they be in a dynamic array (if so how?) which would create a new item for each projectile launched, since they could be 1 to 21 at a time depending on the gun)


It's probably a good idea to create a class to handle the creation and destruction of game objects. Call it GameObjectManager or whatever. This class would contain the "master list(s)" of all the objects in the game.

Then what you should do is implement the Observer pattern. You can read about it here: http://en.wikipedia.org/wiki/Observer_pattern. The idea is that the GameObjectManager somehow knows when an object is created elsewhere so it can add the object to its list(s). Use methods like notifyCreated() and notifyDestroyed().

Projectile (Will contain pos and collision detection?) Now once I am done with this how could I remove it from the Guns class. Since its no longer in use?[/quote]

The projectile should exist more or less independently of the gun that launched it. All that the gun does is give the projectile its initial velocity vector. After that, the projectile is on its own. What you should do is something like this:

void Gun::fire() {   // Create a new projectile   Projectile p = new Projectile( /* parameters */ );      // Notify the object manager about the new projectile   notifyCreated(&p);}
starfleetrp
starfleetrp
Ok, I have just a few question about this. Projectiles would be a structure with all of the params, right? Not a class. Second how would you go about updating the shells position, since its stored in the ObjectManager. Would it be a class outside of the ObjectManager, and if so how would you recommend going about this. And lastly what other object would be commonly in the ObjectManager?
andrew_480
andrew_480
OK. The way I would do it is to have all of your sprites images in one picture, and use a sprite sheet type to store all of the animation information.
I think that generally you would set up a sprite class with these classes:

1. SpriteRect - the area of an image that makes one frame. Store left, top, width, height, x offset, and y offset.

2. SpriteSheet - has an image with all of the frames on it. Stores all of the SpriteRects in an array. Stores all of the possible animations for this sprite type in an array.

4. SpriteFrame - stores the SpriteRect index(from the SpriteSheet) and the duration of the frame.

3. Animation - An array of SpriteFrames.

5. SpriteBase - Points to a SpriteSheet. Derive playerBase, gunBase, bulletBase, and enemyBase(?) types from this. In these types store things that are common to all players/guns/enemies, like max hit points, damage, armor piercing...

6. Sprite - Points to it's SpriteBase. Derive your player, gun, bullet, and enemy types from this. Instances of these will be the actual game objects. Store things like current hit points, inventory, armor, current animation, current frame, ... Should be able to draw itself, move, ...

Quote:

Second how would you go about updating the shells position, since its stored in the ObjectManager. Would it be a class outside of the ObjectManager, and if so how would you recommend going about this. And lastly what other object would be commonly in the ObjectManager?


I think that the ObjectManager should go through all of the objects each cycle and maybe call a tick()/move() method of each object, which will then handle it's own actions. It could also check collisions between sprites, delete old ones, ...

I also have a question for eveyone. Do you normally make managers for everything, and access them with indexes? I have been coding a wrapper for SDL and I made audio and font managers, but nothing to manage images. Should all images be accessed via a manager? And is it OK to use a map instead of a vector to do this(thats what I did for audio and fonts)?
VisionForce
VisionForce
Here's the layout I think you should use.

SpriteClass (renders any object through DirectX, but not really useful alone. Includes X,Y,Z positions, Width, Height, and texture and/or mesh info.)

Bullet - Inherits SpriteClass (or Projectile, maybe add a BulletType enumeration)

Player - Inherits SpriteClass (has Player information, but also has a Bullet array in it. The player class also has a ShootBullet and MoveBullets procedure)

First, all of the bullets in the array are Null, but then when call the ShootBullet method, it initializes one of the bullets in the array. The ShootBullet method can never create more bullets than the length of the array, so remember that.

Second, your main game loop will always call the Player's MoveBullets method in your engine's calculation procedure (i.e. the game's "Calculate" method will continuously call the player's MoveBullets method). The MoveBullets method only moves the bullets that are not Null. This method also destroys the bullet objects in the array when they are off the screen or when they hit something so that they'll be ready immediately the next time they're needed.

Here is some code I've written in VB.NET to explain; you may or may not be able to translate.

Notes:
1) The MoveCircle array is an array that helps the player or the bullet move in the proper direction when he is rotated. This was Jack Hoxley's (jollyjeffers) idea.
2) Environment.TickCount is pretty much the same as the GetTickCount function in kernel32.dll.

    Private Sub ShootBullet()        Dim i As Integer        If Not (AllowShoot) Then Return        ShootSound.SetPosition(0)        ShootSound.Play()        For i = 0 To UBound(Bullet)            If (Bullet(i) Is Nothing) Then                Bullet(i) = New MeshClass( _                    Application.StartupPath & "\obj\bullet\bullet.x", _                    Application.StartupPath & "\obj\bullet\")                Bullet(i).X = X                Bullet(i).Y = Y                Bullet(i).Z = Z                Bullet(i).RotateX = 1.7                Bullet(i).RotateY = RotateY                Bullet(i).ScaleX = 0.4                Bullet(i).ScaleY = 0.4                Bullet(i).ScaleZ = 0.4                Bullet(i).Scale = True                AllowShoot = False                Return            End If        Next    End Sub    Public Sub MoveBullets(ByVal MaxLeft As Single, ByVal MaxRight As Single, ByVal Close As Single, ByVal Far As Single)        Dim i As Integer        Dim MoveCircle As Vector3() = GenerateMoveCircle()        If (Environment.TickCount - ShootTick) >= 1000 Then            AllowShoot = True            ShootTick = Environment.TickCount        End If        For i = 0 To UBound(Bullet)            If Not (Bullet(i) Is Nothing) Then                Bullet(i).X += MoveCircle(PlayerRotation).X                Bullet(i).Z += MoveCircle(PlayerRotation).Z                If Bullet(i).X < MaxLeft Then                    Bullet(i) = Nothing                ElseIf Bullet(i).X > MaxRight Then                    Bullet(i) = Nothing                ElseIf Bullet(i).Z < Close Then                    Bullet(i) = Nothing                ElseIf Bullet(i).Z > Far Then                    Bullet(i) = Nothing                Else                    Bullet(i).Render()                End If            End If        Next    End Sub
Alex C. BarberiChief Executive OfficerVisionForcehttp://www.visionforceweb.com

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.