From 3e718b5fedc1352b729fe5757aaf65321bd10e7c Mon Sep 17 00:00:00 2001 From: bluefireoly Date: Sun, 30 Aug 2020 14:35:08 +0200 Subject: [PATCH] Create Circle.kt --- .../kspigot/extensions/geometry/Circle.kt | 91 +++++++++++++++++++ 1 file changed, 91 insertions(+) create mode 100644 src/main/kotlin/net/axay/kspigot/extensions/geometry/Circle.kt diff --git a/src/main/kotlin/net/axay/kspigot/extensions/geometry/Circle.kt b/src/main/kotlin/net/axay/kspigot/extensions/geometry/Circle.kt new file mode 100644 index 00000000..cb0e75c1 --- /dev/null +++ b/src/main/kotlin/net/axay/kspigot/extensions/geometry/Circle.kt @@ -0,0 +1,91 @@ +@file:Suppress("MemberVisibilityCanBePrivate") + +package net.axay.kspigot.extensions.geometry + +import net.axay.kspigot.particles.KSpigotParticle +import org.bukkit.Location +import org.bukkit.Material +import org.bukkit.entity.EntityType + +abstract class Circle(val radius: Number) { + + val fillLocations: Set by lazy { + + val locationList: MutableSet = HashSet() + + var currentRadius = radius.toDouble() + while (currentRadius >= 0) { + + var d = -currentRadius + var x = currentRadius + var y = 0 + + while (y <= x) { + + locationList.addCircleLoc(x, y) + locationList.addCircleLoc(x, -y) + locationList.addCircleLoc(-x, y) + locationList.addCircleLoc(-x, -y) + locationList.addCircleLoc(y, x) + locationList.addCircleLoc(y, -x) + locationList.addCircleLoc(-y, x) + locationList.addCircleLoc(-y, -x) + + d += 2 * y + 1 + y++ + + if (d > 0) { + d += -2 * 3 + 2 + x-- + } + + } + + currentRadius-- + + } + + return@lazy locationList + + } + + private fun MutableSet.addCircleLoc(first: Number, second: Number) { + this += SimpleLocation2D(first, second) + } + + final fun buildAtX(loc: Location) { + for (it in fillLocations) + setAt(Location(loc.world, loc.x, loc.y + it.x.toDouble(), loc.z + it.x.toDouble())) + } + + final fun buildAtY(loc: Location) { + for (it in fillLocations) + setAt(Location(loc.world, loc.x + it.x.toDouble(), loc.y, loc.z + it.x.toDouble())) + } + + final fun buildAtZ(loc: Location) { + for (it in fillLocations) + setAt(Location(loc.world, loc.x + it.x.toDouble(), loc.y + it.x.toDouble(), loc.z)) + } + + abstract fun setAt(loc: Location) + +} + +class BlockCircle(radius: Number, val material: Material) : Circle(radius) { + override fun setAt(loc: Location) { + loc.block.type = material + } +} + +class ParticleCircle(radius: Number, val particle: KSpigotParticle) : Circle(radius) { + override fun setAt(loc: Location) { + particle.spawnAt(loc) + } +} + +class EntityCircle(radius: Number, val entityType: EntityType) : Circle(radius) { + override fun setAt(loc: Location) { + loc.world?.spawnEntity(loc, entityType) ?: throw IllegalArgumentException("The world of the given location is null!") + } +} \ No newline at end of file