-
Notifications
You must be signed in to change notification settings - Fork 147
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Co-authored-by: DaPigGuy <[email protected]> Co-authored-by: RoyalMCPE <[email protected]>
- Loading branch information
1 parent
0347c6e
commit 613899c
Showing
13 changed files
with
403 additions
and
13 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
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,230 @@ | ||
package item | ||
|
||
import ( | ||
"time" | ||
_ "unsafe" | ||
|
||
"github.com/df-mc/dragonfly/server/world" | ||
"github.com/df-mc/dragonfly/server/world/sound" | ||
) | ||
|
||
// Crossbow is a ranged weapon similar to a bow that uses arrows or fireworks as ammunition. | ||
type Crossbow struct { | ||
// Item is the item the crossbow is charged with. | ||
Item Stack | ||
} | ||
|
||
// Charge starts the charging process and checks if the charge duration meets the required duration. | ||
func (c Crossbow) Charge(releaser Releaser, tx *world.Tx, ctx *UseContext, duration time.Duration) bool { | ||
if !c.Item.Empty() { | ||
return false | ||
} | ||
|
||
creative := releaser.GameMode().CreativeInventory() | ||
held, left := releaser.HeldItems() | ||
|
||
chargeDuration := time.Duration(1.25 * float64(time.Second)) | ||
for _, enchant := range held.Enchantments() { | ||
if q, ok := enchant.Type().(interface{ ChargeDuration(int) time.Duration }); ok { | ||
chargeDuration = min(chargeDuration, q.ChargeDuration(enchant.Level())) | ||
} | ||
} | ||
|
||
if duration < chargeDuration { | ||
return false | ||
} | ||
|
||
var projectileItem Stack | ||
if !left.Empty() { | ||
_, isFirework := left.Item().(Firework) | ||
_, isArrow := left.Item().(Arrow) | ||
if isFirework || isArrow { | ||
projectileItem = left | ||
} | ||
} | ||
|
||
if projectileItem.Empty() { | ||
var ok bool | ||
projectileItem, ok = ctx.FirstFunc(func(stack Stack) bool { | ||
_, isArrow := stack.Item().(Arrow) | ||
return isArrow | ||
}) | ||
|
||
if !ok && !creative { | ||
return false | ||
} | ||
|
||
if projectileItem.Empty() { | ||
projectileItem = NewStack(Arrow{}, 1) | ||
} | ||
} | ||
|
||
c.Item = projectileItem.Grow(-projectileItem.Count() + 1) | ||
if !creative { | ||
ctx.Consume(c.Item) | ||
} | ||
|
||
crossbow := held.WithItem(c) | ||
releaser.SetHeldItems(crossbow, left) | ||
return true | ||
} | ||
|
||
// ContinueCharge ... | ||
func (c Crossbow) ContinueCharge(releaser Releaser, tx *world.Tx, ctx *UseContext, duration time.Duration) { | ||
if !c.Item.Empty() { | ||
return | ||
} | ||
|
||
creative := releaser.GameMode().CreativeInventory() | ||
held, left := releaser.HeldItems() | ||
|
||
chargeDuration, quickChargeLevel := time.Duration(1.25*float64(time.Second)), 0 | ||
for _, enchant := range held.Enchantments() { | ||
if q, ok := enchant.Type().(interface{ ChargeDuration(int) time.Duration }); ok { | ||
chargeDuration = min(chargeDuration, q.ChargeDuration(enchant.Level())) | ||
quickChargeLevel = enchant.Level() | ||
} | ||
} | ||
|
||
var projectileItem Stack | ||
if !left.Empty() { | ||
_, isFirework := left.Item().(Firework) | ||
_, isArrow := left.Item().(Arrow) | ||
if isFirework || isArrow { | ||
projectileItem = left | ||
} | ||
} | ||
|
||
if projectileItem.Empty() { | ||
var ok bool | ||
projectileItem, ok = ctx.FirstFunc(func(stack Stack) bool { | ||
_, isArrow := stack.Item().(Arrow) | ||
return isArrow | ||
}) | ||
|
||
if !ok && !creative { | ||
return | ||
} | ||
|
||
if projectileItem.Empty() { | ||
projectileItem = NewStack(Arrow{}, 1) | ||
} | ||
} | ||
|
||
if projectileItem.Empty() { | ||
return | ||
} | ||
|
||
hasQuickCharge := quickChargeLevel > 0 | ||
progress := float64(duration) / float64(chargeDuration) | ||
if duration.Seconds() <= 0.1 { | ||
tx.PlaySound(releaser.Position(), sound.Crossbow{Stage: sound.CrossbowStageLoadStart, QuickCharge: hasQuickCharge}) | ||
} | ||
|
||
// Base reload time is 25 ticks; each Quick Charge level reduces by 5 ticks | ||
multiplier := 25.0 / float64(25-(5*quickChargeLevel)) | ||
|
||
// Adjust ticks based on the multiplier | ||
adjustedTicks := int(float64(duration.Milliseconds()) / (50 / multiplier)) | ||
|
||
// Play sound after every 16 ticks (adjusted by Quick Charge) | ||
if adjustedTicks%16 == 0 { | ||
tx.PlaySound(releaser.Position(), sound.Crossbow{Stage: sound.CrossbowStageMiddle, QuickCharge: hasQuickCharge}) | ||
} | ||
|
||
if progress >= 1 && quickChargeLevel > 0 { | ||
tx.PlaySound(releaser.Position(), sound.Crossbow{Stage: sound.CrossbowStageLoadEnd, QuickCharge: hasQuickCharge}) | ||
} | ||
} | ||
|
||
// ReleaseCharge checks if the item is fully charged and, if so, releases it. | ||
func (c Crossbow) ReleaseCharge(releaser Releaser, tx *world.Tx, ctx *UseContext) bool { | ||
if c.Item.Empty() { | ||
return false | ||
} | ||
|
||
creative := releaser.GameMode().CreativeInventory() | ||
rot := releaser.Rotation().Neg() | ||
dirVec := releaser.Rotation().Vec3().Normalize() | ||
|
||
if firework, isFirework := c.Item.Item().(Firework); isFirework { | ||
createFirework := tx.World().EntityRegistry().Config().Firework | ||
fireworkEntity := createFirework(world.EntitySpawnOpts{ | ||
Position: torsoPosition(releaser), | ||
Velocity: dirVec.Mul(0.1), | ||
Rotation: rot, | ||
}, firework, releaser, 1.15, 0, false) | ||
tx.AddEntity(fireworkEntity) | ||
ctx.DamageItem(3) | ||
} else { | ||
createArrow := tx.World().EntityRegistry().Config().Arrow | ||
arrow := createArrow(world.EntitySpawnOpts{ | ||
Position: torsoPosition(releaser), | ||
Velocity: dirVec.Mul(5.15), | ||
Rotation: rot, | ||
}, 9, releaser, false, false, !creative, 0, c.Item.Item().(Arrow).Tip) | ||
tx.AddEntity(arrow) | ||
ctx.DamageItem(1) | ||
} | ||
|
||
c.Item = Stack{} | ||
held, left := releaser.HeldItems() | ||
crossbow := held.WithItem(c) | ||
releaser.SetHeldItems(crossbow, left) | ||
tx.PlaySound(releaser.Position(), sound.Crossbow{Stage: sound.CrossbowStageShoot}) | ||
return true | ||
} | ||
|
||
// MaxCount always returns 1. | ||
func (Crossbow) MaxCount() int { | ||
return 1 | ||
} | ||
|
||
// DurabilityInfo ... | ||
func (Crossbow) DurabilityInfo() DurabilityInfo { | ||
return DurabilityInfo{ | ||
MaxDurability: 464, | ||
BrokenItem: simpleItem(Stack{}), | ||
} | ||
} | ||
|
||
// FuelInfo ... | ||
func (Crossbow) FuelInfo() FuelInfo { | ||
return newFuelInfo(time.Second * 15) | ||
} | ||
|
||
// EnchantmentValue ... | ||
func (Crossbow) EnchantmentValue() int { | ||
return 1 | ||
} | ||
|
||
// EncodeItem ... | ||
func (Crossbow) EncodeItem() (name string, meta int16) { | ||
return "minecraft:crossbow", 0 | ||
} | ||
|
||
// DecodeNBT ... | ||
func (c Crossbow) DecodeNBT(data map[string]any) any { | ||
c.Item = mapItem(data, "chargedItem") | ||
return c | ||
} | ||
|
||
// EncodeNBT ... | ||
func (c Crossbow) EncodeNBT() map[string]any { | ||
if !c.Item.Empty() { | ||
return map[string]any{ | ||
"chargedItem": writeItem(c.Item, true), | ||
} | ||
} | ||
return nil | ||
} | ||
|
||
// noinspection ALL | ||
// | ||
//go:linkname writeItem github.com/df-mc/dragonfly/server/internal/nbtconv.WriteItem | ||
func writeItem(s Stack, disk bool) map[string]any | ||
|
||
// noinspection ALL | ||
// | ||
//go:linkname mapItem github.com/df-mc/dragonfly/server/internal/nbtconv.MapItem | ||
func mapItem(x map[string]any, k string) Stack |
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,49 @@ | ||
package enchantment | ||
|
||
import ( | ||
"github.com/df-mc/dragonfly/server/item" | ||
"github.com/df-mc/dragonfly/server/world" | ||
"time" | ||
) | ||
|
||
// QuickCharge is an enchantment for quickly reloading a crossbow. | ||
var QuickCharge quickCharge | ||
|
||
type quickCharge struct{} | ||
|
||
// Name ... | ||
func (quickCharge) Name() string { | ||
return "Quick Charge" | ||
} | ||
|
||
// MaxLevel ... | ||
func (quickCharge) MaxLevel() int { | ||
return 3 | ||
} | ||
|
||
// Cost ... | ||
func (quickCharge) Cost(level int) (int, int) { | ||
minCost := 12 + (level-1)*20 | ||
return minCost, 50 | ||
} | ||
|
||
// Rarity ... | ||
func (quickCharge) Rarity() item.EnchantmentRarity { | ||
return item.EnchantmentRarityUncommon | ||
} | ||
|
||
// ChargeDuration returns the charge duration. | ||
func (quickCharge) ChargeDuration(level int) time.Duration { | ||
return time.Duration((1.25 - 0.25*float64(level)) * float64(time.Second)) | ||
} | ||
|
||
// CompatibleWithEnchantment ... | ||
func (quickCharge) CompatibleWithEnchantment(item.EnchantmentType) bool { | ||
return true | ||
} | ||
|
||
// CompatibleWithItem ... | ||
func (quickCharge) CompatibleWithItem(i world.Item) bool { | ||
_, ok := i.(item.Crossbow) | ||
return ok | ||
} |
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
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
Oops, something went wrong.