private void SolveVert() { Vector2i posi = new Vector2i((int)Pos.X, (int)Pos.Y); Vector2i vertDest = new Vector2i(posi.X, posi.Y + (int)Velocity.Y); //if we're to move at all if (vertDest.Y != posi.Y) { int direction = Math.Sign(vertDest.Y - posi.Y); CollideTypes type = (SoftDrop || direction == -1) ? CollideTypes.Hard : CollideTypes.HardOrSoft; bool hitSomething = true; int y = posi.Y; while (!dm.MapCollide(posi.X, y, type)) { if (y == vertDest.Y) { hitSomething = false; break; } y += direction; } if (hitSomething) { Velocity.Y = 0; y -= direction; } Pos.Y = y; } }
private void UpdatePosition() { //increase velocity by gravity, up to a maximum Velocity.Y += dm.gravity; if (Velocity.Y > dm.gravity * 12) { Velocity.Y = dm.gravity * 12; } Speed = 2; if (Input.isKeyDown(Keyboard.Key.LShift)) { Speed = 3f; } //horizontal decay, up to a maximum Velocity.X *= .75f; Velocity.X = Helper.ClampSigned(Speed, Velocity.X); //truncate tiny velocities if (Math.Abs(Velocity.X) < 1) { Velocity.X = 0; } //determine whether we're dropping through platforms CollideTypes type = SoftDrop ? CollideTypes.Hard : CollideTypes.HardOrSoft; //we do the horizontal and vertical movements separately SolveHoriz(); SolveVert(); }
private void UpdateOnGround() { Vector2i posi = new Vector2i((int)Pos.X, (int)Pos.Y); Vector2i below = posi + new Vector2i(0, 1); CollideTypes type = SoftDrop ? CollideTypes.Hard : CollideTypes.HardOrSoft; OnGround = dm.MapCollide(below.X, below.Y, type); //also set jumps. if just walked off a cliff, take away first jump if (OnGround) { JumpsLeft = MaxJumps; } else if (JumpsLeft == MaxJumps) { JumpsLeft = MaxJumps - 1; } }
public bool MapCollide(int x, int y, CollideTypes types) { //check if OOB if (x < 0 || x >= mapWidth || y < 0 || y >= mapHeight) { return(false); } int index = (y * mapWidth + x) * 4; byte r = mapBytes[index]; byte g = mapBytes[index + 1]; byte b = mapBytes[index + 2]; byte a = mapBytes[index + 3]; //only collide if black or green if (types.HasFlag(CollideTypes.Hard) && a == 255 && r == 0 && g == 0 && b == 0) { return(true); } if (types.HasFlag(CollideTypes.Soft) && a == 255 && r == 0 && g == 255 && b == 0) { return(true); } return(false); }
public bool MapCollide(int x, int y, CollideTypes types) { //check if OOB if (x < 0 || x >= mapWidth || y < 0 || y >= mapHeight) { return false; } int index = (y * mapWidth + x) * 4; byte r = mapBytes[index]; byte g = mapBytes[index + 1]; byte b = mapBytes[index + 2]; byte a = mapBytes[index + 3]; //only collide if black or green if (types.HasFlag(CollideTypes.Hard) && a == 255 && r == 0 && g == 0 && b == 0) return true; if (types.HasFlag(CollideTypes.Soft) && a == 255 && r == 0 && g == 255 && b == 0) return true; return false; }