Week two! Went off-script a bit...

This commit is contained in:
AnicaCihla 2025-04-14 20:56:26 -04:00
parent ffd0c3f70e
commit c3f5a6116e
14 changed files with 223 additions and 71 deletions

View File

@ -14,3 +14,16 @@ config/name="learning"
run/main_scene="res://scenes/node_2d.tscn"
config/features=PackedStringArray("4.4", "Forward Plus")
config/icon="res://icon.svg"
[input]
attract={
"deadzone": 0.2,
"events": [Object(InputEventMouseButton,"resource_local_to_scene":false,"resource_name":"","device":-1,"window_id":0,"alt_pressed":false,"shift_pressed":false,"ctrl_pressed":false,"meta_pressed":false,"button_mask":0,"position":Vector2(0, 0),"global_position":Vector2(0, 0),"factor":1.0,"button_index":2,"canceled":false,"pressed":false,"double_click":false,"script":null)
]
}
shoot={
"deadzone": 0.2,
"events": [Object(InputEventMouseButton,"resource_local_to_scene":false,"resource_name":"","device":-1,"window_id":0,"alt_pressed":false,"shift_pressed":false,"ctrl_pressed":false,"meta_pressed":false,"button_mask":0,"position":Vector2(0, 0),"global_position":Vector2(0, 0),"factor":1.0,"button_index":1,"canceled":false,"pressed":false,"double_click":false,"script":null)
]
}

14
scenes/ball.tscn Normal file
View File

@ -0,0 +1,14 @@
[gd_scene load_steps=3 format=3 uid="uid://cdyodb8q2mkwd"]
[ext_resource type="Script" uid="uid://doqrmlk7jvot7" path="res://scripts/ball.gd" id="1_7s4qf"]
[sub_resource type="CircleShape2D" id="CircleShape2D_rf3rd"]
radius = 24.0
[node name="RigidBody2D" type="RigidBody2D"]
script = ExtResource("1_7s4qf")
metadata/_edit_group_ = true
[node name="CollisionShape2D" type="CollisionShape2D" parent="."]
shape = SubResource("CircleShape2D_rf3rd")
debug_color = Color(0.809626, 0.453417, 0.723759, 0.42)

14
scenes/bullet.tscn Normal file
View File

@ -0,0 +1,14 @@
[gd_scene load_steps=3 format=3 uid="uid://ohcwdeyx018t"]
[ext_resource type="Script" uid="uid://dkr6ap7argaf3" path="res://scripts/bullet.gd" id="1_mkf8s"]
[sub_resource type="CircleShape2D" id="CircleShape2D_y25gk"]
[node name="Area2D" type="Area2D"]
script = ExtResource("1_mkf8s")
[node name="CollisionShape2D" type="CollisionShape2D" parent="."]
shape = SubResource("CircleShape2D_y25gk")
debug_color = Color(0.557768, 0.541597, 0.413157, 0.42)
[connection signal="body_entered" from="." to="." method="_on_body_entered"]

View File

@ -1,19 +1,14 @@
[gd_scene load_steps=10 format=3 uid="uid://f0wep3w30dh8"]
[ext_resource type="Script" uid="uid://ct2t7c0om0cdh" path="res://scripts/gamecontroller.gd" id="1_4dvvg"]
[ext_resource type="Script" uid="uid://cmrdtps7txgxx" path="res://scripts/character_body_2d.gd" id="1_6e84v"]
[ext_resource type="Script" uid="uid://dnx7g8ts5f4uc" path="res://scripts/scene_manager.gd" id="2_4vge2"]
[ext_resource type="PackedScene" uid="uid://6vl0c70v1gqs" path="res://scenes/player.tscn" id="2_rnjyy"]
[ext_resource type="PackedScene" uid="uid://cdyodb8q2mkwd" path="res://scenes/ball.tscn" id="3_04qyp"]
[ext_resource type="PackedScene" uid="uid://dxj2q0disdlb5" path="res://scenes/area_2d.tscn" id="3_rnjyy"]
[sub_resource type="RectangleShape2D" id="RectangleShape2D_rf3rd"]
size = Vector2(433.759, 40)
[sub_resource type="CircleShape2D" id="CircleShape2D_rf3rd"]
radius = 24.0
[sub_resource type="CapsuleShape2D" id="CapsuleShape2D_rf3rd"]
radius = 21.0
height = 78.0
[sub_resource type="WorldBoundaryShape2D" id="WorldBoundaryShape2D_6e84v"]
[sub_resource type="WorldBoundaryShape2D" id="WorldBoundaryShape2D_4dvvg"]
@ -22,63 +17,65 @@ normal = Vector2(0.999872, -0.015998)
[sub_resource type="WorldBoundaryShape2D" id="WorldBoundaryShape2D_rnjyy"]
normal = Vector2(-0.999909, -0.0135123)
[node name="Node2D" type="Node2D"]
[node name="Game" type="Node2D"]
script = ExtResource("1_4dvvg")
[node name="StaticBody2D" type="StaticBody2D" parent="."]
position = Vector2(370, 393)
[node name="SceneManager" type="Node" parent="."]
unique_name_in_owner = true
script = ExtResource("2_4vge2")
[node name="level" type="Node2D" parent="."]
[node name="StaticBody2D" type="StaticBody2D" parent="level"]
position = Vector2(347, 337)
rotation = 0.253073
metadata/_edit_group_ = true
[node name="CollisionShape2D" type="CollisionShape2D" parent="StaticBody2D"]
rotation = 0.359538
[node name="CollisionShape2D" type="CollisionShape2D" parent="level/StaticBody2D"]
shape = SubResource("RectangleShape2D_rf3rd")
[node name="RigidBody2D" type="RigidBody2D" parent="."]
position = Vector2(370, 273)
[node name="StaticBody2D2" type="StaticBody2D" parent="level"]
position = Vector2(713, 488)
metadata/_edit_group_ = true
[node name="CollisionShape2D" type="CollisionShape2D" parent="RigidBody2D"]
shape = SubResource("CircleShape2D_rf3rd")
debug_color = Color(1, 0.234871, 0.652107, 0.42)
[node name="StaticBody2D2" type="StaticBody2D" parent="."]
position = Vector2(679, 504)
metadata/_edit_group_ = true
[node name="CollisionShape2D" type="CollisionShape2D" parent="StaticBody2D2"]
rotation = -0.357792
[node name="CollisionShape2D" type="CollisionShape2D" parent="level/StaticBody2D2"]
shape = SubResource("RectangleShape2D_rf3rd")
[node name="CharacterBody2D" type="CharacterBody2D" parent="."]
position = Vector2(623, 366)
script = ExtResource("1_6e84v")
metadata/_edit_group_ = true
[node name="CollisionShape2D" type="CollisionShape2D" parent="CharacterBody2D"]
shape = SubResource("CapsuleShape2D_rf3rd")
debug_color = Color(0.232329, 0.752878, 1.15514e-06, 0.42)
[node name="floor" type="StaticBody2D" parent="."]
[node name="floor" type="StaticBody2D" parent="level"]
position = Vector2(558, 646)
metadata/_edit_group_ = true
[node name="CollisionShape2D" type="CollisionShape2D" parent="floor"]
[node name="CollisionShape2D" type="CollisionShape2D" parent="level/floor"]
shape = SubResource("WorldBoundaryShape2D_6e84v")
[node name="left wall" type="StaticBody2D" parent="."]
[node name="ceiling" type="StaticBody2D" parent="level"]
position = Vector2(569, -1)
rotation = 3.14159
metadata/_edit_group_ = true
[node name="CollisionShape2D" type="CollisionShape2D" parent="level/ceiling"]
shape = SubResource("WorldBoundaryShape2D_6e84v")
[node name="left wall" type="StaticBody2D" parent="level"]
position = Vector2(0, 387)
metadata/_edit_group_ = true
[node name="CollisionShape2D" type="CollisionShape2D" parent="left wall"]
[node name="CollisionShape2D" type="CollisionShape2D" parent="level/left wall"]
shape = SubResource("WorldBoundaryShape2D_4dvvg")
[node name="right wall" type="StaticBody2D" parent="."]
[node name="right wall" type="StaticBody2D" parent="level"]
position = Vector2(1154, 345)
metadata/_edit_group_ = true
[node name="CollisionShape2D" type="CollisionShape2D" parent="right wall"]
[node name="CollisionShape2D" type="CollisionShape2D" parent="level/right wall"]
shape = SubResource("WorldBoundaryShape2D_rnjyy")
[node name="ball" parent="." instance=ExtResource("3_04qyp")]
position = Vector2(370, 273)
[node name="CharacterBody2D" parent="." instance=ExtResource("2_rnjyy")]
position = Vector2(623, 366)
[node name="Area2D" parent="." instance=ExtResource("3_rnjyy")]
position = Vector2(83, 587)

24
scenes/player.tscn Normal file
View File

@ -0,0 +1,24 @@
[gd_scene load_steps=4 format=3 uid="uid://6vl0c70v1gqs"]
[ext_resource type="Script" uid="uid://cmrdtps7txgxx" path="res://scripts/player.gd" id="1_3vyb7"]
[sub_resource type="CapsuleShape2D" id="CapsuleShape2D_rf3rd"]
radius = 21.0
height = 78.0
[sub_resource type="GDScript" id="GDScript_3vyb7"]
script/source = "extends RayCast2D
"
[node name="CharacterBody2D" type="CharacterBody2D"]
script = ExtResource("1_3vyb7")
metadata/_edit_group_ = true
[node name="CollisionShape2D" type="CollisionShape2D" parent="."]
shape = SubResource("CapsuleShape2D_rf3rd")
debug_color = Color(0.232329, 0.752878, 1.15514e-06, 0.42)
[node name="RayCast2D" type="RayCast2D" parent="."]
script = SubResource("GDScript_3vyb7")

1
scripts/ball.gd Normal file
View File

@ -0,0 +1 @@
class_name Ball extends RigidBody2D

1
scripts/ball.gd.uid Normal file
View File

@ -0,0 +1 @@
uid://doqrmlk7jvot7

24
scripts/bullet.gd Normal file
View File

@ -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)

1
scripts/bullet.gd.uid Normal file
View File

@ -0,0 +1 @@
uid://dkr6ap7argaf3

View File

@ -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)

78
scripts/player.gd Normal file
View File

@ -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

15
scripts/scene_manager.gd Normal file
View File

@ -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)

View File

@ -0,0 +1 @@
uid://dnx7g8ts5f4uc