Collisions and sounds

master
DJh2o2 2024-02-23 10:10:03 +07:00
parent dc05a9b974
commit 1fb41ba5cc
8 changed files with 113 additions and 93 deletions

@ -1,9 +1,10 @@
<Project Sdk="Godot.NET.Sdk/4.3.0-dev.3">
<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<TargetFramework Condition=" '$(GodotTargetPlatform)' == 'android' ">net7.0</TargetFramework>
<TargetFramework Condition=" '$(GodotTargetPlatform)' == 'ios' ">net8.0</TargetFramework>
<EnableDynamicLoading>true</EnableDynamicLoading>
<RootNamespace>Pong</RootNamespace>
</PropertyGroup>
<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<TargetFramework Condition=" '$(GodotTargetPlatform)' == 'android' ">net7.0</TargetFramework>
<TargetFramework Condition=" '$(GodotTargetPlatform)' == 'ios' ">net8.0</TargetFramework>
<EnableDynamicLoading>true</EnableDynamicLoading>
<RootNamespace>Pong</RootNamespace>
<LangVersion>default</LangVersion>
</PropertyGroup>
</Project>

@ -1,22 +0,0 @@
using System;
using Godot;
public partial class Area : Area2D
{
public override void _PhysicsProcess(double delta) {
// BodyEntered += OnBodyEntered;
// BodyExited += OnBodyExited;
}
private void OnBodyEntered(Node2D body) {
if (body is Ball) {
GD.Print("Ball entered the area!");
}
}
private void OnBodyExited(Node2D body) {
if (body is Ball) {
GD.Print("Ball left the area!");
}
}
}

@ -1,19 +1,19 @@
using Godot;
using Godot.Collections;
public partial class Ball : Node2D {
private int direction = 1;
[Export] private int speed = 500;
[Export] private Area2D area;
public partial class Ball : RigidBody2D {
[Export] private AudioStreamPlayer audioStreamPlayer;
[Export] private Array<AudioStream> sounds;
private Vector2 velocity;
public override void _Ready() {
}
public override void _Ready() => velocity = new(500, GD.RandRange(-100, 100));
public override void _Process(double delta) {
Vector2 position = Position;
position.X += Mathf.RoundToInt(direction * speed * delta);
Position = position;
public override void _PhysicsProcess(double delta) {
KinematicCollision2D collision = MoveAndCollide(velocity * (float)delta);
if (collision != null) {
velocity = velocity.Bounce(collision.GetNormal());
audioStreamPlayer.Stream = collision.GetCollider() is Player ? sounds[1] : sounds[0];
audioStreamPlayer.Play();
}
}
public void ChangeDirection() => direction *= -1;
}

@ -1,12 +1,11 @@
using Godot;
using System;
public partial class GameManager : Node2D {
[Export] private Player player1;
[Export] private Player player2;
[Export] private Ball ball;
// [Export] private Ball ball;
// [Export] private Player player1;
// [Export] private Player player2;
public override void _Ready() {
// ball.ChangeDirection();
}
}
}

@ -1,21 +1,29 @@
using Godot;
public partial class Player : Node2D {
public partial class Player : CharacterBody2D {
[Export] private int playerNumber;
[Export] private int speed = 600;
[Export] private int speed = 10000;
public override void _Process(double delta) {
Vector2 position = Position;
if (Input.IsActionPressed("Player1Up") && playerNumber == 1) position.Y -= Mathf.RoundToInt(speed * delta);
else if (Input.IsActionPressed("Player1Down") && playerNumber == 1) position.Y += Mathf.RoundToInt(speed * delta);
if (Input.IsActionPressed("Player2Up") && playerNumber == 2) position.Y -= Mathf.RoundToInt(speed * delta);
else if (Input.IsActionPressed("Player2Down") && playerNumber == 2) position.Y += Mathf.RoundToInt(speed * delta);
public override void _PhysicsProcess(double delta) {
Vector2 velocity = Velocity;
if (position.Y > 520) position.Y = 520;
if (position.Y < 0) position.Y = 0;
Position = position;
if (Input.IsAnythingPressed()) {
if (Input.IsActionPressed("Player1Up") && playerNumber == 1) velocity.Y = Mathf.RoundToInt(-speed * delta);
else if (Input.IsActionPressed("Player1Down") && playerNumber == 1) velocity.Y = Mathf.RoundToInt(speed * delta);
if (Input.IsActionPressed("Player2Up") && playerNumber == 2) velocity.Y = Mathf.RoundToInt(-speed * delta);
else if (Input.IsActionPressed("Player2Down") && playerNumber == 2) velocity.Y = Mathf.RoundToInt(speed * delta);
Velocity = velocity;
}
else {
Velocity = Vector2.Zero;
}
KinematicCollision2D collision = MoveAndCollide(velocity * (float)delta);
if (collision?.GetCollider() is StaticBody2D staticBody2D) {
Velocity = Vector2.Zero;
}
}
}
}

@ -1,20 +1,25 @@
[gd_scene load_steps=3 format=3 uid="uid://bxkg2ri346c2o"]
[gd_scene load_steps=5 format=3 uid="uid://bxkg2ri346c2o"]
[ext_resource type="Texture2D" uid="uid://c5jcxyyyd0m60" path="res://Ball.png" id="1_ihs8s"]
[ext_resource type="Script" path="res://Ball.cs" id="1_jhuit"]
[ext_resource type="AudioStream" uid="uid://dg8y8aw5j5m1d" path="res://pong1.wav" id="2_w88js"]
[ext_resource type="AudioStream" uid="uid://ds2fe4cdlwv05" path="res://pong2.wav" id="3_dh6iu"]
[node name="Ball" type="Node2D"]
script = ExtResource("1_jhuit")
[node name="RigidBody2D" type="RigidBody2D" parent="."]
[node name="Ball" type="RigidBody2D" node_paths=PackedStringArray("audioStreamPlayer")]
mass = 0.1
gravity_scale = 0.0
script = ExtResource("1_jhuit")
audioStreamPlayer = NodePath("AudioStreamPlayer")
sounds = Array[AudioStream]([ExtResource("2_w88js"), ExtResource("3_dh6iu")])
[node name="Sprite2D" type="Sprite2D" parent="RigidBody2D"]
[node name="Sprite2D" type="Sprite2D" parent="."]
position = Vector2(16, 16)
scale = Vector2(1, 1.14)
texture = ExtResource("1_ihs8s")
[node name="CollisionPolygon2D" type="CollisionPolygon2D" parent="RigidBody2D"]
[node name="CollisionPolygon2D" type="CollisionPolygon2D" parent="."]
position = Vector2(16, 16)
scale = Vector2(1, 1.14)
polygon = PackedVector2Array(16, -11.7, 16, 10, 10.7, 16, -11, 16, -16, 11.8, -16, -10, -10, -15.8, 10, -16)
[node name="AudioStreamPlayer" type="AudioStreamPlayer" parent="."]

@ -1,18 +1,18 @@
[gd_scene load_steps=3 format=3 uid="uid://dbgbygehmmopa"]
[gd_scene load_steps=4 format=3 uid="uid://dbgbygehmmopa"]
[ext_resource type="Script" path="res://Player.cs" id="1_cs5ta"]
[ext_resource type="Texture2D" uid="uid://1aivwap6lvr4" path="res://Player.png" id="2_jdmud"]
[node name="Player1" type="Node2D"]
script = ExtResource("1_cs5ta")
[sub_resource type="CircleShape2D" id="CircleShape2D_bj6vt"]
radius = 64.0
[node name="RigidBody2D" type="RigidBody2D" parent="."]
gravity_scale = 0.0
[node name="Player" type="CharacterBody2D"]
script = ExtResource("1_cs5ta")
[node name="Sprite2D" type="Sprite2D" parent="RigidBody2D"]
[node name="Sprite2D" type="Sprite2D" parent="."]
position = Vector2(0, 64)
texture = ExtResource("2_jdmud")
[node name="CollisionPolygon2D" type="CollisionPolygon2D" parent="RigidBody2D"]
[node name="CollisionShape2D" type="CollisionShape2D" parent="."]
position = Vector2(0, 64)
polygon = PackedVector2Array(5, -63.7, 5, 64, -4, 64, -4, 63.3, -5, 61, -5, -61.1, -4, -63.4, -4, -64, 4.2, -64)
shape = SubResource("CircleShape2D_bj6vt")

@ -3,36 +3,65 @@
[ext_resource type="PackedScene" uid="uid://dbgbygehmmopa" path="res://player.tscn" id="1_7bgs4"]
[ext_resource type="Script" path="res://GameManager.cs" id="1_284o0"]
[ext_resource type="PackedScene" uid="uid://bxkg2ri346c2o" path="res://ball.tscn" id="2_dcxy0"]
[ext_resource type="Script" path="res://Area.cs" id="4_gu0cb"]
[ext_resource type="Texture2D" uid="uid://d08fnpfoa3vsl" path="res://Wall.png" id="4_elhby"]
[sub_resource type="RectangleShape2D" id="RectangleShape2D_r6erp"]
size = Vector2(1152, 648)
[sub_resource type="RectangleShape2D" id="RectangleShape2D_2o851"]
size = Vector2(18, 648)
[node name="Pong" type="Node2D"]
[node name="GameManager" type="Node2D" parent="." node_paths=PackedStringArray("player1", "player2", "ball")]
[node name="GameManager" type="Node2D" parent="."]
script = ExtResource("1_284o0")
player1 = NodePath("../Player1")
player2 = NodePath("../Player2")
ball = NodePath("../Ball")
[node name="Player1" parent="." instance=ExtResource("1_7bgs4")]
position = Vector2(64, 264)
playerNumber = 1
[node name="Player2" parent="." instance=ExtResource("1_7bgs4")]
position = Vector2(1080, 256)
playerNumber = 2
[node name="Ball" parent="." instance=ExtResource("2_dcxy0")]
position = Vector2(80, 216)
[node name="Area2D" type="Area2D" parent="."]
script = ExtResource("4_gu0cb")
[node name="Walls" type="Node2D" parent="."]
[node name="CollisionShape2D" type="CollisionShape2D" parent="Area2D"]
position = Vector2(576, 324)
shape = SubResource("RectangleShape2D_r6erp")
[node name="WallLeft" type="Sprite2D" parent="Walls"]
position = Vector2(8, 325)
texture = ExtResource("4_elhby")
[connection signal="body_entered" from="Area2D" to="Area2D" method="OnBodyEntered"]
[connection signal="body_exited" from="Area2D" to="Area2D" method="OnBodyExited"]
[node name="StaticBody2D" type="StaticBody2D" parent="Walls/WallLeft"]
[node name="CollisionShape2D" type="CollisionShape2D" parent="Walls/WallLeft/StaticBody2D"]
shape = SubResource("RectangleShape2D_2o851")
[node name="WallRight" type="Sprite2D" parent="Walls"]
position = Vector2(1144, 325)
texture = ExtResource("4_elhby")
[node name="StaticBody2D" type="StaticBody2D" parent="Walls/WallRight"]
[node name="CollisionShape2D" type="CollisionShape2D" parent="Walls/WallRight/StaticBody2D"]
shape = SubResource("RectangleShape2D_2o851")
[node name="WallTop" type="Sprite2D" parent="Walls"]
position = Vector2(576, 8)
rotation = 1.5708
scale = Vector2(1, 1.78)
texture = ExtResource("4_elhby")
[node name="StaticBody2D" type="StaticBody2D" parent="Walls/WallTop"]
[node name="CollisionShape2D" type="CollisionShape2D" parent="Walls/WallTop/StaticBody2D"]
shape = SubResource("RectangleShape2D_2o851")
[node name="WallGround" type="Sprite2D" parent="Walls"]
position = Vector2(576, 640)
rotation = 1.5708
scale = Vector2(1, 1.78)
texture = ExtResource("4_elhby")
[node name="StaticBody2D" type="StaticBody2D" parent="Walls/WallGround"]
[node name="CollisionShape2D" type="CollisionShape2D" parent="Walls/WallGround/StaticBody2D"]
shape = SubResource("RectangleShape2D_2o851")
[node name="AudioListener2D" type="AudioListener2D" parent="."]