175 lines
5.9 KiB
GDScript
175 lines
5.9 KiB
GDScript
extends Node3D
|
|
## A cast fishing float.
|
|
##
|
|
## flying -> it arcs out from the rod tip.
|
|
## floating -> it landed on deep enough water; a fish bites after a while
|
|
## (sooner at dawn and dusk, and in the rain).
|
|
## biting -> the float dips; you have BITE_WINDOW seconds to reel in
|
|
## (Actions.reel), or the fish gets away and you wait again.
|
|
## landed -> it fell on dry ground: nothing will bite. Reel in and try again.
|
|
## Walking too far away snaps the line.
|
|
##
|
|
## A line is drawn from the rod tip each frame (hands.gd gives the tip).
|
|
|
|
const BITE_WINDOW := 1.5
|
|
const LINE_LENGTH := 26.0
|
|
|
|
var water_type := "" ## "lake", "sea"... (world.water_at)
|
|
var water_depth := 0.0 ## how deep it is where the float came down
|
|
var table: Dictionary = {} ## that water's fishing table (ItemDB.water_fishing)
|
|
|
|
var world: Node3D
|
|
var player: Node3D
|
|
var state := "flying"
|
|
var velocity := Vector3.ZERO
|
|
var fish := "" ## what's on the line while biting
|
|
var wait_left := 0.0
|
|
var bite_left := 0.0
|
|
var rng := RandomNumberGenerator.new()
|
|
var tip := Vector3.ZERO ## rod tip, world space (set by hands.gd)
|
|
var _t := 0.0
|
|
var _mesh: MeshInstance3D
|
|
var _line: MeshInstance3D
|
|
var _imesh: ImmediateMesh
|
|
|
|
func setup(w: Node3D, p: Node3D, from: Vector3, vel: Vector3, seed: int) -> void:
|
|
world = w
|
|
player = p
|
|
velocity = vel
|
|
position = from
|
|
tip = from
|
|
rng.seed = seed
|
|
_mesh = MeshInstance3D.new()
|
|
_mesh.mesh = ItemDB.mesh_for("bobber")
|
|
_mesh.material_override = ItemDB.item_material()
|
|
_mesh.cast_shadow = GeometryInstance3D.SHADOW_CASTING_SETTING_OFF
|
|
add_child(_mesh)
|
|
_imesh = ImmediateMesh.new()
|
|
_line = MeshInstance3D.new()
|
|
_line.mesh = _imesh
|
|
_line.top_level = true
|
|
_line.cast_shadow = GeometryInstance3D.SHADOW_CASTING_SETTING_OFF
|
|
var lm := StandardMaterial3D.new()
|
|
lm.shading_mode = BaseMaterial3D.SHADING_MODE_UNSHADED
|
|
lm.albedo_color = Color(0.9, 0.9, 0.85, 0.7)
|
|
lm.transparency = BaseMaterial3D.TRANSPARENCY_ALPHA
|
|
_line.material_override = lm
|
|
add_child(_line)
|
|
|
|
## Online, our game shows the server's float (creature_net.gd): it doesn't
|
|
## run any fishing itself, it's just moved and shown.
|
|
var puppet := false
|
|
|
|
func _physics_process(delta: float) -> void:
|
|
if puppet:
|
|
_t += delta
|
|
_mesh.position.y = (-0.06 + sin(_t * 18.0) * 0.03) if state == "biting" else (sin(_t * 2.2) * 0.012 if state == "floating" else 0.0)
|
|
return
|
|
step(delta)
|
|
|
|
func _process(_delta: float) -> void:
|
|
_draw_line()
|
|
|
|
func _dawn_dusk() -> bool:
|
|
var h: float = world.day_night.hours()
|
|
return (h > 5.0 and h < 8.5) or (h > 17.0 and h < 20.5)
|
|
|
|
## How long until the next bite (seconds), from data/fish.json.
|
|
var baited := false ## this wait was shortened by bait (rotten food)
|
|
|
|
func _next_wait() -> float:
|
|
var mods: Dictionary = ItemDB.fishing.modifiers
|
|
var w: Array = table.get("bite_wait", [5.0, 14.0])
|
|
var k := 1.0
|
|
if _dawn_dusk():
|
|
k *= float(mods.get("dawn_dusk_wait", 0.6)) # fish feed at dawn and dusk
|
|
if world.weather.precip > 0.3:
|
|
k *= float(mods.get("rain_wait", 0.8))
|
|
# Rotten food in your bag is bait: the fish come sooner, and a bite takes one.
|
|
baited = player != null and player.get("inventory") != null and player.inventory.count(Actions.BAIT) > 0
|
|
if baited:
|
|
k *= Actions.BAIT_WAIT
|
|
return rng.randf_range(w[0], w[1]) * k
|
|
|
|
## A weighted pick from this water's fish (dawn/dusk weights when they apply).
|
|
## (A fish with its own "min_depth" only bites in water that deep: cod.)
|
|
func _pick_fish() -> String:
|
|
var fish: Array = table.get("fish", []).filter(func(f): return water_depth >= float(f.get("min_depth", 0.0)))
|
|
var key := "dawn_dusk" if _dawn_dusk() else "weight"
|
|
var total := 0.0
|
|
for f in fish:
|
|
total += float(f[key])
|
|
var roll := rng.randf() * total
|
|
for f in fish:
|
|
roll -= float(f[key])
|
|
if roll <= 0.0:
|
|
return f.id
|
|
return fish[-1].id if not fish.is_empty() else ""
|
|
|
|
func step(dt: float) -> void:
|
|
_t += dt
|
|
var terrain: Node3D = world.terrain
|
|
if global_position.distance_to(player.global_position) > LINE_LENGTH + 4.0:
|
|
Actions.tell(player, "The line snapped")
|
|
world.end_fishing(player)
|
|
return
|
|
match state:
|
|
"flying":
|
|
velocity.y -= 9.8 * dt
|
|
var p := global_position + velocity * dt
|
|
var ground: float = terrain.sample_height(p.x, p.z)
|
|
var water: Dictionary = world.water_at(p.x, p.z)
|
|
if not water.is_empty() and p.y <= float(water.surface):
|
|
p.y = water.surface
|
|
water_type = water.type
|
|
water_depth = float(water.depth)
|
|
table = ItemDB.water_fishing(water_type)
|
|
if table.is_empty() or table.fish.is_empty():
|
|
state = "landed"
|
|
Actions.tell(player, "Nothing to catch here")
|
|
elif float(water.depth) < float(table.min_depth):
|
|
state = "landed"
|
|
Actions.tell(player, "Too shallow here: cast further out")
|
|
else:
|
|
state = "floating"
|
|
wait_left = _next_wait()
|
|
elif p.y <= ground + 0.03:
|
|
p.y = ground + 0.03
|
|
state = "landed"
|
|
Actions.tell(player, "Cast into the water")
|
|
Sfx.at("splash", p)
|
|
global_position = p
|
|
"floating":
|
|
wait_left -= dt
|
|
_mesh.position.y = sin(_t * 2.2) * 0.012
|
|
if wait_left <= 0.0:
|
|
state = "biting"
|
|
fish = _pick_fish()
|
|
bite_left = BITE_WINDOW
|
|
Actions.tell(player, "Bite! Click to reel in")
|
|
Sfx.at("bite", global_position)
|
|
if baited and player.inventory.remove(Actions.BAIT, 1):
|
|
baited = false
|
|
"biting":
|
|
bite_left -= dt
|
|
# The float bobs hard and goes under.
|
|
_mesh.position.y = -0.06 + sin(_t * 18.0) * 0.03
|
|
if bite_left <= 0.0:
|
|
state = "floating"
|
|
fish = ""
|
|
wait_left = _next_wait() * float(ItemDB.fishing.modifiers.get("missed_wait", 1.3))
|
|
_mesh.position.y = 0.0
|
|
Actions.tell(player, "It got away")
|
|
|
|
func _draw_line() -> void:
|
|
_imesh.clear_surfaces()
|
|
_imesh.surface_begin(Mesh.PRIMITIVE_LINE_STRIP)
|
|
var a := tip
|
|
var b := global_position + Vector3(0, 0.1, 0)
|
|
for i in range(13):
|
|
var k := i / 12.0
|
|
var p := a.lerp(b, k)
|
|
p.y -= sin(k * PI) * 0.06 * a.distance_to(b) * (0.3 if state == "biting" else 1.0) # slack
|
|
_imesh.surface_add_vertex(p)
|
|
_imesh.surface_end()
|