Week two! Went off-script a bit...
This commit is contained in:
1 parent
ffd0c3f70e
commit
c3f5a6116e
14 files changed
+223
-71
No files matched your search
@@ -0,0 +1 @@
|
||||
class_name Ball extends RigidBody2D
|
||||
@@ -0,0 +1 @@
|
||||
uid://doqrmlk7jvot7
|
||||
@@ -0,0 +1,24 @@
|
||||
class_name Bullet extends Area2D
|
||||
|
||||
const BULLET_SCENE: PackedScene = preload("res://scenes/bullet.tscn")
|
||||
@export var SPEED = 700
|
||||
var bearing: Vector2 = Vector2(1,0)
|
||||
|
||||
signal bulletHit(body, bullet)
|
||||
|
||||
static func create(pos: Vector2, bearing: Vector2) -> Bullet:
|
||||
var instance = BULLET_SCENE.instantiate()
|
||||
instance.position = pos
|
||||
instance.bearing = bearing
|
||||
return instance
|
||||
|
||||
func _physics_process(delta: float) -> void:
|
||||
position += bearing * SPEED * delta
|
||||
if position.x < 0 || position.x > 1152:
|
||||
bearing.x *= -1
|
||||
if position.y < 0 || position.y > 648:
|
||||
bearing.y *= -1
|
||||
|
||||
func _on_body_entered(body: Node2D) -> void:
|
||||
if not body is Player:
|
||||
bulletHit.emit(body, self)
|
||||
@@ -0,0 +1 @@
|
||||
uid://dkr6ap7argaf3
|
||||
@@ -1,31 +0,0 @@
|
||||
extends CharacterBody2D
|
||||
|
||||
|
||||
const SPEED = 300.0
|
||||
const JUMP_VELOCITY = -400.0
|
||||
|
||||
const PUSH = 100.0
|
||||
|
||||
|
||||
func _physics_process(delta: float) -> void:
|
||||
# Add the gravity.
|
||||
if not is_on_floor():
|
||||
velocity += get_gravity() * delta
|
||||
|
||||
# Handle jump.
|
||||
if Input.is_action_just_pressed("ui_accept") and is_on_floor():
|
||||
velocity.y = JUMP_VELOCITY
|
||||
|
||||
# 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()
|
||||
for i in get_slide_collision_count():
|
||||
var c = get_slide_collision(i)
|
||||
if c.get_collider() is RigidBody2D:
|
||||
c.get_collider().apply_central_impulse(-c.get_normal() * PUSH)
|
||||
@@ -0,0 +1,78 @@
|
||||
class_name Player extends CharacterBody2D
|
||||
|
||||
@onready var ray_cast_2d: RayCast2D = $RayCast2D
|
||||
@onready var game: GameController = $".."
|
||||
|
||||
const SPEED = 300.0
|
||||
const JUMP_VELOCITY = -400.0
|
||||
|
||||
const PUSH = 100.0
|
||||
|
||||
enum InteractionStates {NONE, ATTRACT, REPEL}
|
||||
var interactionState = InteractionStates.NONE
|
||||
|
||||
func _physics_process(delta: float) -> void:
|
||||
# Add the gravity.
|
||||
if not is_on_floor():
|
||||
velocity += get_gravity() * delta
|
||||
|
||||
handle_input()
|
||||
|
||||
move_and_slide()
|
||||
|
||||
handle_collisions()
|
||||
|
||||
func handle_input() -> void:
|
||||
# Handle jump.
|
||||
if Input.is_action_just_pressed("ui_up") and is_on_floor():
|
||||
velocity.y = JUMP_VELOCITY
|
||||
|
||||
# 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)
|
||||
|
||||
# Follow mouse cursor with raycast.
|
||||
var target = get_local_mouse_position()
|
||||
ray_cast_2d.target_position = target
|
||||
|
||||
# Handle attract/repel inputs, or reset to no interaction
|
||||
if Input.is_action_just_pressed("attract"):
|
||||
interactionState = InteractionStates.ATTRACT
|
||||
#elif Input.is_action_just_pressed("repel"):
|
||||
#interactionState = InteractionStates.REPEL
|
||||
else:
|
||||
interactionState = InteractionStates.NONE
|
||||
|
||||
# Shoot bullets
|
||||
if Input.is_action_just_pressed("shoot"):
|
||||
var dir = (get_global_mouse_position() - global_position).normalized()
|
||||
%SceneManager.makeBullet(global_position, dir)
|
||||
|
||||
func update_movement() -> void:
|
||||
pass
|
||||
|
||||
func update_states() -> void:
|
||||
pass
|
||||
|
||||
func handle_collisions() -> void:
|
||||
#for i in get_slide_collision_count():
|
||||
#var c = get_slide_collision(i)
|
||||
#if c.get_collider() is RigidBody2D:
|
||||
#c.get_collider().apply_central_impulse(-c.get_normal() * PUSH)
|
||||
|
||||
if ray_cast_2d.is_colliding():
|
||||
var collider = ray_cast_2d.get_collider()
|
||||
if collider is RigidBody2D:
|
||||
var angle = collider.get_angle_to(to_local(position))
|
||||
var direction = Vector2.RIGHT.rotated(angle)
|
||||
match interactionState:
|
||||
InteractionStates.REPEL:
|
||||
collider.apply_central_impulse(direction * PUSH)
|
||||
InteractionStates.ATTRACT:
|
||||
collider.apply_central_impulse((direction * PUSH) * -1)
|
||||
InteractionStates.NONE:
|
||||
pass
|
||||
File renamed without changes.
@@ -0,0 +1,15 @@
|
||||
extends Node
|
||||
|
||||
@onready var game: GameController = $".."
|
||||
#var bullet = preload("res://scenes/bullet.tscn")
|
||||
|
||||
func buildLevel() -> void:
|
||||
pass
|
||||
|
||||
func _ready() -> void:
|
||||
buildLevel()
|
||||
|
||||
func makeBullet(pos, dir) -> void:
|
||||
print("pos: %s \n dir: %s" % [pos, dir])
|
||||
var bullet = Bullet.create(pos, dir)
|
||||
game.add_child(bullet)
|
||||
@@ -0,0 +1 @@
|
||||
uid://dnx7g8ts5f4uc
|
||||
Reference in new issue
Block a user