-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #92 from DockyardMC/feature/guardian-beams
Feature/guardian beams
- Loading branch information
Showing
8 changed files
with
213 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,2 @@ | ||
dockyard.version=0.7.15 | ||
dockyard.version=0.7.16 | ||
kotlin.code.style=official |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
121 changes: 121 additions & 0 deletions
121
src/main/kotlin/io/github/dockyardmc/apis/GuardianBeam.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,121 @@ | ||
package io.github.dockyardmc.apis | ||
|
||
import cz.lukynka.Bindable | ||
import cz.lukynka.BindablePool | ||
import io.github.dockyardmc.entity.EntityManager.despawnEntity | ||
import io.github.dockyardmc.entity.EntityManager.spawnEntity | ||
import io.github.dockyardmc.entity.Guardian | ||
import io.github.dockyardmc.entity.Squid | ||
import io.github.dockyardmc.location.Location | ||
import io.github.dockyardmc.player.Player | ||
import io.github.dockyardmc.runnables.ticks | ||
import io.github.dockyardmc.scheduler.SchedulerTask | ||
import io.github.dockyardmc.scroll.LegacyTextColor | ||
import io.github.dockyardmc.team.TeamCollisionRule | ||
import io.github.dockyardmc.team.TeamManager | ||
import io.github.dockyardmc.utils.Disposable | ||
import io.github.dockyardmc.utils.Viewable | ||
import io.github.dockyardmc.utils.locationLerp | ||
import io.github.dockyardmc.utils.percent | ||
import java.util.UUID | ||
import kotlin.math.round | ||
import kotlin.time.Duration | ||
|
||
class GuardianBeam(start: Location, end: Location): Viewable(), Disposable { | ||
|
||
private companion object { | ||
val NO_COLLISION_TEAM = TeamManager.create("beam_no_collision_${UUID.randomUUID()}", LegacyTextColor.WHITE) | ||
|
||
init { | ||
NO_COLLISION_TEAM.teamCollisionRule.value = TeamCollisionRule.NEVER | ||
} | ||
} | ||
|
||
private val bindablePool = BindablePool() | ||
|
||
val start: Bindable<Location> = bindablePool.provideBindable(start) | ||
val end: Bindable<Location> = bindablePool.provideBindable(end) | ||
|
||
private val guardianEntity: Guardian = start.world.spawnEntity(Guardian(start)) as Guardian | ||
private val targetEntity: Squid = start.world.spawnEntity(Squid(end)) as Squid | ||
|
||
private var currentStartMoveTask: SchedulerTask? = null | ||
private var currentEndMoveTask: SchedulerTask? = null | ||
|
||
override var autoViewable: Boolean = false // doesnt even work | ||
|
||
init { | ||
targetEntity.isInvisible.value = true | ||
guardianEntity.isInvisible.value = true | ||
|
||
targetEntity.team.value = NO_COLLISION_TEAM | ||
guardianEntity.team.value = NO_COLLISION_TEAM | ||
|
||
guardianEntity.target.value = targetEntity | ||
|
||
this.start.valueChanged { change -> | ||
guardianEntity.teleport(change.newValue) | ||
} | ||
|
||
this.end.valueChanged { change -> | ||
targetEntity.teleport(change.newValue) | ||
} | ||
} | ||
|
||
override fun addViewer(player: Player) { | ||
targetEntity.addViewer(player) | ||
guardianEntity.addViewer(player) | ||
} | ||
|
||
override fun removeViewer(player: Player) { | ||
targetEntity.removeViewer(player) | ||
guardianEntity.removeViewer(player) | ||
} | ||
|
||
override fun dispose() { | ||
start.value.world.despawnEntity(targetEntity) | ||
start.value.world.despawnEntity(guardianEntity) | ||
bindablePool.dispose() | ||
} | ||
|
||
fun moveEnd(newLocation: Location, interpolation: Duration = 0.ticks) { | ||
val scheduler = start.value.world.scheduler | ||
val totalTicks = round(interpolation.inWholeMilliseconds / 50f).toInt() | ||
|
||
currentEndMoveTask?.cancel() | ||
|
||
var currentTick = 0 | ||
currentEndMoveTask = scheduler.runRepeating(1.ticks) { | ||
currentTick++ | ||
if(currentTick == totalTicks) currentEndMoveTask?.cancel() | ||
|
||
val time = percent(totalTicks, currentTick) / 100f | ||
val loc = locationLerp(targetEntity.location, newLocation, time) | ||
end.value = loc | ||
} | ||
} | ||
|
||
fun moveStart(newLocation: Location, interpolation: Duration = 0.ticks) { | ||
val scheduler = start.value.world.scheduler | ||
val totalTicks = round(interpolation.inWholeMilliseconds / 50f).toInt() | ||
|
||
currentStartMoveTask?.cancel() | ||
|
||
var currentTick = 0 | ||
currentStartMoveTask = scheduler.runRepeating(1.ticks) { | ||
currentTick++ | ||
if(currentTick == totalTicks) currentStartMoveTask?.cancel() | ||
|
||
val time = percent(totalTicks, currentTick) / 100f | ||
val loc = locationLerp(guardianEntity.location, newLocation, time) | ||
start.value = loc | ||
} | ||
} | ||
|
||
fun cancelMovement() { | ||
currentStartMoveTask?.cancel() | ||
currentEndMoveTask?.cancel() | ||
currentStartMoveTask = null | ||
currentEndMoveTask = null | ||
} | ||
} |
12 changes: 12 additions & 0 deletions
12
src/main/kotlin/io/github/dockyardmc/entity/ElderGuardian.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
package io.github.dockyardmc.entity | ||
|
||
import cz.lukynka.Bindable | ||
import io.github.dockyardmc.location.Location | ||
import io.github.dockyardmc.registry.EntityTypes | ||
import io.github.dockyardmc.registry.registries.EntityType | ||
|
||
class ElderGuardian(location: Location): Guardian(location) { | ||
|
||
override var health: Bindable<Float> = bindablePool.provideBindable(80f) | ||
override var type: EntityType = EntityTypes.ELDER_GUARDIAN | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package io.github.dockyardmc.entity | ||
|
||
import cz.lukynka.Bindable | ||
import io.github.dockyardmc.location.Location | ||
import io.github.dockyardmc.registry.EntityTypes | ||
import io.github.dockyardmc.registry.registries.EntityType | ||
|
||
open class Guardian(location: Location): Entity(location) { | ||
|
||
companion object { | ||
val RETRACTING_SPIKES_METADATA = EntityMetadataType.GUARDIAN_RETRACTING_SPIKES | ||
val TARGET_ENTITY_ID = EntityMetadataType.GUARDIAN_TARGET_ENTITY_ID | ||
} | ||
|
||
override var type: EntityType = EntityTypes.GUARDIAN | ||
override var health: Bindable<Float> = bindablePool.provideBindable(30f) | ||
override var inventorySize: Int = 0 | ||
|
||
val isRetractingSpikes: Bindable<Boolean> = bindablePool.provideBindable(false) | ||
val target: Bindable<Entity?> = bindablePool.provideBindable(null) | ||
|
||
init { | ||
isRetractingSpikes.valueChanged { change -> | ||
metadata[RETRACTING_SPIKES_METADATA] = EntityMetadata(RETRACTING_SPIKES_METADATA, EntityMetaValue.BOOLEAN, change.newValue) | ||
} | ||
|
||
target.valueChanged { change -> | ||
metadata[TARGET_ENTITY_ID] = EntityMetadata(TARGET_ENTITY_ID, EntityMetaValue.VAR_INT, change.newValue?.entityId ?: 0) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
package io.github.dockyardmc.entity | ||
|
||
import cz.lukynka.Bindable | ||
import io.github.dockyardmc.location.Location | ||
import io.github.dockyardmc.registry.EntityTypes | ||
import io.github.dockyardmc.registry.registries.EntityType | ||
|
||
class Squid(location: Location): Entity(location) { | ||
override var type: EntityType = EntityTypes.SQUID | ||
override var health: Bindable<Float> = bindablePool.provideBindable(10f) | ||
override var inventorySize: Int = 0 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters