Set up a revised player scene with a component based apparoach; shooting
functionality incomplete.
This commit is contained in:
parent
8d89271074
commit
9b3841775c
13 changed files with 331 additions and 0 deletions
11
scripts/input_component.gd
Normal file
11
scripts/input_component.gd
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
class_name InputComponent extends Node
|
||||
|
||||
var move_dir: Vector2 = Vector2.ZERO
|
||||
var shoot_pressed: bool = false
|
||||
var shooting: bool = false
|
||||
|
||||
|
||||
func update() -> void:
|
||||
move_dir = Input.get_vector("left", "right", "up", "down")
|
||||
shoot_pressed = Input.is_action_just_pressed("shoot")
|
||||
shooting = Input.is_action_pressed("shoot")
|
||||
1
scripts/input_component.gd.uid
Normal file
1
scripts/input_component.gd.uid
Normal file
|
|
@ -0,0 +1 @@
|
|||
uid://dss0dbwr71y6m
|
||||
46
scripts/movement_component.gd
Normal file
46
scripts/movement_component.gd
Normal file
|
|
@ -0,0 +1,46 @@
|
|||
class_name MovementComponent extends Node
|
||||
|
||||
@export var player: Area2D
|
||||
@export var speed: float = 275
|
||||
@export var bounds_x: int = 12
|
||||
@export var bounds_y: int = 12
|
||||
|
||||
@onready var weapon_component: WeaponComponent = %WeaponComponent
|
||||
|
||||
|
||||
@onready var screensize = get_viewport().content_scale_size
|
||||
|
||||
var input = Input.get_vector("left", "right", "up","down")
|
||||
|
||||
func tick(delta: float):
|
||||
# Check to see if there's a player to move
|
||||
if player == null: return
|
||||
|
||||
# Calculate ship to bullet displacemnt
|
||||
player.ship_displacement = player.position.y - player.previous_position.y
|
||||
player.travel += abs(weapon_component.bullet_data.speed) * delta + (player.ship_displacement)
|
||||
player.travel = clamp(player.travel, 0, 1.9 * weapon_component.bullet_data.projectile_spacing)
|
||||
|
||||
# Thruster animation in relation to player movement
|
||||
if input.x > 0:
|
||||
player.position.x = player.position.x+1
|
||||
%Ship.frame = 2
|
||||
%Ship/Thrusters.flip_h = true
|
||||
%Ship/Thrusters.animation = "banked"
|
||||
elif input.x < 0:
|
||||
player.position.x = player.position.x-1
|
||||
%Ship.frame = 1
|
||||
%Ship/Thrusters.flip_h = false
|
||||
%Ship/Thrusters.animation = "banked"
|
||||
else:
|
||||
%Ship.frame = 0
|
||||
player.position.x = player.position.x
|
||||
%Ship/Thrusters.flip_h = false
|
||||
%Ship/Thrusters.animation = "fwd"
|
||||
|
||||
# Get previous positon for equidistant bullet calculation
|
||||
player.previous_position = player.position
|
||||
|
||||
# Move player within screenbounds
|
||||
player.position += input * speed * delta
|
||||
player.position = player.position.clamp(Vector2i(bounds_x,bounds_y), screensize - Vector2i(bounds_x,bounds_y))
|
||||
1
scripts/movement_component.gd.uid
Normal file
1
scripts/movement_component.gd.uid
Normal file
|
|
@ -0,0 +1 @@
|
|||
uid://c0rikbakpcags
|
||||
33
scripts/player_revision.gd
Normal file
33
scripts/player_revision.gd
Normal file
|
|
@ -0,0 +1,33 @@
|
|||
class_name Player extends Area2D
|
||||
|
||||
@onready var input_component: InputComponent = %InputComponent
|
||||
@onready var movement_component: MovementComponent = %MovementComponent
|
||||
@onready var shoot_component: ShootComponent = %ShootComponent
|
||||
@onready var weapon_component: WeaponComponent = %WeaponComponent
|
||||
|
||||
@export var can_shoot: bool = true
|
||||
@export var is_shooting: bool = false
|
||||
|
||||
var previous_position: Vector2
|
||||
var ship_displacement: float
|
||||
var travel: float = 0
|
||||
|
||||
|
||||
func _process(delta) -> void:
|
||||
|
||||
# Read Controls
|
||||
input_component.update()
|
||||
|
||||
# Read Movement Component
|
||||
movement_component.input = input_component.move_dir
|
||||
movement_component.tick(delta)
|
||||
|
||||
# Set up shooting
|
||||
if input_component.shooting:
|
||||
is_shooting = true
|
||||
if travel > weapon_component.bullet_data.projectile_spacing:
|
||||
shoot_component.shoot()
|
||||
travel -= weapon_component.bullet_data.projectile_spacing
|
||||
|
||||
else: is_shooting = false
|
||||
|
||||
1
scripts/player_revision.gd.uid
Normal file
1
scripts/player_revision.gd.uid
Normal file
|
|
@ -0,0 +1 @@
|
|||
uid://dulfr27gg4evl
|
||||
34
scripts/shoot_component.gd
Normal file
34
scripts/shoot_component.gd
Normal file
|
|
@ -0,0 +1,34 @@
|
|||
class_name ShootComponent extends Node
|
||||
|
||||
@onready var weapon_component: Node = %WeaponComponent
|
||||
@onready var player: Player = $".."
|
||||
|
||||
var travel: float = 0
|
||||
var weapon_spacing: float = 0.1
|
||||
|
||||
|
||||
func shoot():
|
||||
|
||||
# Continuous fire
|
||||
if player.is_shooting == true:
|
||||
|
||||
# Adjust bullet spacing before firing
|
||||
|
||||
|
||||
for b in range(weapon_component.bullet_data.projectiles):
|
||||
|
||||
# Instantiate the bullets
|
||||
var bullet = weapon_component.weapon_current.instantiate()
|
||||
get_tree().root.add_child(bullet)
|
||||
bullet.position = player.position + Vector2((b - (weapon_component.bullet_data.projectiles - 1) / 2.0) * weapon_component.bullet_data.projectile_spacing, weapon_component.bullet_data.projectile_origin)
|
||||
|
||||
## Get weapon speed and spacing for equidistant calculations
|
||||
#weapon_rate = bullet.weapon_component.bullet_data.rate
|
||||
#weapon_spacing = bullet.weapon_component.bullet_data.projectile_spacing
|
||||
#bullet.weapon_component.bullet_data.speed = bullet.weapon_component.bullet_data.speed
|
||||
|
||||
# Print which weapon is firing
|
||||
prints(bullet.shot_data.shot_name, "weapon fired")
|
||||
|
||||
|
||||
|
||||
1
scripts/shoot_component.gd.uid
Normal file
1
scripts/shoot_component.gd.uid
Normal file
|
|
@ -0,0 +1 @@
|
|||
uid://bgd1hwindc2ui
|
||||
21
scripts/weapon_component.gd
Normal file
21
scripts/weapon_component.gd
Normal file
|
|
@ -0,0 +1,21 @@
|
|||
class_name WeaponComponent extends Node
|
||||
|
||||
@export var weapon_current : PackedScene
|
||||
#@export var projectile_origin: int = -23
|
||||
var bullet_data: PlayerShot = load("res://resources/player_weapons/shot_stock_static.tres")
|
||||
|
||||
#@export_category("Temp Bullet Data")
|
||||
#@export var projectiles: int = 3
|
||||
#@export var projectile_spacing: float = 10
|
||||
|
||||
#var damage: int = 0
|
||||
#var speed: int = 135
|
||||
#var projectiles: int = 2
|
||||
#var rate: float = 0.1
|
||||
#var cooldown: float = 0.25
|
||||
#var projectile_spacing: float = 10
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
1
scripts/weapon_component.gd.uid
Normal file
1
scripts/weapon_component.gd.uid
Normal file
|
|
@ -0,0 +1 @@
|
|||
uid://ylmao2ndp22y
|
||||
Loading…
Add table
Add a link
Reference in a new issue