Create Circle.kt

This commit is contained in:
bluefireoly
2020-08-30 14:35:08 +02:00
parent 96986d947e
commit 3e718b5fed

View File

@@ -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<SimpleLocation2D> by lazy {
val locationList: MutableSet<SimpleLocation2D> = 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<SimpleLocation2D>.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!")
}
}