From cd4f527ab1bcce18aba398cffde4130eed571051 Mon Sep 17 00:00:00 2001 From: tedlipper Date: Tue, 10 Mar 2026 18:26:21 -0700 Subject: [PATCH] Code cleanup Cleaned up various things in the code to make it easier for the next person --- TedsNotepad.txt | 57 +++++++++++++++++++++++++++++++++ block/Scenes/Projectile_1.gd | 38 +++++----------------- block/Scenes/enemy_1.gd | 14 ++------- block/Scenes/player.gd | 11 +++---- block/Scenes/shield_hitbox.gd | 59 +++++++++++++++-------------------- todoList | 56 +++++---------------------------- 6 files changed, 105 insertions(+), 130 deletions(-) create mode 100644 TedsNotepad.txt diff --git a/TedsNotepad.txt b/TedsNotepad.txt new file mode 100644 index 0000000..d234302 --- /dev/null +++ b/TedsNotepad.txt @@ -0,0 +1,57 @@ + + + + + + + +x1*x2+y1*y2 + +along normal speedx*sp + + + + + + + + var proje_normal = global_transform.x.normalized() + #proje_normal.y = -proje_normal.y + print("Proje Normal: ", proje_normal) + var speed_perp: float = proje_normal.dot(normal) + var perp_line: Vector2 = Vector2(normal.y,-normal.x) + var speed_para: float = proje_normal.dot(perp_line) + print("speed perp: ", speed_perp) + print("speed para: ", speed_para) + # this is just to check my math + # should be aproximately 1 (there will be some bit depth precision errors) + print("speed hype: ", speed_para*speed_para + speed_perp*speed_perp) + + var newDirection: Vector2 = Vector2( + (speed_perp * normal.x) + (speed_para * normal.y), + (speed_perp * normal.y) + (speed_para *-normal.x)) + + + + + + + + + + var projectile_velocity = area.get_parent().get_facing_direction() + print("projectile speed......................", projectile_velocity) + var shield_normal = global_transform.x.normalized() + print("Shield Normal: ", shield_normal) + #shield_normal.y = -shield_normal.y +# if abs(projectile_velocity.y)>abs(projectile_velocity.x): +# shield_normal = global_transform.y.normalized() + #shield_normal.y = -shield_normal.y + #shield_normal.x = -shield_normal.x +# var shield_perp = Vector2(shield_normal.y,-shield_normal.x) +# if area.has_method("bounce"): +# if projectile_velocity.dot(shield_perp) < 0: +# area.bounce(shield_normal) + if area.has_method("bounce"): + #if projectile_velocity.dot(shield_normal) < 0: + area.bounce(shield_normal) \ No newline at end of file diff --git a/block/Scenes/Projectile_1.gd b/block/Scenes/Projectile_1.gd index 02c8efa..4eb2716 100644 --- a/block/Scenes/Projectile_1.gd +++ b/block/Scenes/Projectile_1.gd @@ -1,46 +1,24 @@ extends Node2D +const isDebugLineOn: bool = false const speed = 200 @onready var player: CharacterBody2D = get_tree().get_first_node_in_group("player") var direction = Vector2.RIGHT -#var debug_normal: Vector2 = Vector2.ZERO - func _ready() -> void: - #direction = (player.global_position - global_position).normalized() - #look_at(player.global_position) -# pass + # direction of the player direction = (player.global_position - global_position).normalized() - global_rotation=direction.angle() - #direction= global_position.direction_to(player.global_position) - #direction = Vector2.RIGHT.rotated(rotation+ get_angle_to(playerDirection)) + global_rotation = direction.angle() -func _draw(): - pass -# draw_line(Vector2(1,0) * 1000, debug_normal * 50, Color.RED, 2.0) - -# Called every frame. 'delta' is the elapsed time since the previous frame. func _process(delta: float) -> void: position += direction * speed * delta - #position += playerDirection * speed * delta - #look_at(player.global_position) - #look_at(direction)# - -func get_facing_direction(): - # Get the direction vector based on the node's current rotation - var direction_vector: Vector2 = Vector2.from_angle(rotation) - return direction_vector - func changeDirection(normal: Vector2): - print("direction:", direction) -# direction = direction.bounce(normal) direction = normal - #direction = newDirection - #direction = direction.bounce(perp_line) rotation = direction.angle() - print("direction:", direction) -func bounce(normal: Vector2): - direction = direction.bounce(normal) - rotation = direction.angle() +#debug functions +var debug_normal: Vector2 = Vector2.ZERO +func _draw(): + if isDebugLineOn: + draw_line(Vector2(1,0) * 1000, debug_normal * 50, Color.RED, 2.0) diff --git a/block/Scenes/enemy_1.gd b/block/Scenes/enemy_1.gd index 02131ad..f31adf4 100644 --- a/block/Scenes/enemy_1.gd +++ b/block/Scenes/enemy_1.gd @@ -13,20 +13,15 @@ enum States const max_distance = 750 const SPEED = 300.0 var is_enemy_chase: bool -# + var health = 100 var health_max = 100 var health_min = 0 - var attack_dammage = 10 - var dir: Vector2 var knockback = 200 var current_state = States.IDLE - - - @onready var player: CharacterBody2D = get_tree().get_first_node_in_group("player") @onready var _animation_player = $AnimationPlayer @export var Projectile: PackedScene @@ -35,14 +30,12 @@ var anim_player func _on_timer_timeout() -> void: $DirectionTimer.wait_time = chose([1,2,3]) - if current_state !=States.CHASE: + if current_state != States.CHASE: dir = chose([Vector2.RIGHT, Vector2.LEFT]) var newProjectile = Projectile.instantiate() as Node2D newProjectile.global_position=global_position # newProjectile.global_rotation=global_rotation get_tree().current_scene.add_child(newProjectile) - - func chose(array): array.shuffle() @@ -64,9 +57,8 @@ func _process(delta: float) -> void: if anim_player.has_animation("Idle"): anim_player.play("Idle") look_at(player.global_position) - #print(player.global_position) var distance = global_position.distance_to(player.global_position) - if distance>max_distance: + if distance > max_distance: wraparound() diff --git a/block/Scenes/player.gd b/block/Scenes/player.gd index 2a4a026..b8ef38f 100644 --- a/block/Scenes/player.gd +++ b/block/Scenes/player.gd @@ -1,10 +1,8 @@ extends CharacterBody2D - const SPEED = 300.0 - -func _process(_delta: float) -> void: +func _process(delta: float) -> void: var dirX := Input.get_axis("move_left", "move_right") var dirY := Input.get_axis("move_up", "move_down") @@ -17,7 +15,8 @@ func _process(_delta: float) -> void: move_and_slide() -# look_at(get_global_mouse_position()) # uncompensated mouse control + look_at(get_global_mouse_position()) - var angle_to_mouse = get_angle_to(get_global_mouse_position()) - rotation += angle_to_mouse# + deg_to_rad(90) #+90 to makes top of image face mouse, not side + # mouse control no longer requires this 90 degree compensation +# var angle_to_mouse = get_angle_to(get_global_mouse_position()) +# rotation += angle_to_mouse + deg_to_rad(90) diff --git a/block/Scenes/shield_hitbox.gd b/block/Scenes/shield_hitbox.gd index 8dfc819..2d4f866 100644 --- a/block/Scenes/shield_hitbox.gd +++ b/block/Scenes/shield_hitbox.gd @@ -1,45 +1,36 @@ extends Area2D -var debug_normal: Vector2 = Vector2.ZERO -var bounceTrajectory: Vector2 +# Toggles debugger console output +const isVerbose: bool = false - -# Called when the node enters the scene tree for the first time. func _ready() -> void: area_entered.connect(_on_area_entered) - print("READY!") - - - -func _draw(): -# pass - draw_line(bounceTrajectory *1000, debug_normal * 50, Color.RED, 2.0) # normal -# draw_line(Vector2(400,900), debug_normal * 50, Color.RED, 2.0) # normal -# draw_line(Vector2.ZERO, debug_bounce * 50, Color.GREEN, 2.0) # bounce direction -# draw_line(Vector2.ZERO, debug_incoming * 50, Color.BLUE, 2.0) # incoming projectile - func _on_area_entered(area): if area.is_in_group("Projectiles"): - draw_line(Vector2(400,900), debug_normal * 50, Color.RED, 2.0) # normal - - print("projectile has hit player shield") - #gets the proper velocity - var projectile_velocity = area.get_parent().get_facing_direction() - print("projectile speed......................", projectile_velocity) + # gets parent node because parent is what's moving, not the child area node + var parent = area.get_parent() + readout("Event: Projectile has hit player shield") + var projectile_dir = Vector2.from_angle(parent.rotation) + vec2Readout("Projectile Dir", projectile_dir) var shield_normal = global_transform.x.normalized() - #var bounceTrajectory: Vector2 = projectile_velocity.bounce(shield_normal) - bounceTrajectory = projectile_velocity.bounce(shield_normal) - print("Shield Normal: ", shield_normal) - if area.get_parent().has_method("changeDirection"): - draw_line(bounceTrajectory, debug_normal * 50, Color.RED, 2.0) -# area.changeDirection(bounceTrajectory) -# area.get_parent().changeTrajectory(bounceTrajectory) - area.get_parent().changeDirection(bounceTrajectory) - #if area.has_method("bounce"): - # area.bounce(bounceTrajectory) + vec2Readout("Shield Normal: ", shield_normal) + if parent.has_method("changeDirection"): + var bounce_dir = projectile_dir.bounce(shield_normal) + parent.changeDirection(bounce_dir) +# makes debug output cleaner when printing Vector2 +func vec2str(vec: Vector2) -> String: + var x_str = String.num(vec.x, 2) + var y_str = String.num(vec.y, 2) + if not x_str.begins_with("-"): x_str = " " + x_str + if not y_str.begins_with("-"): y_str = " " + y_str + return "(%s, %s)" % [x_str, y_str] + +func vec2Readout(label: String, vec: Vector2): + if isVerbose: + print("Readout: ", label, vec2str(vec)) -# Called every frame. 'delta' is the elapsed time since the previous frame. -#func _process(delta: float) -> void: -# pass +func readout(str: String): + if isVerbose: + print(str) diff --git a/todoList b/todoList index 899aa7b..437f17e 100644 --- a/todoList +++ b/todoList @@ -2,12 +2,15 @@ So that we can keep track of what things need to be done Using godot version 4.6 - - Get sprites for basic characters, and background - - collisions - - projectiles - - attack types? + - Get sprites for and background + - projectile damage + - enemy attack types + - enemy behaviors Done - WASD movement + - projectiles launching + - projectiles bouncing + - Get sprites for basic characters, Current imported assests: @@ -15,9 +18,6 @@ https://screamingbrainstudios.itch.io/tiny-texture-pack -x1*x2+y1*y2 - -along normal speedx*sp @@ -27,45 +27,3 @@ along normal speedx*sp - - - var proje_normal = global_transform.x.normalized() - #proje_normal.y = -proje_normal.y - print("Proje Normal: ", proje_normal) - var speed_perp: float = proje_normal.dot(normal) - var perp_line: Vector2 = Vector2(normal.y,-normal.x) - var speed_para: float = proje_normal.dot(perp_line) - print("speed perp: ", speed_perp) - print("speed para: ", speed_para) - # this is just to check my math - # should be aproximately 1 (there will be some bit depth precision errors) - print("speed hype: ", speed_para*speed_para + speed_perp*speed_perp) - - var newDirection: Vector2 = Vector2( - (speed_perp * normal.x) + (speed_para * normal.y), - (speed_perp * normal.y) + (speed_para *-normal.x)) - - - - - - - - - - var projectile_velocity = area.get_parent().get_facing_direction() - print("projectile speed......................", projectile_velocity) - var shield_normal = global_transform.x.normalized() - print("Shield Normal: ", shield_normal) - #shield_normal.y = -shield_normal.y -# if abs(projectile_velocity.y)>abs(projectile_velocity.x): -# shield_normal = global_transform.y.normalized() - #shield_normal.y = -shield_normal.y - #shield_normal.x = -shield_normal.x -# var shield_perp = Vector2(shield_normal.y,-shield_normal.x) -# if area.has_method("bounce"): -# if projectile_velocity.dot(shield_perp) < 0: -# area.bounce(shield_normal) - if area.has_method("bounce"): - #if projectile_velocity.dot(shield_normal) < 0: - area.bounce(shield_normal) \ No newline at end of file