//Picks either the upper, lower, left, or right sides for the grapple to attach to. public PointF getGrappleAttachPoint(Entity attachEnt, GrappleComponent grapComp) { PositionComponent attachPosComp = ( PositionComponent )attachEnt.getComponent(GlobalVars.POSITION_COMPONENT_NAME); PointF attachPos = attachPosComp.getLocAsPoint(); PointF attachSize = attachPosComp.getSizeAsPoint(); if (attachEnt.hasComponent(GlobalVars.COLLIDER_COMPONENT_NAME)) { ColliderComponent colComp = ( ColliderComponent )attachEnt.getComponent(GlobalVars.COLLIDER_COMPONENT_NAME); attachPos = colComp.getLocationAsPoint(attachPosComp); attachSize = colComp.getSizeAsPoint(); } PointF grapPos = grapComp.getLastPoint(); PointF left = new PointF(attachPos.X - attachSize.X / 2, attachPos.Y); PointF right = new PointF(attachPos.X + attachSize.X / 2, attachPos.Y); PointF up = new PointF(attachPos.X, attachPos.Y - attachSize.Y / 2); PointF down = new PointF(attachPos.X, attachPos.Y + attachSize.Y / 2); double distLeft = getDist(grapPos, left); double distRight = getDist(grapPos, right); double distUp = getDist(grapPos, up); double distDown = getDist(grapPos, down); double minA = Math.Min(distLeft, distRight); double minB = Math.Min(distUp, distDown); double min = Math.Min(minA, minB); if (min == distLeft) { return(left); } else if (min == distRight) { return(right); } else if (min == distUp) { return(up); } else if (min == distDown) { return(down); } else { Console.WriteLine("Error picking point for grapple attachment!"); return(attachPos); } }
/*public bool checkForStopsBetweenPoints( PointF p1, PointF p2 ) { * List<Entity> cols = level.getCollisionSystem().findObjectsBetweenPoints( p1.X, p1.Y, p2.X, p2.Y ); * if ( cols.Count > 0 ) { * foreach ( Entity e in cols ) { * ColliderComponent col = ( ColliderComponent )e.getComponent( GlobalVars.COLLIDER_COMPONENT_NAME ); * if ( grappleColliders.Contains( col.colliderType ) ) { * return true; * } * } * } * return false; * }*/ public Entity checkForStopsBetweenPoints(PointF p1, PointF p2, GrappleComponent grap) { List <Entity> cols = level.getCollisionSystem().findObjectsBetweenPoints(p1.X, p1.Y, p2.X, p2.Y); double minDist = Double.MaxValue; Entity minEnt = null; if (cols.Contains(level.getPlayer())) { cols.Remove(level.getPlayer()); } if (cols.Count > 0) { //Console.WriteLine( "Between " + p1 + " and " + p2 ); foreach (Entity e in cols) { ColliderComponent col = ( ColliderComponent )e.getComponent(GlobalVars.COLLIDER_COMPONENT_NAME); if (grappleColliders.Contains(col.colliderType)) { PositionComponent entPos = ( PositionComponent )e.getComponent(GlobalVars.POSITION_COMPONENT_NAME); double dist = getDist(grap.getLastPoint(), col.getLocationAsPoint(entPos)); //Console.Write( dist ); if (dist < minDist) { minEnt = e; minDist = dist; //Console.Write( "! " ); } else { //Console.Write( " " ); } } } //Console.WriteLine( "Min: " + minDist ); } return(minEnt); }
//You must have an Update. public override void Update(float deltaTime) { bool loopWasEntered = false; foreach (Entity e in getApplicableEntities()) //There should only ever be 0 or 1 { loopWasEntered = true; GrappleComponent grapComp = ( GrappleComponent )e.getComponent(GlobalVars.GRAPPLE_COMPONENT_NAME); //Growing if (grapComp.state == 0 && level.getPlayer() != null) { float newX = grapComp.getLastPoint().X; float newY = grapComp.getLastPoint().Y; //Check if the player has moved. If so move the start point of the grapple. PositionComponent playerPos = ( PositionComponent )level.getPlayer().getComponent(GlobalVars.POSITION_COMPONENT_NAME); if (playerPos.getLocAsPoint() != grapComp.getFirstPoint()) { //Move start point grapComp.setFirstPoint(playerPos.getLocAsPoint()); //Check to see if it's intersecting anything now Entity mrIntersection = checkForStopsBetweenPoints(grapComp.getFirstPoint(), grapComp.getLastPoint(), grapComp); if (mrIntersection != null) { //If it DID intersect something, destroy the grapple. //finishGrapple(e, false); //AKSHUALLY connect the grapple to the new thing PositionComponent pos = ( PositionComponent )mrIntersection.getComponent(GlobalVars.POSITION_COMPONENT_NAME); PointF setPos = getGrappleAttachPoint(mrIntersection, grapComp); //grapComp.setEndPoint( pos.getLocAsPoint() ); grapComp.setEndPoint(setPos); grapComp.state = 1; return; } } // If no collision - progress forwards! //h = speed*time //x = h*cos(theta) //y = h*sin(theta) float h = growSpeed * deltaTime; newX += h * ( float )Math.Cos(grapComp.direction); newY += h * ( float )Math.Sin(grapComp.direction); System.Drawing.PointF p = new System.Drawing.PointF(newX, newY); //Don't allow it to go off the sides of the screen if (newX < -h || newY < -h || newX > level.levelWidth + h || newY > level.levelHeight + h) { grapComp.state = 2; return; } //check if it's past the max grapple distance if (getDist(grapComp.getFirstPoint(), grapComp.getLastPoint()) > GlobalVars.MAX_GRAPPLE_DISTANCE) { grapComp.state = 2; //Retreat! return; } PointF p1 = new PointF(newX, newY); //Check if it's done grappling //colEnt is the entity which the grapple latches onto Entity colEnt = checkForStopsBetweenPoints(grapComp.getLastPoint(), p1, grapComp); if (colEnt != null) { PositionComponent pos = ( PositionComponent )colEnt.getComponent(GlobalVars.POSITION_COMPONENT_NAME); PointF setPos = getGrappleAttachPoint(colEnt, grapComp); //grapComp.setEndPoint( pos.getLocAsPoint() ); grapComp.setEndPoint(setPos); grapComp.myLink = colEnt; grapComp.state = 1; if (removeGravity == 2) { level.getPlayer().removeComponent(GlobalVars.GRAVITY_COMPONENT_NAME); } return; } //Not colliding with anything, set the end point to the calculated point. grapComp.setEndPoint(p); } //Following else if (grapComp.state == 1 && level.getPlayer() != null) { if (level.getPlayer() == null) { level.removeEntity(e); return; //No player, nothing to do! } //Get player's position component PositionComponent playerPos = ( PositionComponent )level.getPlayer().getComponent(GlobalVars.POSITION_COMPONENT_NAME); /* * //Check to see if it's intersecting anything mid-way * Entity mrIntersection = null; * if ( checkForStopsBetweenPointsExclude( grapComp.getFirstPoint(), grapComp.getLastPoint(), grapComp.myLink, ref mrIntersection ) ) { * //If it DID intersect something, destroy the grapple. * //finishGrapple(e, false); * //return; * PositionComponent pos = ( PositionComponent )mrIntersection.getComponent( GlobalVars.POSITION_COMPONENT_NAME ); * grapComp.setEndPoint( pos.getLocAsPoint() ); * } * * * //Check if, for whatever reason, the player wasn't able to move. * float buff = 0.5f; * buff = GlobalVars.MIN_TILE_SIZE; * if ( Math.Abs( playerPos.x - grapComp.getFirstPoint().X ) > buff || Math.Abs( playerPos.y - grapComp.getFirstPoint().Y ) > buff ) { * Console.WriteLine( "Finish 1" ); * //finishGrapple( e, true ); * grapComp.state = 2; * stopPlayer = true; * return; * } */ double distBefore = getDist(playerPos.getLocAsPoint(), grapComp.getLastPoint()); //Move first point in grapple up float newX = grapComp.getFirstPoint().X; float newY = grapComp.getFirstPoint().Y; float h = followSpeed * deltaTime; newX += h * ( float )Math.Cos(grapComp.direction); newY += h * ( float )Math.Sin(grapComp.direction); System.Drawing.PointF p = new System.Drawing.PointF(newX, newY); //This checks if the next point is near final point in the grapple. //If so, remove the grapple and break. ColliderComponent playerCol = ( ColliderComponent )level.getPlayer().getComponent(GlobalVars.COLLIDER_COMPONENT_NAME); float buffer = GlobalVars.MIN_TILE_SIZE; if (playerCol != null) { buffer += playerCol.width / 1.5f; } else { buffer += playerPos.width / 1.5f; } if (Math.Abs(newX - grapComp.getLastPoint().X) <= buffer && Math.Abs(newY - grapComp.getLastPoint().Y) <= buffer) { //Console.WriteLine( "Finish 3!" ); //finishGrapple( e, true ); grapComp.state = 2; stopPlayer = true; return; } //Move player level.getMovementSystem().changePosition(playerPos, p.X, playerPos.y, false, false); level.getMovementSystem().changePosition(playerPos, playerPos.x, p.Y, false, false); grapComp.setFirstPoint(playerPos.getLocAsPoint()); //Check to make sure the player isn't getting further away. If he is, sthap! double nowDist = getDist(grapComp.getFirstPoint(), grapComp.getLastPoint()); if (nowDist >= distBefore) { //Console.WriteLine( "Finish 2!" ); //finishGrapple( e, true ); grapComp.state = 2; stopPlayer = true; return; } } //Retreating else if (grapComp.state == 2 && level.getPlayer() != null) { PositionComponent playerPos = ( PositionComponent )level.getPlayer().getComponent(GlobalVars.POSITION_COMPONENT_NAME); grapComp.setFirstPoint(playerPos.getLocAsPoint()); double distBefore = getDist(grapComp.getFirstPoint(), grapComp.getLastPoint()); float newX = grapComp.getLastPoint().X; float newY = grapComp.getLastPoint().Y; float h = retreatSpeed * deltaTime; if (h > distBefore) { h = ( float )distBefore; } //Calculate the direction double dir = Math.Atan((grapComp.getFirstPoint().Y - newY) / (grapComp.getFirstPoint().X - newX)); if (grapComp.getFirstPoint().X < newX) { dir += Math.PI; } newX += h * ( float )Math.Cos(dir); newY += h * ( float )Math.Sin(dir); System.Drawing.PointF p = new System.Drawing.PointF(newX, newY); //Make sure it isn't getting longer if (getDist(p, grapComp.getFirstPoint()) > distBefore) { finishGrapple(e); return; } //Check if it's fully retreated - if so, delete the grapple! float buffer = GlobalVars.MIN_TILE_SIZE; if (Math.Abs(newX - grapComp.getFirstPoint().X) <= buffer && Math.Abs(newY - grapComp.getFirstPoint().Y) <= buffer) { finishGrapple(e); return; } grapComp.setEndPoint(p); } } isGrappling = loopWasEntered; }
public void Draw(Graphics mainG, List <Entity> entities) { //g.FillRectangle(bkgBrush, new Rectangle(0, 0, (int)width, (int)height)); //Clear if (!(level is RunningGame.Level_Editor.CreationLevel)) { if (!hasDecreasedQuality) { g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.NearestNeighbor; // or NearestNeighbour g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.None; g.PixelOffsetMode = System.Drawing.Drawing2D.PixelOffsetMode.None; g.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighSpeed; g.TextRenderingHint = System.Drawing.Text.TextRenderingHint.SingleBitPerPixel; mainG.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.NearestNeighbor; // or NearestNeighbour mainG.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.None; mainG.PixelOffsetMode = System.Drawing.Drawing2D.PixelOffsetMode.None; mainG.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighSpeed; mainG.TextRenderingHint = System.Drawing.Text.TextRenderingHint.SingleBitPerPixel; hasDecreasedQuality = true; } //Find background Entity first if need be if (bkgEnt == null) { foreach (Entity e in entities) { if (e is BackgroundEntity) { bkgEnt = ( BackgroundEntity )e; //Find background entity } } } if (staticObjImg == null /*|| redrawStatics*/) { if (seperateStaticObjImage) { staticObjImg = new Bitmap(( int )Math.Ceiling(level.levelWidth), ( int )Math.Ceiling(level.levelHeight)); } else { foreach (Entity e in entities) { if (e is BackgroundEntity) { if (bkgEnt == null) { bkgEnt = ( BackgroundEntity )e; //Find background entity } DrawComponent bkgDraw = ( DrawComponent )bkgEnt.getComponent(GlobalVars.DRAW_COMPONENT_NAME); staticObjImg = ( Bitmap )bkgDraw.getImage(); } } } //Draw static entities onto background if (!GlobalVars.fullForegroundImage) { foreach (Entity ent in GlobalVars.groundEntities.Values) { DrawComponent grnDraw = ( DrawComponent )ent.getComponent(GlobalVars.DRAW_COMPONENT_NAME); /*DrawComponent bkgDraw = (DrawComponent)bkgEnt.getComponent(GlobalVars.DRAW_COMPONENT_NAME); * * PositionComponent posComp = (PositionComponent)ent.getComponent(GlobalVars.POSITION_COMPONENT_NAME); * PointF drawPoint = posComp.getPointF(); * drawPoint.X -= (posComp.width / 2.0f); * drawPoint.Y -= (posComp.height / 2.0f); * * Graphics graph = Graphics.FromImage(bkgDraw.getImage());*/ PositionComponent posComp = ( PositionComponent )ent.getComponent(GlobalVars.POSITION_COMPONENT_NAME); PointF drawPoint = posComp.getLocAsPoint(); drawPoint.X -= (posComp.width / 2.0f); drawPoint.Y -= (posComp.height / 2.0f); Graphics graph = Graphics.FromImage(staticObjImg); lock (grnDraw.getImage()) { graph.DrawImageUnscaled(grnDraw.getImage(), new Point(( int )drawPoint.X, ( int )drawPoint.Y)); //Draw the image to the view } grnDraw.needRedraw = false; //redrawStatics = false; } } } //First, if there's a background entity, draw that! if (bkgEnt != null) { drawBkgEntity(bkgEnt); } if (seperateStaticObjImage) { drawStaticObjImage(); } //If there's a grapple, draw it if (level.sysManager != null && level.sysManager.grapSystem.isGrappling) { foreach (Entity e in GlobalVars.nonGroundEntities.Values) { if (e is GrappleEntity) { GrappleComponent grapComp = ( GrappleComponent )e.getComponent(GlobalVars.GRAPPLE_COMPONENT_NAME); PointF start = grapComp.getFirstPoint(); PointF end = grapComp.getLastPoint(); /* * // Calc the pos relative to the view * start.X -= this.x; * start.Y -= this.y; * end.X -= this.x; * end.Y -= this.y; * * start.X *= wRatio; * start.Y *= hRatio; * end.X *= wRatio; * end.Y *= hRatio; */ g.DrawLine(GrapplePen, start, end); break; //Should only be one - this'll save some time. } } } //For all applicable entities (Entities with required components) foreach (Entity e in entities) { if (!(e is BackgroundEntity)) { drawEntity(e); } } if (level.sysManager.drawSystem.drawDebugStuff) { for (int i = 0; i < level.sysManager.drawSystem.debugLines.Count - 1; i += 2) { g.DrawLine(new Pen(Color.Blue, 3), level.sysManager.drawSystem.debugLines[i], level.sysManager.drawSystem.debugLines[i + 1]); } level.sysManager.drawSystem.debugLines.Clear(); int circWidth = 4; for (int i = 0; i < level.sysManager.drawSystem.debugPoints.Count; i++) { g.FillEllipse(Brushes.Green, ( int )Math.Round(level.sysManager.drawSystem.debugPoints[i].X - circWidth / 2), ( int )Math.Round(level.sysManager.drawSystem.debugPoints[i].Y) - circWidth / 2, circWidth, circWidth); } level.sysManager.drawSystem.debugPoints.Clear(); } //mainG.DrawImage(drawImg, new Point((int)displayX, (int)displayY)); //Draw the view to the main window //mainG.DrawImageUnscaled(drawImg, new Point((int)displayX, (int)displayY)); //Draw the view to the main window mainG.DrawImage(drawImg, new RectangleF(displayX, displayY, displayWidth, displayHeight), new RectangleF(x, y, width, height), GraphicsUnit.Pixel); //Draw Border if (this.hasBorder) { if (!this.borderFade) { mainG.DrawRectangle(new Pen(borderBrush, borderSize), new Rectangle(( int )(displayX), ( int )(displayY), ( int )(displayWidth), ( int )(displayHeight))); } else { int alphaDiff = ( int )Math.Ceiling(255.0f / (borderSize - amntSolid)); //How much to decrease alpha per layer //Draw the solid bit //mainG.DrawRectangle(new Pen(borderBrush, amntSolid), new Rectangle((int)(displayX), (int)(displayY), //(int)(displayWidth), (int)(displayHeight))); int alphaVal = 255; alphaVal -= alphaDiff; for (int i = 0; i <= borderSize; i++) { if (alphaVal < 0) { alphaVal = 0; } Color tmpCol = Color.FromArgb(alphaVal, borderBrush.Color); Pen pen = new Pen(new SolidBrush(tmpCol), 1); mainG.DrawRectangle(pen, new Rectangle(( int )(displayX + i), ( int )(displayY + i), ( int )(displayWidth - 2 * i), ( int )(displayHeight - 2 * i))); alphaVal -= alphaDiff; alphaVal -= alphaDiff; if (alphaVal < 0) { alphaVal = 0; } } } } } else { //For all applicable entities (Entities with required components) foreach (Entity e in entities) { drawEntity(e); } mainG.DrawImage(drawImg, new RectangleF(displayX, displayY, displayWidth, displayHeight), new RectangleF(x, y, width, height), GraphicsUnit.Pixel); } }