https://files.fm/f/4dg84ghff
heres a link to the video.
The weapon is an inheritable scene that is inherited by other weapons. They all have the code to flip the sprite image horizontally towards to the direction of the mouse from the player body. It also has the code that allows it to move around the player which works well but for some reason the flip doesnt work when it is instanced by the player but when played as just the scene (F6) it works properly as seen from the start of the video.
edit:
my bad, newbie here sorry, here's the code for the player spawning the weapons.
extends KinematicBody2D
const BASED_MOVEMENT_SPEED = 10 * 16
var desired_velocity = Vector2.ZERO
var velocity = Vector2.ZERO
var direction = Vector2.ZERO
var weight = 0.3
var weapon_change = "sword";
var weapons: = {
sword = preload("res://Scenes/Weapon.tscn"),
rock = preload("res://Scenes/Rock.tscn"),
claw = preload("res://Scenes/Claw.tscn")
}
onready var target = Vector2.ZERO
onready var weapon_position = $weapon_manager
func _process(delta):
_movement_input()
_weapon_movement()
func _weapon_change():
if (Input.is_action_just_pressed("Switch_Item1")):
weapon_change = "sword"
elif (Input.is_action_just_pressed("Switch_Item2")):
weapon_change = "rock"
elif (Input.is_action_just_pressed("Switch_Item3")):
weapon_change = "claw"
func _movement_input():
direction = Vector2.ZERO
direction.x = -int(Input.is_action_pressed("ui_left")) + int(Input.is_action_pressed("ui_right"))
direction.y = -int(Input.is_action_pressed("ui_up")) + int(Input.is_action_pressed("ui_down"))
desired_velocity = direction.normalized() * BASED_MOVEMENT_SPEED
velocity = velocity.linear_interpolate(desired_velocity, weight)
velocity = move_and_slide(velocity)
func _weapon_movement():
_weapon_change()
for weapon in weapon_position.get_children():
weapon.queue_free()
var weapon_instance = weapons[weapon_change].instance()
weapon_position.add_child(weapon_instance)
target = get_global_mouse_position()
weapon_position.look_at(target)
And Here's the script for the weapon that is instanced by the player
extends Node2D
onready var target = Vector2.ZERO
# Called when the node enters the scene tree for the first time.
func _process(delta):
target = get_global_mouse_position()
if target.x > self.position.x:
get_node("Sprite").set_flip_v(false)
elif target.x < self.position.x:
get_node("Sprite").set_flip_v(true)