Class hierarchy for ship game
2 posters
Freelance Development :: Code :: Code
Page 1 of 1
Class hierarchy for ship game
Here's the basic class hierarchy I made:
P.S. the code boxes on this forum are painful with the current color scheme. Someone's gonna have to do something about that.
Edit: Added update position function in the base sprite class. It's only preliminary, so if someone has a better function for that, let me know. Also: I suggest that all new classes we code (Ships, bullets, etc.) all be placed in here for easy organization.
Edit2: I dun edited it again. Not that anyone has looked at this before I edited it, so I won't even bother saying what I changed. I also created a naive camera class:
And, finally, finished the first bullet class to get the idea I'm going for in building these accross:
No time to test all this right now, but it at least compiles.
- Code:
public abstract class sprite
{
protected int Xposition;
protected int Yposition;
protected int Xsize;
protected int Ysize;
protected int Yspeed;
protected int Xspeed;
public int hp;
public int damage;
protected Texture2D tex;
public Rectangle rect;
public abstract bool update(sprite[] onscreen, SpriteBatch sb, Camera cam); //If update returns false, we kill the sprite
protected void updatepos()
{
Xposition += Xspeed;
Yposition += Yspeed;
}
public int[,] GetClipping()
{
int[,] clipping = { { Xposition, Yposition }, { Xposition + Xsize, Yposition + Ysize } };
return clipping;
}
public bool checkclipping(sprite sp)
{
return rect.Intersects(sp.rect);
}
}
public abstract class ship : sprite
{
public abstract void hit(sprite sp);
}
public abstract class goodship : ship
{
}
public abstract class badship : ship
{
}
public abstract class bullet : sprite
{
protected int lifetimer;
}
P.S. the code boxes on this forum are painful with the current color scheme. Someone's gonna have to do something about that.
Edit: Added update position function in the base sprite class. It's only preliminary, so if someone has a better function for that, let me know. Also: I suggest that all new classes we code (Ships, bullets, etc.) all be placed in here for easy organization.
Edit2: I dun edited it again. Not that anyone has looked at this before I edited it, so I won't even bother saying what I changed. I also created a naive camera class:
- Code:
public class Camera
{
private int Xpos;
private int Ypos;
private int Xsize;
private int Ysize;
private int xspeed = 0;
private int yspeed = 0;
private delegate void function(); //This is going to need to be edited for whatever we want to pass to the update functions
//(In case you didn't figure it out, this is a callback. You write a funciton then just pass that here, the camera will update using that function.
//This is for greater extendability
private function updatefunc;
public void setupdate(Delegate func)
{
updatefunc = (function)func;
}
public Camera(int x, int y, int xs, int ys)
{
Xpos = x; Ypos = y; Xsize = xs; Ysize = ys;
}
public void move(int x, int y)
{
Xpos += x;
Ypos += y;
}
public int get_Xpos()
{
return Xpos;
}
public int get_Ypos()
{
return Ypos;
}
public int get_Xsize()
{
return Xsize;
}
public int get_Ysize()
{
return Ysize;
}
public void update()
{
updatefunc();
}
}
And, finally, finished the first bullet class to get the idea I'm going for in building these accross:
- Code:
public class standardgoodbullet : bullet
{
public standardgoodbullet(int x, int y, int w, int h, int xs, int ys)
{
hp = 1;
damage = 10;
lifetimer = 40;
Xposition = x;
Yposition = y;
Xsize = w;
Ysize = h;
Yspeed = ys;
Xspeed = xs;
}
private void interact(ship s)
{
if (checkclipping(s))
{
s.hit(this);
}
}
private void draw(SpriteBatch sb, Camera cam)
{
int drawposx1 = Xposition - cam.get_Xpos();
int drawposy1 = Yposition - cam.get_Ypos();
int drawposx2 = drawposx1 - Xspeed;
int drawposy2 = drawposy1 - Yspeed;
int drawposx3 = drawposx2 - Xspeed - Xspeed;
int drawposy3 = drawposy2 - Yspeed - Yspeed;
Random random = new Random();
byte r1 = (byte)random.Next(0, 256);
byte r2 = (byte)random.Next(0, 256);
byte r3 = (byte)random.Next(0, 256);
byte b1 = (byte)random.Next(0, 256);
byte b2 = (byte)random.Next(0, 256);
byte b3 = (byte)random.Next(0, 256);
byte g1 = (byte)random.Next(0, 256);
byte g2 = (byte)random.Next(0, 256);
byte g3 = (byte)random.Next(0, 256);
sb.Draw(tex, new Rectangle(drawposx1, drawposy1, Xsize, Ysize), new Color(r1, b1, g1));
sb.Draw(tex, new Rectangle(drawposx2, drawposy2, Xsize, Ysize), new Color(r2, b2, g2));
sb.Draw(tex, new Rectangle(drawposx3, drawposy3, Xsize, Ysize), new Color(r3, b3, g3));
}
public override bool update(sprite[] onscreen, SpriteBatch sb, Camera cam)
{
updatepos();
lifetimer -= 1;
rect = new Rectangle(Xposition, Yposition, Xsize, Ysize);
draw(sb, cam);
foreach (sprite i in onscreen)
{
if (i is badship)
{
interact((ship)i);
}
}
if (lifetimer <= 0 ||
hp <= 0)
{
return false;
}
else
{
return true;
}
}
}
No time to test all this right now, but it at least compiles.
JosiahB- Posts : 2
Join date : 2011-04-02
Re: Class hierarchy for ship game
Here is a great place to find info on making bullets and weapons
http://davisxna.wordpress.com/2011/04/21/topdownshooter-part-3-weapons-and-bullets/
http://davisxna.wordpress.com/2011/04/21/topdownshooter-part-3-weapons-and-bullets/
AustinW- Posts : 1
Join date : 2011-04-14
Freelance Development :: Code :: Code
Page 1 of 1
Permissions in this forum:
You cannot reply to topics in this forum