Collisions and sounds
parent
dc05a9b974
commit
1fb41ba5cc
@ -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")
|
||||
|
||||
Loading…
Reference in New Issue