visual studio 2010 - Creating a rectangle within a class -
so i'm trying figure out how implement rectangles in class around each bullet fire. it's missile command type game. can't figure out declare rectangle , how pass game.
here code rocket class...
using system; using system.collections.generic; using system.linq; using system.text; using microsoft.xna.framework; using microsoft.xna.framework.graphics; namespace shootingrocket { public class rocket { public texture2d drawtexture { get; set; } public vector2 position { get; set; } public vector2 direction { get; set; } public float rotation { get; set; } public float speed { get; set; } public bool isshooting { get; set; } int timebetweenshots = 100; int shottimer = 0; public rocket(texture2d texture, vector2 position, vector2 direction, float rotation, float speed) { this.drawtexture = texture; this.position = position; this.direction = direction; this.rotation = rotation; this.speed = speed; this.isshooting = false; } public void update(gametime gametime) { this.position += direction * speed; if (isshooting) { shottimer += gametime.elapsedgametime.milliseconds; if (shottimer > timebetweenshots) { shottimer = 0; projectilemanager.addbullet(this.position, this.direction, 12, 2000, bullettype.player); } } } public void draw(spritebatch spritebatch) { spritebatch.draw( this.drawtexture, this.position, null, color.white, this.rotation, new vector2( this.drawtexture.width / 2, this.drawtexture.height / 2), 1.0f, spriteeffects.none, 1.0f);
here code bullet class...
using system; using system.collections.generic; using system.linq; using system.text; using microsoft.xna.framework; using microsoft.xna.framework.graphics; namespace shootingrocket { public enum bullettype { player, enemy } public class bullet { public bullettype type { get; set; } public texture2d drawtexture { get; set; } public vector2 position { get; set; } public vector2 direction { get; set; } public float speed { get; set; } public int activetime { get; set; } public int totalactivetime { get; set; } public bullet(texture2d texture, vector2 position, vector2 direction, float speed, int activetime, bullettype type) { this.drawtexture = texture; this.position = position; this.direction = direction; this.speed = speed; this.activetime = activetime; this.type = type; this.totalactivetime = 0; } public void update(gametime gametime) { this.position += direction * speed; this.totalactivetime += gametime.elapsedgametime.milliseconds; } public void draw(spritebatch spritebatch) { spritebatch.draw( drawtexture, position, null, color.white, 0f, new vector2( drawtexture.width / 2, drawtexture.height / 2), 1.0f, spriteeffects.none, 0.8f); } } }
and last not least projectile manager class...
using system; using system.collections.generic; using system.linq; using microsoft.xna.framework; using microsoft.xna.framework.audio; using microsoft.xna.framework.content; using microsoft.xna.framework.gamerservices; using microsoft.xna.framework.graphics; using microsoft.xna.framework.input; using microsoft.xna.framework.media; namespace shootingrocket { public class projectilemanager : drawablegamecomponent { static list<bullet> bullets = new list<bullet>(); static texture2d playerbullettexture; spritebatch spritebatch; public projectilemanager(game game, spritebatch spritebatch) : base(game) { game.components.add(this); playerbullettexture = game.content.load<texture2d>("bullet"); this.spritebatch = spritebatch; } public override void update(gametime gametime) { for(int = 0; < bullets.count; i++) { bullets[i].update(gametime); if (bullets[i].totalactivetime > bullets[i].activetime) bullets.removeat(i); } base.update(gametime); } public override void draw(gametime gametime) { foreach (bullet b in bullets) { b.draw(spritebatch); } base.draw(gametime); } public static void addbullet(vector2 position, vector2 direction, float speed, int activetime, bullettype type) { bullets.add(new bullet(playerbullettexture, position, direction, speed, activetime, type)); } } }
any appreciated!
public rectangle collisionrec {get; private set;}
then in update method of bullet update rectangle coordinates.
collisionrec = new rectangle(position.x, position.y, spritewidth, spriteheight);
you maintaining list of bullets in game1 or somewhere.
foreach (bullet b in bulletlist) { if (b.collisionrec.intersects(someotherrec)) { //do stuff. } }
Comments
Post a Comment