Add demo: Physics Tests 2D

Similar to its 3D counterpart, with tests using 2D physics.
This commit is contained in:
PouleyKetchoupp
2020-12-09 20:01:30 -07:00
parent 3147c6c5bd
commit 8241be5817
37 changed files with 1465 additions and 0 deletions

View File

@@ -0,0 +1,41 @@
extends Control
const MAX_ENTRIES = 100
var _entry_template
func _enter_tree():
Log.connect("entry_logged", self, "_on_log_entry")
_entry_template = get_child(0) as Label
remove_child(_entry_template)
func _exit_tree():
_entry_template.free()
func clear():
while get_child_count():
var entry = get_child(get_child_count() - 1)
remove_child(entry)
entry.queue_free()
func _on_log_entry(message, type):
var new_entry = _entry_template.duplicate() as Label
new_entry.set_text(message)
if type == Log.LogType.ERROR:
new_entry.modulate = Color.red
else:
new_entry.modulate = Color.white
if get_child_count() >= MAX_ENTRIES:
var first_entry = get_child(0) as Label
remove_child(first_entry)
first_entry.queue_free()
add_child(new_entry)

View File

@@ -0,0 +1,12 @@
extends Label
func _process(_delta):
var engine_name = ""
match System.get_physics_engine():
System.PhysicsEngine.GODOT_PHYSICS:
engine_name = "Godot Physics"
System.PhysicsEngine.OTHER:
var engine_setting = ProjectSettings.get_setting("physics/2d/physics_engine")
engine_name = "Other (%s)" % engine_setting
set_text("Physics engine: %s" % engine_name)

View File

@@ -0,0 +1,5 @@
extends Label
func _process(_delta):
set_text("FPS: %d" % Engine.get_frames_per_second())

View File

@@ -0,0 +1,13 @@
extends Label
var test_name setget _set_test_name
func _ready():
set_text("Select a test from the menu to start it")
func _set_test_name(value):
test_name = value
set_text("Test: %s" % test_name)

View File

@@ -0,0 +1,5 @@
extends Label
func _process(_delta):
set_text("Godot Version: %s" % Engine.get_version_info().string)

View File

@@ -0,0 +1,47 @@
class_name OptionMenu
extends MenuButton
signal option_selected(item_path)
func add_menu_item(item_path):
var path_elements = item_path.split("/", false)
var path_element_count = path_elements.size()
assert(path_element_count > 0)
var path = ""
var popup = get_popup()
for element_index in path_element_count - 1:
var popup_label = path_elements[element_index]
path += popup_label + "/"
popup = _add_popup(popup, path, popup_label)
_add_item(popup, path_elements[path_element_count - 1])
func _add_item(parent_popup, label):
parent_popup.add_item(label)
func _add_popup(parent_popup, path, label):
if parent_popup.has_node(label):
var popup_node = parent_popup.get_node(label)
var popup_menu = popup_node as PopupMenu
assert(popup_menu)
return popup_menu
var popup_menu = PopupMenu.new()
popup_menu.name = label
parent_popup.add_child(popup_menu)
parent_popup.add_submenu_item(label, label)
popup_menu.connect("index_pressed", self, "_on_item_pressed", [popup_menu, path])
return popup_menu
func _on_item_pressed(item_index, popup_menu, path):
var item_path = path + popup_menu.get_item_text(item_index)
emit_signal("option_selected", item_path)

View File

@@ -0,0 +1,14 @@
extends ScrollContainer
export(bool) var auto_scroll = false setget set_auto_scroll
func _process(_delta):
if auto_scroll:
var scrollbar = get_v_scrollbar()
scrollbar.value = scrollbar.max_value
func set_auto_scroll(value):
auto_scroll = value

View File

@@ -0,0 +1,50 @@
extends Node
enum PhysicsEngine {
GODOT_PHYSICS,
OTHER,
}
var _engine = PhysicsEngine.OTHER
func _enter_tree():
get_tree().debug_collisions_hint = true
var engine_string = ProjectSettings.get_setting("physics/2d/physics_engine")
match engine_string:
"DEFAULT":
_engine = PhysicsEngine.GODOT_PHYSICS
"GodotPhysics":
_engine = PhysicsEngine.GODOT_PHYSICS
_:
_engine = PhysicsEngine.OTHER
func _process(_delta):
if Input.is_action_just_pressed("toggle_full_screen"):
OS.window_fullscreen = not OS.window_fullscreen
if Input.is_action_just_pressed("toggle_debug_collision"):
var debug_collision_enabled = not _is_debug_collision_enabled()
_set_debug_collision_enabled(debug_collision_enabled)
if debug_collision_enabled:
Log.print_log("Debug Collision ON")
else:
Log.print_log("Debug Collision OFF")
if Input.is_action_just_pressed("exit"):
get_tree().quit()
func get_physics_engine():
return _engine
func _set_debug_collision_enabled(enabled):
get_tree().debug_collisions_hint = enabled
func _is_debug_collision_enabled():
return get_tree().debug_collisions_hint

View File

@@ -0,0 +1,20 @@
extends Node
enum LogType {
LOG,
ERROR,
}
signal entry_logged(message, type)
func print_log(message):
print(message)
emit_signal("entry_logged", message, LogType.LOG)
func print_error(message):
push_error(message)
printerr(message)
emit_signal("entry_logged", message, LogType.ERROR)