From d532570052901e9e956fab4809228a6b742ee233 Mon Sep 17 00:00:00 2001 From: freddie-a <57713959+freddie-a@users.noreply.github.com> Date: Sun, 30 Oct 2022 09:21:08 +0000 Subject: Rotate player to mouse and rename --- KinematicBody2D.gd | 42 ------------------------------------------ Player.gd | 43 +++++++++++++++++++++++++++++++++++++++++++ Player.tscn | 2 +- 3 files changed, 44 insertions(+), 43 deletions(-) delete mode 100644 KinematicBody2D.gd create mode 100644 Player.gd diff --git a/KinematicBody2D.gd b/KinematicBody2D.gd deleted file mode 100644 index 22f7db1..0000000 --- a/KinematicBody2D.gd +++ /dev/null @@ -1,42 +0,0 @@ -extends Area2D - -signal hit -signal coin -var screen_size -export (int) var speed = 500 -var spawn_object = load("res://Bullet.tscn") -var velocity = Vector2() -onready var globals = get_node("/root/Global") - -func get_input(): - velocity = Vector2() - if Input.is_action_pressed("right"): - velocity.x += 1 - if Input.is_action_pressed("left"): - velocity.x -= 1 - if Input.is_action_pressed("down"): - velocity.y += 1 - if Input.is_action_pressed("up"): - velocity.y -= 1 - velocity = velocity.normalized() * speed - -func _ready(): - screen_size = get_viewport_rect().size - -func _physics_process(delta): - get_input() - globals.playerPos = global_position - position += velocity * delta - position.x = clamp(position.x, 0, screen_size.x) - position.y = clamp(position.y, 0, screen_size.y) - if Input.is_action_just_pressed("click"): - var obj = spawn_object.instance() - obj.position = get_position() - get_parent().add_child(obj) - var bodies = get_overlapping_bodies() - for body in bodies: - if "Coin" in body.get_name(): - emit_signal("coin") - body.free() - elif "Enemy" in body.get_name(): - emit_signal("hit", delta) diff --git a/Player.gd b/Player.gd new file mode 100644 index 0000000..9b5bbd6 --- /dev/null +++ b/Player.gd @@ -0,0 +1,43 @@ +extends Area2D + +signal hit +signal coin +var screen_size +export (int) var speed = 500 +var spawn_object = load("res://Bullet.tscn") +var velocity = Vector2() +onready var globals = get_node("/root/Global") + +func get_input(): + velocity = Vector2() + if Input.is_action_pressed("right"): + velocity.x += 1 + if Input.is_action_pressed("left"): + velocity.x -= 1 + if Input.is_action_pressed("down"): + velocity.y += 1 + if Input.is_action_pressed("up"): + velocity.y -= 1 + velocity = velocity.normalized() * speed + +func _ready(): + screen_size = get_viewport_rect().size + +func _physics_process(delta): + get_input() + globals.playerPos = global_position + position += velocity * delta + position.x = clamp(position.x, 0, screen_size.x) + position.y = clamp(position.y, 0, screen_size.y) + rotation = global_position.angle_to_point(get_global_mouse_position()) + PI + PI / 8 # extra /8 to offset TeX's snout + if Input.is_action_just_pressed("click"): + var obj = spawn_object.instance() + obj.position = get_position() + get_parent().add_child(obj) + var bodies = get_overlapping_bodies() + for body in bodies: + if "Coin" in body.get_name(): + emit_signal("coin") + body.free() + elif "Enemy" in body.get_name(): + emit_signal("hit", delta) diff --git a/Player.tscn b/Player.tscn index 4dcae32..8491e22 100644 --- a/Player.tscn +++ b/Player.tscn @@ -1,6 +1,6 @@ [gd_scene load_steps=3 format=2] -[ext_resource path="res://KinematicBody2D.gd" type="Script" id=1] +[ext_resource path="res://Player.gd" type="Script" id=1] [ext_resource path="res://assets/htm_tex.png" type="Texture" id=2] [node name="Area2D" type="Area2D"] -- cgit v1.2.3-70-g09d2