internal void OnCollision(ColliderComponent thisHitbox, ColliderComponent otherHitbox, Point penetration) { }
public void RemoveComponent(ColliderComponent component) { components[component.Parent].Remove(component); }
public void recoverSize(Entity e) { PositionComponent posComp = ( PositionComponent )e.getComponent(GlobalVars.POSITION_COMPONENT_NAME); SquishComponent squishComp = ( SquishComponent )e.getComponent(GlobalVars.SQUISH_COMPONENT_NAME); //Restore Width if (posComp.width < squishComp.baseWidth) { //Collisions on left or right side? List <Entity> leftCollisions = level.getCollisionSystem().findObjectsBetweenPoints(posComp.x - posComp.width / 2 - 1, posComp.y - posComp.height / 2 - 1, posComp.x - posComp.width / 2 - 1, posComp.y + posComp.height / 2 - 1); List <Entity> rightCollisions = level.getCollisionSystem().findObjectsBetweenPoints(posComp.x + posComp.width / 2 + 1, posComp.y - posComp.height / 2 - 1, posComp.x + posComp.width / 2 + 1, posComp.y + posComp.height / 2 - 1); bool leftSide = false; bool rightSide = false; foreach (Entity ent in leftCollisions) { if (ent.hasComponent(GlobalVars.COLLIDER_COMPONENT_NAME)) { ColliderComponent colComp = ( ColliderComponent )ent.getComponent(GlobalVars.COLLIDER_COMPONENT_NAME); if (squishStoppers.Contains(colComp.colliderType)) { leftSide = true; break; } } } foreach (Entity ent in rightCollisions) { if (ent.hasComponent(GlobalVars.COLLIDER_COMPONENT_NAME)) { ColliderComponent colComp = ( ColliderComponent )ent.getComponent(GlobalVars.COLLIDER_COMPONENT_NAME); if (squishStoppers.Contains(colComp.colliderType)) { rightSide = true; break; } } } if (!leftSide && !rightSide) { squishWidthCenter(posComp, squishComp, -squishComp.xEaseRate); } if (leftSide && !rightSide) { squishRight(posComp, squishComp, -squishComp.xEaseRate); } if (!leftSide && rightSide) { squishLeft(posComp, squishComp, -squishComp.xEaseRate); } //Don't over compensate if (posComp.width > squishComp.baseWidth) { level.getMovementSystem().changeWidth(posComp, squishComp.baseWidth); } } else if (posComp.width > squishComp.baseWidth) { level.getMovementSystem().changeWidth(posComp, posComp.width - squishComp.xEaseRate); if (posComp.width < squishComp.baseWidth) { level.getMovementSystem().changeWidth(posComp, squishComp.baseWidth); } } //Restore Height if (posComp.height < squishComp.baseHeight) { //Collisions on left or right side? List <Entity> upperCollisions = level.getCollisionSystem().findObjectsBetweenPoints(posComp.x - posComp.width / 2 + 1, posComp.y + posComp.height / 2 + 1, posComp.x + posComp.width / 2 - 1, posComp.y + posComp.height / 2 + 1); List <Entity> lowerCollisions = level.getCollisionSystem().findObjectsBetweenPoints(posComp.x - posComp.width / 2 + 1, posComp.y - posComp.height / 2 - 1, posComp.x + posComp.width / 2 - 1, posComp.y - posComp.height / 2 - 1); bool upperSide = false; bool lowerSide = false; foreach (Entity ent in upperCollisions) { if (ent.hasComponent(GlobalVars.COLLIDER_COMPONENT_NAME)) { ColliderComponent colComp = ( ColliderComponent )ent.getComponent(GlobalVars.COLLIDER_COMPONENT_NAME); if (squishStoppers.Contains(colComp.colliderType)) { upperSide = true; break; } } } foreach (Entity ent in lowerCollisions) { if (ent.hasComponent(GlobalVars.COLLIDER_COMPONENT_NAME)) { ColliderComponent colComp = ( ColliderComponent )ent.getComponent(GlobalVars.COLLIDER_COMPONENT_NAME); if (squishStoppers.Contains(colComp.colliderType)) { lowerSide = true; break; } } } if (!upperSide && !lowerSide) { squishHeightCenter(posComp, squishComp, -squishComp.xEaseRate); } if (upperSide && !lowerSide) { squishDown(posComp, squishComp, -squishComp.xEaseRate); } if (!upperSide && lowerSide) { squishUp(posComp, squishComp, -squishComp.xEaseRate); } //Don't overcompensate if (posComp.height > squishComp.baseHeight) { level.getMovementSystem().changeHeight(posComp, squishComp.baseHeight); } } else if (posComp.height > squishComp.baseHeight) { level.getMovementSystem().changeHeight(posComp, posComp.height - squishComp.yEaseRate); if (posComp.height < squishComp.baseHeight) { level.getMovementSystem().changeHeight(posComp, squishComp.baseHeight); } } }
public void drawEntity(Entity e) { //Pull out all required components PositionComponent posComp = ( PositionComponent )e.getComponent(GlobalVars.POSITION_COMPONENT_NAME); DrawComponent drawComp = ( DrawComponent )e.getComponent(GlobalVars.DRAW_COMPONENT_NAME); if (e is ShooterBullet) { //Console.WriteLine( "In Draw ShooterBullet - Redraw: " + drawComp.needRedraw + ", In View: " + isInView( posComp ) ); } if (drawComp.needRedraw) { if (isInView(posComp)) { if (g != null) { Image img = null; //If size is locked, don't resize the image. if (drawComp.sizeLocked && wRatio == 1 && hRatio == 1) { img = drawComp.getImage(); } else { Size imageSize = new Size(( int )(posComp.width * wRatio), ( int )(posComp.height * hRatio)); img = new Bitmap(drawComp.getImage(), imageSize); } PointF drawPoint = posComp.getLocAsPoint(); drawPoint.X -= (posComp.width / 2.0f); drawPoint.Y -= (posComp.height / 2.0f); lock ( img ) { g.DrawImageUnscaled(img, new Point(( int )drawPoint.X, ( int )drawPoint.Y)); //Draw the image to the view } //Health bar if need be if (e.hasComponent(GlobalVars.HEALTH_COMPONENT_NAME)) { HealthComponent healthComp = ( HealthComponent )e.getComponent(GlobalVars.HEALTH_COMPONENT_NAME); if (healthComp.healthBar && (healthComp.showBarOnFull || !healthComp.hasFullHealth())) { int barHeight = 4; int ySpace = 3; int xSpace = 0; int xLoc = (( int )Math.Round(posComp.x - posComp.width / 2) + xSpace); int yLoc = (( int )Math.Round(posComp.y - posComp.height / 2) - barHeight - ySpace); int fullWidth = (( int )Math.Round(posComp.width) - 2 * xSpace); Rectangle backRect = new Rectangle(xLoc, yLoc, fullWidth, barHeight); Rectangle foreRect = new Rectangle(xLoc, yLoc, ( int )Math.Round(fullWidth * healthComp.getHealthPercentage()), barHeight); g.FillRectangle(healthComp.backHealthBarBrush, backRect); g.FillRectangle(healthComp.foreHealthBarBrush, foreRect); g.DrawRectangle(Pens.Black, backRect); //Border } } if (e is BasicGround & bkgEnt != null) { DrawComponent bkgDraw = ( DrawComponent )bkgEnt.getComponent(GlobalVars.DRAW_COMPONENT_NAME); Graphics graph = Graphics.FromImage(bkgDraw.getImage()); lock ( img ) { graph.DrawImageUnscaled(img, new Point(( int )drawPoint.X, ( int )drawPoint.Y)); //Draw the image to the view } drawComp.needRedraw = false; } } } if (GlobalVars.debugBoxes) { g.DrawRectangle(Pens.Blue, posComp.x - posComp.width / 2, posComp.y - posComp.height / 2, posComp.width, posComp.height); if (e.hasComponent(GlobalVars.COLLIDER_COMPONENT_NAME)) { ColliderComponent colComp = (ColliderComponent)e.getComponent(GlobalVars.COLLIDER_COMPONENT_NAME); g.DrawRectangle(Pens.Red, colComp.getX(posComp) - colComp.width / 2, colComp.getY(posComp) - colComp.height / 2, colComp.width, colComp.height); } } } }
//Check to see if only transparent pixels are overlapping. public bool handleTransparentCollision(float x1, float y1, Entity e1, Entity e2) { ColliderComponent firstCol = ( ColliderComponent )e1.getComponent(GlobalVars.COLLIDER_COMPONENT_NAME); ColliderComponent secondCol = ( ColliderComponent )e2.getComponent(GlobalVars.COLLIDER_COMPONENT_NAME); PositionComponent firstPos = ( PositionComponent )e1.getComponent(GlobalVars.POSITION_COMPONENT_NAME); PositionComponent secondPos = ( PositionComponent )e2.getComponent(GlobalVars.POSITION_COMPONENT_NAME); //Something's missing a required component if (firstCol == null || secondCol == null || firstPos == null || secondPos == null) { Console.WriteLine("Missing Required Component for handling collision between: " + e1 + " and " + e2); return(false); } int minAlpha = 50; int pixelBuffer = 0; //Extra pixels to check on either side Bitmap firstImg = null; Bitmap secondImg = null; if (firstCol.drawComponent != null) { if (firstCol.drawComponent.getImage() != null) { firstImg = ( Bitmap )firstCol.drawComponent.getImage(); } } if (secondCol.drawComponent != null) { if (secondCol.drawComponent.getImage() != null) { secondImg = ( Bitmap )secondCol.drawComponent.getImage(); } } bool alwaysCollide1 = false; bool alwaysCollide2 = false; if (firstImg == null) { alwaysCollide1 = firstCol.collideOnNoSprite; } else { alwaysCollide1 = !firstCol.hasTransparentPixels; //Resize as needed if (!firstCol.drawComponent.sizeLocked) { Bitmap newImg1 = new Bitmap(firstImg, new Size(( int )firstPos.width, ( int )firstPos.height)); firstImg = newImg1; } } if (secondImg == null) { alwaysCollide2 = secondCol.collideOnNoSprite; } else { alwaysCollide2 = !secondCol.hasTransparentPixels; //Resize if needed if (!secondCol.drawComponent.sizeLocked) { Bitmap newImg2 = new Bitmap(secondImg, new Size(( int )secondPos.width, ( int )secondPos.height)); secondImg = newImg2; } } //If they're both always going to collide, just return true. No point wasting time checking. if (alwaysCollide1 && alwaysCollide2) { return(true); } //Pixel locations to check int left1 = 0; int left2 = 0; int up1 = 0; int up2 = 0; int hOverlap = 0; int vOverlap = 0; //Edges of each entitiy float leftEdge1 = x1 - firstPos.width / 2; float leftEdge2 = secondPos.x - secondPos.width / 2; float rightEdge1 = x1 + firstPos.width / 2; float rightEdge2 = secondPos.x + secondPos.width / 2; float upperEdge1 = y1 - firstPos.height / 2; float upperEdge2 = secondPos.y - secondPos.height / 2; float lowerEdge1 = y1 + firstPos.height / 2; float lowerEdge2 = secondPos.y + secondPos.height / 2; if (leftEdge1 <= leftEdge2) { left2 = 0; left1 = ( int )leftEdge2 - ( int )leftEdge1; if (rightEdge1 <= rightEdge2) { hOverlap = ( int )rightEdge1 - ( int )leftEdge2; } else { hOverlap = ( int )rightEdge2 - ( int )leftEdge2; } } else { left1 = 0; left2 = ( int )leftEdge1 - ( int )leftEdge2; if (rightEdge1 <= rightEdge2) { hOverlap = ( int )rightEdge1 - ( int )leftEdge1; } else { hOverlap = ( int )rightEdge2 - ( int )leftEdge1; } } if (upperEdge1 <= upperEdge2) { up2 = 0; up1 = ( int )upperEdge2 - ( int )upperEdge1; if (lowerEdge1 <= lowerEdge2) { vOverlap = ( int )lowerEdge1 - ( int )upperEdge2; } else { vOverlap = ( int )lowerEdge2 - ( int )upperEdge2; } } else { up1 = 0; up2 = ( int )upperEdge1 - ( int )upperEdge2; if (lowerEdge1 <= lowerEdge2) { vOverlap = ( int )lowerEdge1 - ( int )upperEdge1; } else { vOverlap = ( int )lowerEdge2 - ( int )upperEdge1; } } //Loop through and see if any non-transparent pixels overlap for (int i = -pixelBuffer; i < hOverlap - 1 + pixelBuffer; i++) { for (int j = -pixelBuffer; j < vOverlap - 1 + pixelBuffer; j++) { //Which pixels to check int xSpot1 = left1 + i; int xSpot2 = left2 + i; int ySpot1 = up1 + j; int ySpot2 = up2 + j; if (xSpot1 < 0 || xSpot2 < 0 || ySpot1 < 0 || ySpot2 < 0) { continue; } if (!(firstImg == null) && (xSpot1 >= firstImg.Width || ySpot1 >= firstImg.Height)) { continue; } if (!(secondImg == null) && (xSpot2 >= secondImg.Width || ySpot2 >= secondImg.Height)) { continue; } //Check the pixel at each spot, if both are non-transparent, collision. if ((alwaysCollide1 || (firstImg != null && firstImg.GetPixel(xSpot1, ySpot1).A >= minAlpha)) && (alwaysCollide2 || (secondImg != null && secondImg.GetPixel(xSpot2, ySpot2).A >= minAlpha))) { return(true); } } } return(false); }
/// <summary> /// Constructor for MapStruct. /// </summary> /// <param name="collider">Reference to collider.</param> public MapStruct(ColliderComponent collider) { Collider = collider; }
/// <summary> /// Инициализация класса /// </summary> public override void Init() { _colliderComponent = GameObject.GetComponent <ColliderComponent>(); }
//Run once each frame deltaTime is the amount of seconds since the last frame public override void Update(float deltaTime) { foreach (Entity e in getApplicableEntities()) { //Don't apply gravity if the object is on top of something PositionComponent posComp1 = ( PositionComponent )e.getComponent(GlobalVars.POSITION_COMPONENT_NAME); ColliderComponent colComp1 = ( ColliderComponent )e.getComponent(GlobalVars.COLLIDER_COMPONENT_NAME); float sideBuffer = -1; float floorBuffer = 1; //Distance it checks below object for the ground float e1X = posComp1.x; float e1Y = posComp1.y; float e1Width = posComp1.width; float e1Height = posComp1.height; //If it has a collider, use its Width and Height instead. if (colComp1 != null) { e1Width = colComp1.width; e1Height = colComp1.height; } if (e1Width != posComp1.width) { float diff = (posComp1.width - e1Width); e1X += diff / 2; } if (e1Height != posComp1.height) { float diff = (posComp1.height - e1Height); e1Y += diff / 2; } float leftX = (e1X - e1Width / 2 - sideBuffer); float rightX = (e1X + e1Width / 2 + sideBuffer); float lowerY = (e1Y + e1Height / 2 + floorBuffer); //Console.WriteLine("Lower y: " + lowerY); //List<Entity> cols = level.getCollisionSystem().checkForCollision(e, posComp.x, lowerY, posComp.width, posComp.height); List <Entity> cols = level.getCollisionSystem().findObjectsBetweenPoints(leftX, lowerY, rightX, lowerY); bool shouldApplyGravity = true; //False if there's a solid object below foreach (Entity ent in cols) { ColliderComponent colComp2 = ( ColliderComponent )ent.getComponent(GlobalVars.COLLIDER_COMPONENT_NAME); //If the object is below e, and it's solid, don't apply gravity. if (colComp2.colliderType == GlobalVars.BASIC_SOLID_COLLIDER_TYPE) { PositionComponent posComp2 = ( PositionComponent )ent.getComponent(GlobalVars.POSITION_COMPONENT_NAME); //Separated out for easy changing. float e2Y = posComp2.y; float e2Height = colComp2.height; //Center width/height values if (e2Height != posComp2.height) { float diff = (posComp2.height - e2Height); e2Y += diff / 2; } float newY = e1Y - e2Height / 2 - e1Height / 2; if (moveToContactWhenTouchGround && Math.Abs(e1Y - newY) > 0.1) { level.getMovementSystem().changePosition(posComp1, e1X, newY, false, true); } shouldApplyGravity = false; break; } } if (shouldApplyGravity) { //Pull out all required components VelocityComponent velComp = ( VelocityComponent )e.getComponent(GlobalVars.VELOCITY_COMPONENT_NAME); GravityComponent gravComp = ( GravityComponent )e.getComponent(GlobalVars.GRAVITY_COMPONENT_NAME); //Add the x and y gravity to their respective velocity components velComp.incVelocity(gravComp.x * deltaTime, gravComp.y * deltaTime); //Console.WriteLine("Gravity for " + e + " - X Vel : " + velComp.x + " - Y Vel - " + velComp.y); } } }