Browse Source

Add hero switch (no interface); network rotation

Currently it just switches up by one hero.

To change, I had to change the set_status rpc protocol to include
rotation (that way switching heroes isn't jarring); so they're combined
here.
master
Luna 7 years ago
parent
commit
735c1e7af2
3 changed files with 71 additions and 12 deletions
  1. +2
    -0
      project.godot
  2. +19
    -0
      scenes/player.tscn
  3. +50
    -12
      scripts/player.gd

+ 2
- 0
project.godot View File

@ -34,6 +34,8 @@ hero_1_confirm_wall=[ Object(InputEventMouseButton,"resource_local_to_scene":fal
]
hero_2_switch_gravity=[ Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":0,"alt":false,"shift":false,"control":false,"meta":false,"command":false,"pressed":false,"scancode":46,"unicode":0,"echo":false,"script":null)
]
switch_hero=[ Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":0,"alt":false,"shift":false,"control":false,"meta":false,"command":false,"pressed":false,"scancode":39,"unicode":0,"echo":false,"script":null)
]
[rendering]


+ 19
- 0
scenes/player.tscn View File

@ -146,4 +146,23 @@ lines_skipped = 0
max_lines_visible = -1
_sections_unfolded = [ "Anchor", "Margin", "Rect" ]
[node name="SwitchCharge" type="Label" parent="MasterOnly" index="1"]
anchor_left = 0.5
anchor_top = 1.0
anchor_right = 0.5
anchor_bottom = 1.0
margin_left = -20.0
margin_top = -108.0
margin_right = 20.0
margin_bottom = -94.0
rect_pivot_offset = Vector2( 0, 0 )
mouse_filter = 2
mouse_default_cursor_shape = 0
size_flags_horizontal = 1
size_flags_vertical = 4
percent_visible = 1.0
lines_skipped = 0
max_lines_visible = -1

+ 50
- 12
scripts/player.gd View File

@ -14,8 +14,8 @@ var floor_friction = 0.92
var air_friction = 0.98
var player_info # Set by lobby
var health = 100
var stamina = 10000
var switch_charge = 0
var movement_charge = 0.0015
var debug_node
@ -34,6 +34,8 @@ func _ready():
if is_network_master():
get_node("Yaw/Pitch/Camera").make_current()
spawn()
else:
remove_child(get_node("MasterOnly"))
func spawn():
var placement = Vector3()
@ -55,8 +57,7 @@ func _input(event):
if event is InputEventMouseMotion:
yaw = fmod(yaw - event.relative.x * view_sensitivity, 360)
pitch = max(min(pitch - event.relative.y * view_sensitivity, 85), -85)
get_node("Yaw").set_rotation(Vector3(0, deg2rad(yaw), 0))
get_node("Yaw/Pitch").set_rotation(Vector3(deg2rad(pitch), 0, 0))
set_rotation()
# Toggle mouse capture:
if Input.is_action_pressed("toggle_mouse_capture"):
@ -67,19 +68,37 @@ func _input(event):
Input.set_mouse_mode(Input.MOUSE_MODE_CAPTURED)
view_sensitivity = 0.25
if Input.is_action_just_pressed("switch_hero"):
switch_hero_interface()
# Quit the game:
if Input.is_action_pressed("quit"):
quit()
func set_rotation():
get_node("Yaw").set_rotation(Vector3(0, deg2rad(yaw), 0))
get_node("Yaw/Pitch").set_rotation(Vector3(deg2rad(pitch), 0, 0))
master func _integrate_forces(state):
control_player(state)
rpc_unreliable("set_status", get_transform(), get_linear_velocity(), get_angular_velocity())
slave func set_status(tf, lv, av):
set_transform(tf)
set_linear_velocity(lv)
set_angular_velocity(av)
func _integrate_forces(state):
if is_network_master():
control_player(state)
rpc_unreliable("set_status", get_status())
slave func set_status(s):
set_transform(s[0])
set_linear_velocity(s[1])
set_angular_velocity(s[2])
yaw = s[3]
pitch = s[4]
set_rotation() # Confirm yaw + pitch changes
func get_status():
return [
get_transform(),
get_linear_velocity(),
get_angular_velocity(),
yaw,
pitch,
]
func control_player(state):
@ -122,9 +141,28 @@ func control_player(state):
state.set_linear_velocity(lin_v)
var vel = get_linear_velocity()
switch_charge += movement_charge * vel.length()
get_node("MasterOnly/SwitchCharge").set_text("%.f%%" % switch_charge)
state.integrate_forces()
func switch_hero_interface():
# TODO: Make a real interface
player_info.hero += 1
rpc("switch_hero", player_info.hero)
sync func switch_hero(hero):
var new_hero = load("res://scenes/heroes/%d.tscn" % hero).instance()
var net_id = get_tree().get_network_unique_id()
set_name("%d-delete" % net_id) # Can't have duplicate names
new_hero.set_name("%d" % net_id)
new_hero.set_network_master(net_id)
new_hero.player_info = player_info
get_node("/root/world/players").call_deferred("add_child", new_hero)
# We must wait until after _ready is called, so that we don't end up at spawn
new_hero.call_deferred("set_status", get_status())
queue_free()
func _exit_scene():
Input.set_mouse_mode(Input.MOUSE_MODE_VISIBLE)


Loading…
Cancel
Save