c# - Second Enemy object not acting like the first enemy -
how come when add new enemy object list of objects loaded game, second 1 doesn't move around screen first one?
objlist code
class items { public static list<obj> objlist = new list<obj>(); public static void initialize() { objlist.add(new player(new vector2(50, 50))); objlist.add(new enemy(new vector2(500, 500))); objlist.add(new enemy(new vector2(600, 200))); objlist.add(new blueball(new vector2(300, 400))); objlist.add(new greenball(new vector2(350, 100))); objlist.add(new orangeball(new vector2(900, 250))); objlist.add(new pinkball(new vector2(100, 500))); objlist.add(new redball(new vector2(600, 500))); objlist.add(new yellowball(new vector2(500, 250))); }
the third entry in list second enemy wish load game. first 1 works should second enemy doesn't move around screen first. below enemy class movement code.
enemy class
class enemy : obj { float spd = 1; float detectiondistance = 175; public enemy(vector2 pos) : base(pos) { position = pos; spritename = "blackball"; speed = spd; } public override void update() { rotation = point_direction(position.x, position.y, player.player.position.x, player.player.position.y); speed = math.abs(player.player.position.x - position.x) < 175 && math.abs(player.player.position.y - position.y) < 230 ? spd : 0; base.update(); } public override void pushto(float pix, float dir) { float newx = (float)math.cos(mathhelper.toradians(dir)); float newy = (float)math.sin(mathhelper.toradians(dir)); newx *= pix; newy *= pix; if (!collision(new vector2(newx, newy), new player(vector2.zero))) { base.pushto(pix, dir); } } //uses trig calculate shortest distance player moves towards position private float point_direction(float x, float y, float x2, float y2) { float diffx = x - x2; float diffy = y - y2; float adj = diffx; float opp = diffy; float tan = opp / adj; float res = mathhelper.todegrees((float)math.atan2(opp, adj)); res = (res - 180) % 360; if (res < 0) { res += 360; } return res; } }
the line speed = math.abs(player.player.position.x - position.x) < 175 && math.abs(player.player.position.y - position.y) < 230 ? spd : 0;
enemy supposed do, move towards player if within radius of 175 doesn't seem working on second enemy , can't see why.
objects updated in game1.cs
protected override void update(gametime gametime) { foreach (obj o in items.objlist) { o.update(); }
object class
class obj : microsoft.xna.framework.game { public vector2 position; public float rotation = 0.0f; public texture2d spriteindex; public string spritename; public float speed = 0.0f; public float scale = 1.0f; public bool alive = true; public rectangle area; public bool solid = false; public int score; public obj(vector2 pos) { position = pos; } private obj() { } public virtual void update() { if (!alive) return; updatearea(); pushto(speed, rotation); } public virtual void loadcontent(contentmanager content) { spriteindex = content.load<texture2d>("sprites\\" + spritename); area = new rectangle((int)position.x - (spriteindex.width / 2), (int)position.y - (spriteindex.height / 2), spriteindex.width, spriteindex.height); } public virtual void draw(spritebatch spritebatch) { if (!alive) return; rectangle size; vector2 center = new vector2(spriteindex.width / 2, spriteindex.height / 2); spritebatch.draw(spriteindex, position, null, color.white, mathhelper.toradians(rotation), center, scale, spriteeffects.none, 0); } public bool collision(vector2 pos, obj obj) { rectangle newarea = new rectangle(area.x, area.y, area.width, area.height); newarea.x += (int)pos.x; newarea.y += (int)pos.y; foreach (obj o in items.objlist) { if (o.gettype() == obj.gettype() && o.solid) if (o.area.intersects(newarea)) return true; } return false; } public obj collision(obj obj) { foreach (obj o in items.objlist) { if (o.gettype() == obj.gettype()) if (o.area.intersects(area)) return o; } return new obj(); } public void updatearea() { area.x = (int)position.x - (spriteindex.width / 2); area.y = (int)position.y - (spriteindex.height / 2); } public t checkcollisionagainst<t>() t : obj { // if collision detected, returns colliding object; otherwise null. return items.objlist .oftype<t>() .firstordefault(o => o.area.intersects(area)); } public virtual void pushto(float pix, float dir) { float newx = (float)math.cos(mathhelper.toradians(dir)); float newy = (float)math.sin(mathhelper.toradians(dir)); position.x += pix * (float)newx; position.y += pix * (float)newy; } }
Comments
Post a Comment