Cleanup
Cleaning up the messy sections of my code and making the debug console output easier to read
This commit is contained in:
@@ -5,45 +5,22 @@ const speed = 200
|
||||
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 = (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))
|
||||
|
||||
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)
|
||||
var direction_vector = Vector2.from_angle(rotation)
|
||||
return direction_vector
|
||||
|
||||
func changeTrajectory(direction_vector: Vector2):
|
||||
rotation = rad_to_deg(atan2(direction_vector.y, direction_vector.x))
|
||||
|
||||
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 function
|
||||
func _draw():
|
||||
# draw_line(Vector2(1,0) * 1000, debug_normal * 50, Color.RED, 2.0)
|
||||
pass
|
||||
|
||||
@@ -13,7 +13,7 @@ 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
|
||||
@@ -24,9 +24,6 @@ var dir: Vector2
|
||||
var knockback = 200
|
||||
var current_state = States.IDLE
|
||||
|
||||
|
||||
|
||||
|
||||
@onready var player: CharacterBody2D = get_tree().get_first_node_in_group("player")
|
||||
@export var Projectile: PackedScene
|
||||
|
||||
@@ -39,8 +36,6 @@ func _on_timer_timeout() -> void:
|
||||
# newProjectile.global_rotation=global_rotation
|
||||
get_tree().current_scene.add_child(newProjectile)
|
||||
|
||||
|
||||
|
||||
func chose(array):
|
||||
array.shuffle()
|
||||
return array.front()
|
||||
@@ -49,10 +44,6 @@ func wraparound():
|
||||
var direction = global_position.direction_to(player.global_position)
|
||||
global_position += direction * max_distance*2
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
func _process(delta: float) -> void:
|
||||
look_at(player.global_position)
|
||||
#print(player.global_position)
|
||||
@@ -61,29 +52,3 @@ func _process(delta: float) -> void:
|
||||
wraparound()
|
||||
|
||||
|
||||
"""
|
||||
if (global_position.x-player.global_position.x)>600:#player to the right
|
||||
global_position.x=player.global_position.x-600#move right
|
||||
elif(global_position.x-player.global_position.x)<-600:#player to the left
|
||||
global_position.x=player.global_position.x+600#move left
|
||||
|
||||
if (global_position.y-player.global_position.y)>600:#player to the up
|
||||
global_position.y=player.global_position.y-600#move up
|
||||
elif(global_position.y-player.global_position.y)<-600:#player to the down
|
||||
global_position.y=player.global_position.y+600#move down
|
||||
"""
|
||||
"""
|
||||
func _physics_process(delta: float) -> void:
|
||||
# Add the gravity.
|
||||
if not is_on_floor():
|
||||
velocity += get_gravity() * delta
|
||||
# Get the input direction and handle the movement/deceleration.
|
||||
# As good practice, you should replace UI actions with custom gameplay actions.
|
||||
var direction := Input.get_axis("ui_left", "ui_right")
|
||||
if direction:
|
||||
velocity.x = direction * SPEED
|
||||
else:
|
||||
velocity.x = move_toward(velocity.x, 0, SPEED)
|
||||
|
||||
move_and_slide()
|
||||
"""
|
||||
|
||||
@@ -17,7 +17,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
|
||||
#OLD: used when player shield direction aigned 90 deg off from front
|
||||
# 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
|
||||
|
||||
@@ -1,45 +1,32 @@
|
||||
extends Area2D
|
||||
|
||||
var debug_normal: Vector2 = Vector2.ZERO
|
||||
var bounceTrajectory: Vector2
|
||||
|
||||
|
||||
# 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)
|
||||
print("Event: Projectile hit shield")
|
||||
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)
|
||||
print("Readout: Shield normal: ", Vec2str(shield_normal))
|
||||
# Gets the direction from the parent Node2D NOT the Area2d component
|
||||
var projectile_velocity = area.get_parent().get_facing_direction()
|
||||
print("Readout: Projectile dir: ", Vec2str(projectile_velocity))
|
||||
var bounceTrajectory = projectile_velocity.bounce(shield_normal)
|
||||
print("Readout: Bounce dir: ", Vec2str(bounceTrajectory))
|
||||
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)
|
||||
|
||||
# Debug functions
|
||||
func _draw():
|
||||
# draw_line(Vector2(1,0) * 1000, debug_normal * 50, Color.RED, 2.0) # normal
|
||||
pass
|
||||
|
||||
# Called every frame. 'delta' is the elapsed time since the previous frame.
|
||||
#func _process(delta: float) -> void:
|
||||
# pass
|
||||
# Makes the string a bit easier to read in debug console
|
||||
func Vec2str(vec: Vector2) -> String:
|
||||
var x_sign = " " if vec.x >= 0 else ""
|
||||
var y_sign = " " if vec.y >= 0 else ""
|
||||
|
||||
return "(%s%.2f, %s%.2f)" % [x_sign, vec.x, y_sign, vec.y]
|
||||
|
||||
Reference in New Issue
Block a user