public void OnCollide(CollideResult result) { throw new NotImplementedException(); }
protected override void OnCollide(CollideResult col) => Destroy(gameObject);
public void Move(Vector2 accel, float gravity) { if (disabled) { return; } facingLockTime -= Time.deltaTime; KillOnOverlap(); moveState = MoveState.Normal; accel *= speed; accel += velocity * friction; if (gravity != 0.0f) { accel.y = gravity; } // Using the following equations of motion: // - p' = 1/2at^2 + vt + p. // - v' = at + v. // - a = specified by input. // Where a = acceleration, v = velocity, and p = position. // v' and p' denote new versions, while non-prime denotes old. // These are found by integrating up from acceleration to velocity. Use derivation // to go from position down to velocity and then down to acceleration to see how // we can integrate back up. Vector2 delta = accel * 0.5f * Utils.Square(Time.deltaTime) + velocity * Time.deltaTime; if (swimming) { velocity = (accel * 0.35f) * Time.deltaTime + velocity; } else { velocity = accel * Time.deltaTime + velocity; } Vector2 target = Position + delta; AABB entityBB = GetBoundingBox(); colFlags = CollideFlags.None; // Player size in tiles. Vector2Int tSize = Utils.CeilToInt(entityBB.radius * 2.0f); Vector2Int start = Utils.TilePos(Position); Vector2Int end = Utils.TilePos(target); // Compute the range of tiles we could touch with our movement. We'll test for collisions // with the tiles in this range. Vector2Int min = new Vector2Int(Mathf.Min(start.x, end.x) - tSize.x, Mathf.Min(start.y, end.y) - tSize.y); Vector2Int max = new Vector2Int(Mathf.Max(start.x, end.x) + tSize.x, Mathf.Max(start.y, end.y) + tSize.y); // Tile collision checking. GetPossibleCollidingTiles(world, entityBB, min, max); // Entity collision checking. GetPossibleCollidingEntities(world, entityBB, min, max); possibleCollides.Sort(collideCompare); float tRemaining = 1.0f; for (int it = 0; it < 3 && tRemaining > 0.0f; ++it) { float tMin = 1.0f; Vector2 normal = Vector2.zero; CollideResult hitResult = default; bool hit = false; for (int i = 0; i < possibleCollides.Count; ++i) { CollideResult info = possibleCollides[i]; bool result = TestTileCollision(world, entityBB, info.bb, delta, ref tMin, ref normal); if (result) { hitResult = info; hit = true; } } MoveBy(delta * tMin); if (normal != Vector2.zero) { MoveBy((normal * Epsilon)); } if (hit) { OnCollide(hitResult); } entityBB = GetBoundingBox(); // Subtract away the component of the velocity that collides with the tile wall // and leave the remaining velocity intact. velocity -= Vector2.Dot(velocity, normal) * normal; delta -= Vector2.Dot(delta, normal) * normal; delta -= (delta * tMin); tRemaining -= (tMin * tRemaining); } possibleCollides.Clear(); if (overlaps.Count > 0) { HandleOverlaps(overlaps); } overlaps.Clear(); SetFacingDirection(); Rebase(world); ApplyOverTimeDamage(); if (DebugServices.Instance.ShowDebug) { GetBoundingBox().Draw(Color.green); } }
public void OnCollide(CollideResult result) { if (result.Type == CollisionType.Side) { int teleport = 0; bool ifContinue = false; if (lastKey == Keys.W && ifContinue == false) { for (int i = 1; i <= 31; i++) { if (level.GetTile(i * 16, Height * 31).Type == TileType.Way) { if (teleport == 0) { teleport = i * 16; } if (Math.Abs(teleport - X) > Math.Abs(i * 16 - X)) { teleport = i * 16; } } } X = teleport; Y = Height * 31; ifContinue = true; renderer.DoRender(); } if (lastKey == Keys.S && ifContinue == false) { for (int i = 1; i <= 31; i++) { if (level.GetTile(i * 16, 0).Type == TileType.Way) { if (teleport == 0) { teleport = i * 16; } if (Math.Abs(teleport - X) > Math.Abs(i * 16 - X)) { teleport = i * 16; } } } X = teleport; Y = 0; ifContinue = true; renderer.DoRender(); } if (lastKey == Keys.D && ifContinue == false) { for (int i = 1; i <= 31; i++) { if (level.GetTile(0, i * 16).Type == TileType.Way) { if (teleport == 0) { teleport = i * 16; } if (Math.Abs(teleport - Y) > Math.Abs(i * 16 - Y)) { teleport = i * 16; } } } Y = teleport; X = 0; ifContinue = true; renderer.DoRender(); } if (lastKey == Keys.A && ifContinue == false) { for (int i = 0; i <= 31; i++) { if (level.GetTile(Width * 31, i * 16).Type == TileType.Way) { if (teleport == 0) { teleport = i * 16; } if (Math.Abs(teleport - Y) > Math.Abs(i * 16 - Y)) { teleport = i * 16; } } } Y = teleport; X = Width * 31; ifContinue = true; renderer.DoRender(); } } }
protected virtual void OnCollide(CollideResult result) { }