Skip to content

Commit

Permalink
entity/effect: Expose all effect implementations as variables instead…
Browse files Browse the repository at this point in the history
… of types.

Reduces the {} ugliness of having to create the effect type everywhere.
  • Loading branch information
Sandertv committed Dec 19, 2024
1 parent 3236eac commit b297b65
Show file tree
Hide file tree
Showing 42 changed files with 334 additions and 265 deletions.
8 changes: 4 additions & 4 deletions server/block/beacon.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ import (
"time"
)

// Beacon is a block that projects a light beam skyward, and can provide status effects such as Speed, Jump
// Boost, Haste, Regeneration, Resistance, or Strength to nearby players.
// Beacon is a block that projects a light beam skyward, and can provide status effects such as speed, Jump
// Boost, haste, regeneration, resistance, or strength to nearby players.
type Beacon struct {
solid
transparent
Expand Down Expand Up @@ -153,12 +153,12 @@ func (b Beacon) broadcastBeaconEffects(pos cube.Pos, tx *world.Tx) {
case 0:
primary = nil
case 1:
switch primary.(type) {
switch primary {
case effect.Resistance, effect.JumpBoost, effect.Strength:
primary = nil
}
case 2:
if _, ok := primary.(effect.Strength); ok {
if primary == effect.Strength {
primary = nil
}
case 3:
Expand Down
2 changes: 1 addition & 1 deletion server/block/flower.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ func (f Flower) EntityInside(_ cube.Pos, _ *world.Tx, e world.Entity) {
if living, ok := e.(interface {
AddEffect(effect.Effect)
}); ok {
living.AddEffect(effect.New(effect.Wither{}, 1, 2*time.Second))
living.AddEffect(effect.New(effect.Wither, 1, 2*time.Second))
}
}
}
Expand Down
14 changes: 8 additions & 6 deletions server/entity/effect/absorption.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,16 @@ import (
"image/color"
)

// Absorption is a lasting effect that increases the health of an entity over the maximum. Once this extra
// health is lost, it will not regenerate.
type Absorption struct {
// Absorption is a lasting effect that increases the health of an entity over
// the maximum. Once this extra health is lost, it will not regenerate.
var Absorption absorption

type absorption struct {
nopLasting
}

// Start ...
func (Absorption) Start(e world.Entity, lvl int) {
func (absorption) Start(e world.Entity, lvl int) {
if i, ok := e.(interface {
SetAbsorption(health float64)
}); ok {
Expand All @@ -21,7 +23,7 @@ func (Absorption) Start(e world.Entity, lvl int) {
}

// End ...
func (Absorption) End(e world.Entity, _ int) {
func (absorption) End(e world.Entity, _ int) {
if i, ok := e.(interface {
SetAbsorption(health float64)
}); ok {
Expand All @@ -30,6 +32,6 @@ func (Absorption) End(e world.Entity, _ int) {
}

// RGBA ...
func (Absorption) RGBA() color.RGBA {
func (absorption) RGBA() color.RGBA {
return color.RGBA{R: 0x25, G: 0x52, B: 0xa5, A: 0xff}
}
9 changes: 6 additions & 3 deletions server/entity/effect/blindness.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,15 @@ import (
"image/color"
)

// Blindness is a lasting effect that greatly reduces the vision range of the entity affected.
type Blindness struct {
// Blindness is a lasting effect that greatly reduces the vision range of the
// entity affected.
var Blindness blindness

type blindness struct {
nopLasting
}

// RGBA ...
func (Blindness) RGBA() color.RGBA {
func (blindness) RGBA() color.RGBA {
return color.RGBA{R: 0x1f, G: 0x1f, B: 0x23, A: 0xff}
}
13 changes: 8 additions & 5 deletions server/entity/effect/conduit_power.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,17 @@ import (
"image/color"
)

// ConduitPower is a lasting effect that grants the affected entity the ability to breathe underwater and
// allows the entity to break faster when underwater or in the rain. (Similarly to haste.)
type ConduitPower struct {
// ConduitPower is a lasting effect that grants the affected entity the ability
// to breathe underwater and allows the entity to break faster when underwater
// or in the rain. (Similarly to haste.)
var ConduitPower conduitPower

type conduitPower struct {
nopLasting
}

// Multiplier returns the mining speed multiplier from this effect.
func (ConduitPower) Multiplier(lvl int) float64 {
func (conduitPower) Multiplier(lvl int) float64 {
v := 1 - float64(lvl)*0.1
if v < 0 {
v = 0
Expand All @@ -20,6 +23,6 @@ func (ConduitPower) Multiplier(lvl int) float64 {
}

// RGBA ...
func (ConduitPower) RGBA() color.RGBA {
func (conduitPower) RGBA() color.RGBA {
return color.RGBA{R: 0x1d, G: 0xc2, B: 0xd1, A: 0xff}
}
9 changes: 6 additions & 3 deletions server/entity/effect/darkness.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,15 @@ import (
"image/color"
)

// Darkness is a lasting effect that causes the player's vision to dim occasionally.
type Darkness struct {
// Darkness is a lasting effect that causes the player's vision to dim
// occasionally.
var Darkness darkness

type darkness struct {
nopLasting
}

// RGBA ...
func (Darkness) RGBA() color.RGBA {
func (darkness) RGBA() color.RGBA {
return color.RGBA{R: 0x29, G: 0x27, B: 0x21, A: 0xff}
}
4 changes: 2 additions & 2 deletions server/entity/effect/effect.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ type Type interface {
// the colours will be mixed together to form a new colour.
RGBA() color.RGBA
// Apply applies the effect to an entity. Apply is called only once for
// instant effects, such as InstantHealth, while it is called every tick for
// instant effects, such as instantHealth, while it is called every tick for
// lasting effects. The Effect holding the Type is passed along with the
// current tick.
Apply(e world.Entity, eff Effect)
Expand Down Expand Up @@ -170,7 +170,7 @@ type living interface {
// healing, for example entity.FoodHealingSource if the entity healed by having a full food bar. If the health
// added to the original health exceeds the entity's max health, Heal may not add the full amount.
Heal(health float64, source world.HealingSource)
// Speed returns the current speed of the living entity. The default value is different for each entity.
// speed returns the current speed of the living entity. The default value is different for each entity.
Speed() float64
// SetSpeed sets the speed of an entity to a new value.
SetSpeed(float64)
Expand Down
13 changes: 8 additions & 5 deletions server/entity/effect/fatal_poison.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,17 @@ import (
"image/color"
)

// FatalPoison is a lasting effect that causes the affected entity to lose health gradually. FatalPoison,
// unlike Poison, can kill the entity it is applied to.
type FatalPoison struct {
// FatalPoison is a lasting effect that causes the affected entity to lose
// health gradually. fatalPoison, unlike poison, can kill the entity it is
// applied to.
var FatalPoison fatalPoison

type fatalPoison struct {
nopLasting
}

// Apply ...
func (FatalPoison) Apply(e world.Entity, eff Effect) {
func (fatalPoison) Apply(e world.Entity, eff Effect) {
interval := max(50>>(eff.Level()-1), 1)
if eff.Tick()%interval == 0 {
if l, ok := e.(living); ok {
Expand All @@ -22,6 +25,6 @@ func (FatalPoison) Apply(e world.Entity, eff Effect) {
}

// RGBA ...
func (FatalPoison) RGBA() color.RGBA {
func (fatalPoison) RGBA() color.RGBA {
return color.RGBA{R: 0x4e, G: 0x93, B: 0x31, A: 0xff}
}
6 changes: 4 additions & 2 deletions server/entity/effect/fire_resistance.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,13 @@ import (
)

// FireResistance is a lasting effect that grants immunity to fire & lava damage.
type FireResistance struct {
var FireResistance fireResistance

type fireResistance struct {
nopLasting
}

// RGBA ...
func (FireResistance) RGBA() color.RGBA {
func (fireResistance) RGBA() color.RGBA {
return color.RGBA{R: 0xe4, G: 0x9a, B: 0x3a, A: 0xff}
}
11 changes: 7 additions & 4 deletions server/entity/effect/haste.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,16 @@ import (
"image/color"
)

// Haste is a lasting effect that increases the mining speed of a player by 20% for each level of the effect.
type Haste struct {
// Haste is a lasting effect that increases the mining speed of a player by 20%
// for each level of the effect.
var Haste haste

type haste struct {
nopLasting
}

// Multiplier returns the mining speed multiplier from this effect.
func (Haste) Multiplier(lvl int) float64 {
func (haste) Multiplier(lvl int) float64 {
v := 1 - float64(lvl)*0.1
if v < 0 {
v = 0
Expand All @@ -19,6 +22,6 @@ func (Haste) Multiplier(lvl int) float64 {
}

// RGBA ...
func (Haste) RGBA() color.RGBA {
func (haste) RGBA() color.RGBA {
return color.RGBA{R: 0xd9, G: 0xc0, B: 0x43, A: 0xff}
}
13 changes: 8 additions & 5 deletions server/entity/effect/health_boost.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,26 +5,29 @@ import (
"image/color"
)

// HealthBoost causes the affected entity to have its maximum health changed for a specific duration.
type HealthBoost struct {
// HealthBoost causes the affected entity to have its maximum health changed
// for a specific duration.
var HealthBoost healthBoost

type healthBoost struct {
nopLasting
}

// Start ...
func (HealthBoost) Start(e world.Entity, lvl int) {
func (healthBoost) Start(e world.Entity, lvl int) {
if l, ok := e.(living); ok {
l.SetMaxHealth(l.MaxHealth() + 4*float64(lvl))
}
}

// End ...
func (HealthBoost) End(e world.Entity, lvl int) {
func (healthBoost) End(e world.Entity, lvl int) {
if l, ok := e.(living); ok {
l.SetMaxHealth(l.MaxHealth() - 4*float64(lvl))
}
}

// RGBA ...
func (HealthBoost) RGBA() color.RGBA {
func (healthBoost) RGBA() color.RGBA {
return color.RGBA{R: 0xf8, G: 0x7d, B: 0x23, A: 0xff}
}
11 changes: 7 additions & 4 deletions server/entity/effect/hunger.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,16 @@ import (
"image/color"
)

// Hunger is a lasting effect that causes an affected player to gradually lose saturation and food.
type Hunger struct {
// Hunger is a lasting effect that causes an affected player to gradually lose
// saturation and food.
var Hunger hunger

type hunger struct {
nopLasting
}

// Apply ...
func (Hunger) Apply(e world.Entity, eff Effect) {
func (hunger) Apply(e world.Entity, eff Effect) {
if i, ok := e.(interface {
Exhaust(points float64)
}); ok {
Expand All @@ -20,6 +23,6 @@ func (Hunger) Apply(e world.Entity, eff Effect) {
}

// RGBA ...
func (Hunger) RGBA() color.RGBA {
func (hunger) RGBA() color.RGBA {
return color.RGBA{R: 0x58, G: 0x76, B: 0x53, A: 0xff}
}
15 changes: 9 additions & 6 deletions server/entity/effect/instant_damage.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,24 +5,27 @@ import (
"image/color"
)

// InstantDamage is an instant effect that causes a living entity to immediately take some damage, depending
// on the level and the potency of the effect.
type InstantDamage struct{}
// InstantDamage is an instant effect that causes a living entity to
// immediately take some damage, depending on the level and the potency of the
// effect.
var InstantDamage instantDamage

type instantDamage struct{}

// Apply ...
func (i InstantDamage) Apply(e world.Entity, eff Effect) {
func (i instantDamage) Apply(e world.Entity, eff Effect) {
base := 3 << eff.Level()
if l, ok := e.(living); ok {
l.Hurt(float64(base)*eff.potency, InstantDamageSource{})
}
}

// RGBA ...
func (InstantDamage) RGBA() color.RGBA {
func (instantDamage) RGBA() color.RGBA {
return color.RGBA{R: 0x43, G: 0x0a, B: 0x09, A: 0xff}
}

// InstantDamageSource is used for damage caused by an effect.InstantDamage
// InstantDamageSource is used for damage caused by an effect.instantDamage
// applied to an entity.
type InstantDamageSource struct{}

Expand Down
15 changes: 9 additions & 6 deletions server/entity/effect/instant_health.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,26 +5,29 @@ import (
"image/color"
)

// InstantHealth is an instant effect that causes the player that it is applied to immediately regain some
// health. The amount of health regained depends on the effect level and potency.
type InstantHealth struct{}
// InstantHealth is an instant effect that causes the player that it is applied
// to immediately regain some health. The amount of health regained depends on
// the effect level and potency.
var InstantHealth instantHealth

type instantHealth struct{}

// Apply instantly heals the world.Entity passed for a bit of health, depending on the effect level and
// potency.
func (i InstantHealth) Apply(e world.Entity, eff Effect) {
func (i instantHealth) Apply(e world.Entity, eff Effect) {
base := 2 << eff.Level()
if l, ok := e.(living); ok {
l.Heal(float64(base)*eff.potency, InstantHealingSource{})
}
}

// RGBA ...
func (InstantHealth) RGBA() color.RGBA {
func (instantHealth) RGBA() color.RGBA {
return color.RGBA{R: 0xf8, G: 0x24, B: 0x23, A: 0xff}
}

// InstantHealingSource is a healing source used when an entity regains
// health from an effect.InstantHealth.
// health from an effect.instantHealth.
type InstantHealingSource struct{}

func (InstantHealingSource) HealingSource() {}
Loading

0 comments on commit b297b65

Please sign in to comment.