/// <summary> /// Constructs a new entity. /// Every entity will have a default Circle hitbox of size 10 right at the center of the entity. /// Hitboxes cannot be null. If you want an entity without a hitbox, make a Polyhitbox and /// do not add anything to it. /// Hp is set to 1 and mp is set to 0 by default. /// Use the properties HP and MP to edit. /// </summary> /// <param name="w">Width of the entity</param> /// <param name="h">Height of the entity</param> /// <param name="x">(Optional)X coordinate of the upper left corner of the entity. Defaults to 0 if not set.</param> /// <param name="y">(Optional)Y coordinate of the upper left corner of the entity. Defaults to 0 if not set.</param> public Entity(Image img) { this.loc = new Point(0,0); hp = 1; mp = 0; baseSpeed = 10; this.img = img; hitbox = new CircleHitbox(this, img.Width / 2, img.Height / 2, 10); currentTeam = Team.NEUTRAL; hitbox.setHitboxColor(Color.Gray, Color.White); Updater.register(this); }
public Entity(Image img, Point loc, Hitbox hb, bool collidable = false,int hp = 1, int mp = 0,int baseSpeed = 10, Team team = Team.NEUTRAL) { this.loc = loc; this.hp = hp; shownHP = hp; currentHP = hp; this.mp = mp; shownMP = mp; currentMP = mp; this.hitbox = hb; this.baseSpeed = baseSpeed; currentTeam = team; hb.setHitboxColor(Color.Gray, Color.White); Updater.register(this, collidable); }
/// <summary> /// Check if all the hitbox collides with the other hitbox. /// </summary> /// <param name="collide">The hitbox of the other entity to check collision with.</param> /// <returns>True if it collides, false otherwise.</returns> public override bool CollideWith(Hitbox collide) { int size = hitboxes.Count; if (size == 0) { return false; } //poly to poly, needs to check everything if (collide is PolyHitbox) { int size2 = ((PolyHitbox)collide).getNumOfHitbox(); if (size2 == 0) { return false; } List<Hitbox> others = ((PolyHitbox)collide).hitboxes; for (int i = 0; i < size; i++) { for (int j = 0; j < size2; j++) { if(hitboxes[i].CollideWith(others[j])) { return true; } } } return false; } //poly to single for (int i = 0; i < size; i++) { if (hitboxes[i].CollideWith(collide)) { return true; } } return false; }
/// <summary> /// Checks collision with the entity that was passed in the parameter. /// </summary> /// <param name="other">The entity to be checked</param> /// <returns>true if it collides, false otherwise.</returns> public override bool CollideWith(Hitbox other) { if (other == null) { return false; } double distance; //Square to circle collision if (other is SquareHitbox) { int rightx = other.X + other.Width; int boty = other.Y + other.Height; //((Stage1)(owner.CurrentStagePanel)).label1.Text = cx + " " + cy + " " + other.Y; if (rightx >= cx && other.X <= cx || other.Y <= cy && boty >= cy) { if ((other.X <= x + width && rightx >= X) && (boty >= y && other.Y <= y + width)) { //owner.CurrentStagePanel.PanelBgPanel.BackColor = Color.Black; //((Stage1)(owner.CurrentStagePanel)).label1.Text = "SQUARE!!!"; return true; } else { //owner.CurrentStagePanel.PanelBgPanel.BackColor = Color.Firebrick; return false; } } else if (getDistance(cx, cy, other.X, other.Y) < width / 2 || getDistance(cx, cy, rightx, other.Y) < width / 2 || getDistance(cx, cy, other.X, boty) < width / 2 || getDistance(cx, cy, rightx, boty) < width / 2) { //((Stage1)(owner.CurrentStagePanel)).label1.Text = "sq!!!"; //owner.CurrentStagePanel.PanelBgPanel.BackColor = Color.Black; return true; } else { //owner.CurrentStagePanel.PanelBgPanel.BackColor = Color.Firebrick; return false; } } //Circle to Circle collision else if(other is CircleHitbox) { double combinedradius = (other.Width / 2) + (width / 2); distance = getDistance(cx, cy, other.CX, other.CY); //((Stage1)(owner.CurrentStagePanel)).label1.Text = distance + " " + combinedradius + " " + (distance < combinedradius); if (distance < combinedradius) { //owner.CurrentStagePanel.PanelBgPanel.BackColor = Color.Black; //((Stage1)(owner.CurrentStagePanel)).label1.Text = "COLLIDE cir!!!"; return true; } else { //owner.CurrentStagePanel.PanelBgPanel.BackColor = Color.Firebrick; return false; } } else if (other is PolyHitbox) { PolyHitbox h = (PolyHitbox)other; List<Hitbox> temp = h.getAllHitboxes(); int size = h.getNumOfHitbox(); for (int i = 0; i < size; i++) { if (temp[i].CollideWith(this)) { return true; } } return false; } else { throw new ArgumentException("What kind of hitbox is that?"); } }
/// <summary> /// Sets the hitbox of the entity. /// </summary> /// <param name="h">The new Hitbox. Cannot be null.</param> public void setHitbox(Hitbox h) { if (h == null) { throw new ArgumentException("Hitbox cannot be null"); } hitbox = h; }
public Boss(Image img, Point loc, Hitbox hb,bool collidable = false, int hp = 1, int mp = 0, int baseSpeed = 10, Team team = Team.NEUTRAL) : base(img, loc, hb, collidable ,hp, mp,baseSpeed, team) { }
/// <summary> /// Check if this hitbox will collide to the other entity's hitbox. /// </summary> /// <param name="other">The other entity to check collision.</param> /// <returns>True if they collide. Otherwise, false.</returns> public override bool CollideWith(Hitbox other) { if (other == null) { return false; } if (other is CircleHitbox) { int rightx = x + width; int boty = y + height; int othercx = other.CX; int othercy = other.CY; //((Stage1)(owner.CurrentStagePanel)).label1.Text = cx + " " + cy + " " + other.Y; if (rightx >= othercx && x <= othercx || y <= othercy && boty >= othercy) { if ((x <= other.X + other.Width && rightx >= other.X) && (boty >= other.Y && y <= other.Y + other.Width)) { //owner.CurrentStagePanel.PanelBgPanel.BackColor = Color.Black; //((Stage1)(owner.CurrentStagePanel)).label1.Text = "SQUARE!!!"; return true; } else { //owner.CurrentStagePanel.PanelBgPanel.BackColor = Color.Firebrick; return false; } } else if (getDistance(othercx, othercy, x, y) < other.Width / 2 || getDistance(othercx, othercy, rightx, y) < other.Width / 2 || getDistance(othercx, othercy, x, boty) < other.Width / 2 || getDistance(othercx, othercy, rightx, boty) < other.Width / 2) { //((Stage1)(owner.CurrentStagePanel)).label1.Text = "sq!!!"; //owner.CurrentStagePanel.PanelBgPanel.BackColor = Color.Black; // Random rand = new Random(); // candidate.ChangeHitbox(new CircleHitbox(candidate, candidate.Width / 2, candidate.Height / 2, rand.Next(50)+1)); return true; } else { //owner.CurrentStagePanel.PanelBgPanel.BackColor = Color.Firebrick; return false; } } else if (other is SquareHitbox) { int myrightx = x + width; int myboty = y + height; int otherrightx = other.X + other.Width; int otherboty = other.Y + other.Height; if ((other.X <= myrightx && otherrightx >= X) && (otherboty >= y && other.Y <= myboty)) { // owner.CurrentStagePanel.PanelBgPanel.BackColor = Color.Black; //((Stage1)(owner.CurrentStagePanel)).label1.Text = "SQUARE!!!"; return true; } else { // owner.CurrentStagePanel.PanelBgPanel.BackColor = Color.Firebrick; return false; } } else if (other is PolyHitbox) { PolyHitbox h = (PolyHitbox)other; List<Hitbox> temp = h.getAllHitboxes(); int size = h.getNumOfHitbox(); for (int i = 0; i < size; i++) { if (temp[i].CollideWith(this)) { return true; } } return false; } else { throw new ArgumentException("What hitbox is that?!"); } }
/// <summary> /// Must be implemented. Checks for collision with another entity. /// </summary> /// <param name="collide">Entity to collide with</param> /// <returns>True if they collide. False otherwise.</returns> public abstract bool CollideWith(Hitbox collide);