From bfc01ec2a96ad1454e301eff0b35dc500fb3a287 Mon Sep 17 00:00:00 2001 From: VAILLANT Jeremy Date: Fri, 14 May 2021 22:17:44 +0200 Subject: [PATCH] Use inheritance scene instead write code in level --- scenes/levels/levels.gd | 164 +++++++++++++++++++++++++++++ scenes/levels/levels.tscn | 6 ++ scenes/levels/warcraft/WarCraft.gd | 164 +---------------------------- 3 files changed, 172 insertions(+), 162 deletions(-) create mode 100644 scenes/levels/levels.gd create mode 100644 scenes/levels/levels.tscn diff --git a/scenes/levels/levels.gd b/scenes/levels/levels.gd new file mode 100644 index 0000000..8cb17ec --- /dev/null +++ b/scenes/levels/levels.gd @@ -0,0 +1,164 @@ +extends Spatial + +const TIME_MAX = 3000 # msec +const GYROSCOPE_MAX_DIFF = 0.5 +const OFFSET_CAMERA_MAX = 0.12 +const OFFSET_STEP_CHANGE = 0.01 +const RAY_LENGTH = 1000 + +onready var gyroscope_value_old = Vector3(0, 0, 0) +onready var table = Global.database.get_table_by_name("scenes") +onready var meshes = {} +onready var meshes2 = null +onready var from = null +onready var to = null + +var mscene = load("res://db/MScene.gd") + +func _ready(): + _load_translations() + _load_meshes() + +func _process(_delta): + _check_quit_scene() + _check_dissolve_mesh() + _check_change_angle_camera() + +func _check_quit_scene(): + # Event key "escape" and "godot event" ui_end + if Input.is_action_just_pressed("ui_end"): + _confirm_before_quit() + +func _check_dissolve_mesh(): + # Event dissolve in object searched by gamer + for key in meshes: + if meshes[key].lock() == true and meshes[key].mesh() != null: + if meshes[key].tick_reference() == 0: + meshes[key].set_tick_reference(OS.get_ticks_msec()) + + if OS.get_ticks_msec() < meshes[key].tick_reference() + TIME_MAX: + meshes[key].set_value(meshes[key].value() + 0.01) + _node_to_mesh(key).get_surface_material(0).set("shader_param/dissolve_amount", meshes[key].value()) + else: + _node_to_mesh(key).call_deferred("free") + meshes[key].set_mesh(null) + +func _node_to_mesh(key): + return get_node(meshes[key].mesh()) + +func _node_to_area(key): + return get_node(meshes[key].mesh()+"/Area") + +func _check_change_angle_camera(): + var camera = $"Main Camera" + var gyroscope = Input.get_gyroscope() + + if camera.h_offset >= -OFFSET_CAMERA_MAX: + if _action_pressed("ui_left") or _action_gyroscope("left", gyroscope): + print("[warcraft#_ready] move camera angle to left") + $"Main Camera".h_offset -= OFFSET_STEP_CHANGE + + if camera.h_offset <= OFFSET_CAMERA_MAX: + if _action_pressed("ui_right") or _action_gyroscope("right", gyroscope): + print("[warcraft#_ready] move camera angle to right") + $"Main Camera".h_offset += OFFSET_STEP_CHANGE + + if camera.v_offset >= -OFFSET_CAMERA_MAX: + if _action_pressed("ui_down") or _action_gyroscope("down", gyroscope): + print("[warcraft#_ready] move camera angle to down") + $"Main Camera".v_offset -= OFFSET_STEP_CHANGE + + if camera.v_offset <= OFFSET_CAMERA_MAX: + if _action_pressed("ui_up") or _action_gyroscope("up", gyroscope): + print("[warcraft#_ready] move camera angle to up") + $"Main Camera".v_offset += OFFSET_STEP_CHANGE + + gyroscope_value_old = gyroscope + +func _action_pressed(action): + return Input.is_action_pressed(action) + +func _action_gyroscope(action, gyroscope): + if Global.gyroscope_enabled(): + var expression = Expression.new() + + expression.parse("_gyroscope_changed_"+action+"(gyroscope)", ["gyroscope"]) + + if expression.execute([gyroscope], self): + return true + else: + return false + else: + return false + +func _gyroscope_changed_left(gyroscope): + return (gyroscope.abs().y - gyroscope_value_old.abs().y) > GYROSCOPE_MAX_DIFF and \ + gyroscope.y < gyroscope_value_old.y + +func _gyroscope_changed_right(gyroscope): + return (gyroscope.abs().y - gyroscope_value_old.abs().y) > GYROSCOPE_MAX_DIFF and \ + gyroscope.y > gyroscope_value_old.y + +func _gyroscope_changed_down(gyroscope): + return (gyroscope.abs().z - gyroscope_value_old.abs().z) > GYROSCOPE_MAX_DIFF and \ + gyroscope.z > gyroscope_value_old.z or \ + (gyroscope.abs().x - gyroscope_value_old.abs().x) > GYROSCOPE_MAX_DIFF and \ + gyroscope.x > gyroscope_value_old.x + +func _gyroscope_changed_up(gyroscope): + return (gyroscope.abs().z - gyroscope_value_old.abs().z) > GYROSCOPE_MAX_DIFF and \ + gyroscope.z < gyroscope_value_old.z or \ + (gyroscope.abs().x - gyroscope_value_old.abs().x) > GYROSCOPE_MAX_DIFF and \ + gyroscope.x < gyroscope_value_old.x + +func _notification(what): + # Notification for android back action + if what == MainLoop.NOTIFICATION_WM_GO_BACK_REQUEST: + _confirm_before_quit() + +func _confirm_before_quit(): + $Dialog/ConfirmEscape.popup() + +# Back to main scene +func _quit_to_menu(): + Global.goto_scene("res://scenes/main.tscn") + +func _start_dissolve(key): + if meshes[key].lock() == false: + meshes[key].set_lock(true) + +func _on_ConfirmEscape_confirmed(): + _quit_to_menu() + +## PRIVATE +func _load_translations(): + $Dialog/ConfirmEscape.set_title(tr("SCENE_WARCRAFT_DIALOG_QUIT_TITLE")) + $Dialog/ConfirmEscape.set_text(tr("SCENE_WARCRAFT_DIALOG_QUIT_QUESTION")) + +func _load_meshes(): + var scene_detail = null + + for row_index in range(0, table.m_rows_count): + scene_detail = mscene.new(row_index) + + if scene_detail.key() != null: + meshes[scene_detail.key()] = scene_detail + + for key in meshes: + meshes[key].search_keys() + +func _input(event): + if event is InputEventMouseButton or event is InputEventScreenTouch: + var camera = $"Main Camera" + from = camera.project_ray_origin(event.position) + to = from + camera.project_ray_normal(event.position) * RAY_LENGTH + +func _physics_process(_delta): + var space_state = get_world().direct_space_state + if from != null and to != null: + var result = space_state.intersect_ray(from, to, [], 1, false, true) + from = null + to = null + var node = result["collider"].get_parent() + if node != null: + _start_dissolve(node.name) diff --git a/scenes/levels/levels.tscn b/scenes/levels/levels.tscn new file mode 100644 index 0000000..54df392 --- /dev/null +++ b/scenes/levels/levels.tscn @@ -0,0 +1,6 @@ +[gd_scene load_steps=2 format=2] + +[ext_resource path="res://scenes/levels/levels.gd" type="Script" id=1] + +[node name="Spatial" type="Spatial"] +script = ExtResource( 1 ) diff --git a/scenes/levels/warcraft/WarCraft.gd b/scenes/levels/warcraft/WarCraft.gd index 8cb17ec..681b9fd 100644 --- a/scenes/levels/warcraft/WarCraft.gd +++ b/scenes/levels/warcraft/WarCraft.gd @@ -1,164 +1,4 @@ -extends Spatial - -const TIME_MAX = 3000 # msec -const GYROSCOPE_MAX_DIFF = 0.5 -const OFFSET_CAMERA_MAX = 0.12 -const OFFSET_STEP_CHANGE = 0.01 -const RAY_LENGTH = 1000 - -onready var gyroscope_value_old = Vector3(0, 0, 0) -onready var table = Global.database.get_table_by_name("scenes") -onready var meshes = {} -onready var meshes2 = null -onready var from = null -onready var to = null - -var mscene = load("res://db/MScene.gd") +extends "res://scenes/levels/levels.gd" func _ready(): - _load_translations() - _load_meshes() - -func _process(_delta): - _check_quit_scene() - _check_dissolve_mesh() - _check_change_angle_camera() - -func _check_quit_scene(): - # Event key "escape" and "godot event" ui_end - if Input.is_action_just_pressed("ui_end"): - _confirm_before_quit() - -func _check_dissolve_mesh(): - # Event dissolve in object searched by gamer - for key in meshes: - if meshes[key].lock() == true and meshes[key].mesh() != null: - if meshes[key].tick_reference() == 0: - meshes[key].set_tick_reference(OS.get_ticks_msec()) - - if OS.get_ticks_msec() < meshes[key].tick_reference() + TIME_MAX: - meshes[key].set_value(meshes[key].value() + 0.01) - _node_to_mesh(key).get_surface_material(0).set("shader_param/dissolve_amount", meshes[key].value()) - else: - _node_to_mesh(key).call_deferred("free") - meshes[key].set_mesh(null) - -func _node_to_mesh(key): - return get_node(meshes[key].mesh()) - -func _node_to_area(key): - return get_node(meshes[key].mesh()+"/Area") - -func _check_change_angle_camera(): - var camera = $"Main Camera" - var gyroscope = Input.get_gyroscope() - - if camera.h_offset >= -OFFSET_CAMERA_MAX: - if _action_pressed("ui_left") or _action_gyroscope("left", gyroscope): - print("[warcraft#_ready] move camera angle to left") - $"Main Camera".h_offset -= OFFSET_STEP_CHANGE - - if camera.h_offset <= OFFSET_CAMERA_MAX: - if _action_pressed("ui_right") or _action_gyroscope("right", gyroscope): - print("[warcraft#_ready] move camera angle to right") - $"Main Camera".h_offset += OFFSET_STEP_CHANGE - - if camera.v_offset >= -OFFSET_CAMERA_MAX: - if _action_pressed("ui_down") or _action_gyroscope("down", gyroscope): - print("[warcraft#_ready] move camera angle to down") - $"Main Camera".v_offset -= OFFSET_STEP_CHANGE - - if camera.v_offset <= OFFSET_CAMERA_MAX: - if _action_pressed("ui_up") or _action_gyroscope("up", gyroscope): - print("[warcraft#_ready] move camera angle to up") - $"Main Camera".v_offset += OFFSET_STEP_CHANGE - - gyroscope_value_old = gyroscope - -func _action_pressed(action): - return Input.is_action_pressed(action) - -func _action_gyroscope(action, gyroscope): - if Global.gyroscope_enabled(): - var expression = Expression.new() - - expression.parse("_gyroscope_changed_"+action+"(gyroscope)", ["gyroscope"]) - - if expression.execute([gyroscope], self): - return true - else: - return false - else: - return false - -func _gyroscope_changed_left(gyroscope): - return (gyroscope.abs().y - gyroscope_value_old.abs().y) > GYROSCOPE_MAX_DIFF and \ - gyroscope.y < gyroscope_value_old.y - -func _gyroscope_changed_right(gyroscope): - return (gyroscope.abs().y - gyroscope_value_old.abs().y) > GYROSCOPE_MAX_DIFF and \ - gyroscope.y > gyroscope_value_old.y - -func _gyroscope_changed_down(gyroscope): - return (gyroscope.abs().z - gyroscope_value_old.abs().z) > GYROSCOPE_MAX_DIFF and \ - gyroscope.z > gyroscope_value_old.z or \ - (gyroscope.abs().x - gyroscope_value_old.abs().x) > GYROSCOPE_MAX_DIFF and \ - gyroscope.x > gyroscope_value_old.x - -func _gyroscope_changed_up(gyroscope): - return (gyroscope.abs().z - gyroscope_value_old.abs().z) > GYROSCOPE_MAX_DIFF and \ - gyroscope.z < gyroscope_value_old.z or \ - (gyroscope.abs().x - gyroscope_value_old.abs().x) > GYROSCOPE_MAX_DIFF and \ - gyroscope.x < gyroscope_value_old.x - -func _notification(what): - # Notification for android back action - if what == MainLoop.NOTIFICATION_WM_GO_BACK_REQUEST: - _confirm_before_quit() - -func _confirm_before_quit(): - $Dialog/ConfirmEscape.popup() - -# Back to main scene -func _quit_to_menu(): - Global.goto_scene("res://scenes/main.tscn") - -func _start_dissolve(key): - if meshes[key].lock() == false: - meshes[key].set_lock(true) - -func _on_ConfirmEscape_confirmed(): - _quit_to_menu() - -## PRIVATE -func _load_translations(): - $Dialog/ConfirmEscape.set_title(tr("SCENE_WARCRAFT_DIALOG_QUIT_TITLE")) - $Dialog/ConfirmEscape.set_text(tr("SCENE_WARCRAFT_DIALOG_QUIT_QUESTION")) - -func _load_meshes(): - var scene_detail = null - - for row_index in range(0, table.m_rows_count): - scene_detail = mscene.new(row_index) - - if scene_detail.key() != null: - meshes[scene_detail.key()] = scene_detail - - for key in meshes: - meshes[key].search_keys() - -func _input(event): - if event is InputEventMouseButton or event is InputEventScreenTouch: - var camera = $"Main Camera" - from = camera.project_ray_origin(event.position) - to = from + camera.project_ray_normal(event.position) * RAY_LENGTH - -func _physics_process(_delta): - var space_state = get_world().direct_space_state - if from != null and to != null: - var result = space_state.intersect_ray(from, to, [], 1, false, true) - from = null - to = null - var node = result["collider"].get_parent() - if node != null: - _start_dissolve(node.name) + pass