LD46/rb2dladder.gd

91 lines
2.7 KiB
GDScript3
Raw Normal View History

2020-04-19 16:49:28 +09:30
extends RigidBody2D
var held = false
var grabbed_vector = null
onready var beltmap = $"../BeltTiles"
var dir_vectors = [Vector2(1,0), Vector2(0,-1), Vector2(-1,0), Vector2(0,1)]
export var rungs := 12
export var cx := 2.5 # offset for edge feet
export var cy := 5.0 # offset for edge feet
#onready var foot_vectors = [Vector2(0,0), Vector2(cx,cy), Vector2(cx,-cy), Vector2(-cx,-cy), Vector2(-cx,cy)]
var foot_vectors
#var foot_weights = [3, 1, 1, 1, 1]
var total_weight = 7
func _ready():
2020-04-19 17:23:05 +09:30
# friction = 1
2020-04-19 16:49:28 +09:30
linear_damp = 2
angular_damp = 100
total_weight = 1 + rungs*4
foot_vectors = [Vector2(0, 0)]
for i in rungs:
foot_vectors.append(Vector2(cx*(i+1), cy))
foot_vectors.append(Vector2(-cx*(i+1), cy))
foot_vectors.append(Vector2(cx*(i+1), -cy))
foot_vectors.append(Vector2(-cx*(i+1), -cy))
func get_belt_direction(tx, ty):
var xflip = beltmap.is_cell_x_flipped(tx, ty)
var tp = beltmap.is_cell_transposed(tx, ty)
return int(tp) + int(xflip)*2
func get_belt_rect(vec):
var origin = beltmap.map_to_world(vec)
return Rect2(to_local(origin), Vector2(8, 8))
# Called every frame. 'delta' is the elapsed time since the previous frame.
#func _process(delta):
# var belt_speed = delta * 8
func _physics_process(delta):
2020-04-19 17:23:05 +09:30
var belt_speed = 8 * 64
2020-04-19 16:49:28 +09:30
var direction = Vector2(0, 0)
set_applied_force(Vector2(0,0))
set_applied_torque(0)
2020-04-19 17:23:05 +09:30
angular_velocity = 0
var friction_points = []
2020-04-19 16:49:28 +09:30
for i in len(foot_vectors):
var vec = beltmap.world_to_map(position + foot_vectors[i].rotated(rotation))
if beltmap.get_cell(vec.x, vec.y) >= 0:
add_force(vec - position, belt_speed*dir_vectors[get_belt_direction(vec.x, vec.y)]/total_weight)
#direction += dir_vectors[get_belt_direction(vec.x, vec.y)] * foot_weights[i]
2020-04-19 17:23:05 +09:30
else:
friction_points.append(vec - position)
2020-04-19 16:49:28 +09:30
# position += direction/total_weight * belt_speed
if held:
var ggv = to_global(grabbed_vector)
var dv = get_global_mouse_position() - ggv
2020-04-19 17:23:05 +09:30
add_force(grabbed_vector, dv * dv.length()*linear_damp)
var f = get_applied_force() + linear_velocity*mass
var fn = f.normalized()
var fmag = f.length()
var f_max = 200
for p in friction_points:
add_force(p, -fn * min(f_max, fmag/total_weight))
2020-04-19 16:49:28 +09:30
#func _input_event(_viewport, event, _shape_idx):
# if event is InputEventMouseButton:
# held = event.pressed
# if held:
# grabbed_vector = to_local(event.position)
func _input(event):
if held:
if event is InputEventMouseButton:
if not event.pressed:
held = false
# elif event is InputEventMouseMotion:
## position += event.relative
# apply_impulse(grabbed_vector, event.relative * friction)
elif event is InputEventMouseButton and event.pressed:
if $SprLadder6.get_rect().has_point(to_local(event.position)):
held = true
grabbed_vector = to_local(event.position)