public bool BuildingInRange(float range) { float distance = Mathf.Sqrt(Mathf.Pow(PlayerMouvements.GetX() - location.x, 2) + Mathf.Pow(PlayerMouvements.GetY() - location.y, 2)); return(distance <= range); }
public override void _Process(float delta) { if (PlayerState.Is(PlayerState.State.Pause)) { return; } Vector2 playerPosition = new Vector2(PlayerMouvements.GetX(), PlayerMouvements.GetY()); List <Building> buildingsNear = new List <Building>(); int x = (int)(playerPosition.x - radius); x = Chunk.size * Mathf.FloorToInt(x / (float)Chunk.size); for (; x <= (int)(playerPosition.x + radius); x += Chunk.size) { Chunk c = World.GetChunk(x); List <Building> l = World.placedBuildingByChunk[c]; foreach (var b in l) { buildingsNear.Add(b); } } RemoveLine(); foreach (var b in buildingsNear) { if (isInRange(b, playerPosition)) { if (Building.buildingGiveEnergy2Player.Contains(b.type)) { TransfereEnergy(b, delta); } } } }
public static void Spawn_Enemy_Randomly() { r = rand.Next(0, 2); positionPlayer = new Vector2(PlayerMouvements.GetX(), PlayerMouvements.GetY()); positionPlayer = Convertion.World2Location(positionPlayer); if (r == 1) { Spawn_Enemy(PlayerMouvements.GetX() + 10, PlayerMouvements.GetY() + 50); } else { Spawn_Enemy(PlayerMouvements.GetX() - 10, PlayerMouvements.GetY() + 50); } }
private void Get_To_Player() { Position_Player = new Vector2(PlayerMouvements.GetX(), PlayerMouvements.GetY()); Position_Player = Convertion.World2Location(Position_Player); Vector2 direction = (-this.GlobalPosition + Position_Player).Normalized(); vel = direction * Speed; if (vel.x >= 0) { Enemy_fly.FlipH = true; } else { Enemy_fly.FlipH = false; } MoveAndSlide(vel, up); }
public override void _PhysicsProcess(float delta) { if (PlayerState.Is(PlayerState.State.Pause)) { return; } if (dead) { return; } if (t >= lifeTime) { QueueFree(); Enemies.enemiesCount--; } if (!verifground) { if (IsOnFloor()) { verifground = true; } vel.x = 0; if (!verifdie) { vel.y = 300; } else { vel.y = 10; } MoveAndSlide(vel, up); } if (verifground) { this.delta = delta; if (!verifattack) { if (!Player_Near()) { if (!veriftime) { time.Start(); veriftime = true; } t += delta; Random_moves(); } else { t = 0; time.Stop(); veriftime = false; Get_To_Player(); } } else { Position_Player = new Vector2(PlayerMouvements.GetX(), PlayerMouvements.GetY()); Position_Player = Convertion.World2Location(Position_Player); Vector2 direction = (-this.GlobalPosition + Position_Player).Normalized(); vel = direction * (-Speed / 5); MoveAndSlide(vel, up); } } Vector2 p = GetViewportTransform().origin *CurrentCamera.GetXZoom(); int viewportSizeX = Mathf.FloorToInt(GetViewport().Size.x *CurrentCamera.GetXZoom()); Vector2 vecMin = Convertion.Location2World(p) * -1; Vector2 vecMax = Convertion.Location2World(new Vector2(p.x * -1 + viewportSizeX, p.y)); if (vecMin.x < 0) { if (!mirrored) { int i = (int)Mathf.Abs(vecMin.x / Chunk.size) + 1; if (Convertion.Location2World(Position).x >= (World.size - i) * Chunk.size) { Position = Position - new Vector2(World.size * Chunk.size * World.BlockTilemap.CellSize.x, 0); mirrored = true; } } else if (-vecMin.x + prev_x_viewport >= 0.90f * World.size * Chunk.size) { int i = (int)Mathf.Abs(vecMin.x / Chunk.size) + 1; if (Convertion.Location2World(Position).x >= (World.size - i) * Chunk.size) { Position = Position - new Vector2(World.size * Chunk.size * World.BlockTilemap.CellSize.x, 0); mirrored = false; } } } else if (vecMax.x >= World.size * Chunk.size) { if (!mirrored) { int i = (int)Mathf.Abs((vecMax.x - World.size * Chunk.size) / Chunk.size) + 1; if (Convertion.Location2World(Position).x <= i * Chunk.size) { Position = Position + new Vector2(World.size * Chunk.size * World.BlockTilemap.CellSize.x, 0); mirrored = true; } } else if (vecMin.x - prev_x_viewport >= 0.90f * World.size * Chunk.size) { int i = (int)Mathf.Abs((vecMax.x - World.size * Chunk.size) / Chunk.size) + 1; if (Convertion.Location2World(Position).x <= i * Chunk.size) { Position = Position + new Vector2(World.size * Chunk.size * World.BlockTilemap.CellSize.x, 0); mirrored = false; } } } else if (vecMax.x < World.size * Chunk.size && vecMin.x >= 0) { if (mirrored) { if (Convertion.Location2World(Position).x < 0) { Position = Position + new Vector2(World.size * Chunk.size * World.BlockTilemap.CellSize.x, 0); } else { Position = Position - new Vector2(World.size * Chunk.size * World.BlockTilemap.CellSize.x, 0); } mirrored = false; } } prev_x_viewport = vecMin.x; }
private bool PositionInRange(int range, Vector2 pos) { float distance = Mathf.Sqrt(Mathf.Pow((pos.x - PlayerMouvements.GetX()), 2) + Mathf.Pow((pos.y - PlayerMouvements.GetY()), 2)); return(distance <= range); }