Add Mono conversion of the Pong demo

Quick and dirty port by a complete C# newbie,
could probably be optimized a bit.
This commit is contained in:
Rémi Verschelde
2017-10-30 19:11:51 +01:00
parent f8ca6d0a4d
commit 0af6d01f94
12 changed files with 341 additions and 0 deletions

38
mono/monkey_pong/Ball.cs Normal file
View File

@@ -0,0 +1,38 @@
using Godot;
using System;
public class Ball : Area2D
{
private const int BALL_SPEED = 100;
private Vector2 direction = new Vector2(-1, 0);
private int speed = BALL_SPEED;
private Vector2 initialPos;
public void SetDirection(Vector2 newDirection)
{
direction = newDirection;
}
public Vector2 GetDirection()
{
return direction;
}
public void Reset()
{
SetPosition(initialPos);
speed = BALL_SPEED;
direction = new Vector2(-1, 0);
}
public override void _Ready()
{
initialPos = GetPosition();
}
public override void _Process(float delta)
{
SetPosition(GetPosition() + direction * speed * delta);
}
}

View File

@@ -0,0 +1,17 @@
using Godot;
using System;
public class CeilingFloor : Area2D
{
[Export]
private int yDirection = 1;
public void OnAreaEntered(Area2D area)
{
if (area is Ball)
{
Ball ball = (Ball)area;
ball.SetDirection(ball.GetDirection() + new Vector2(0, yDirection));
}
}
}

View File

@@ -0,0 +1,39 @@
using Godot;
using System;
public class Paddle : Area2D
{
[Export]
private int ballDir = 1;
private const int MOVE_SPEED = 100;
public override void _Process(float delta)
{
String which = GetName();
// Move up and down based on input
if (Input.IsActionPressed(which + "_move_up") && GetPosition().y > 0)
{
Vector2 pos = GetPosition();
pos.y -= MOVE_SPEED * delta;
SetPosition(pos);
}
if (Input.IsActionPressed(which + "_move_down") && GetPosition().y < GetViewportRect().Size.y)
{
Vector2 pos = GetPosition();
pos.y += MOVE_SPEED * delta;
SetPosition(pos);
}
}
public void OnAreaEntered(Area2D area)
{
if (area is Ball)
{
// Assign new direction
Ball ball = (Ball)area;
ball.SetDirection(new Vector2(ballDir, (float)new Random().NextDouble() * 2 - 1).normalized());
}
}
}

15
mono/monkey_pong/Wall.cs Normal file
View File

@@ -0,0 +1,15 @@
using Godot;
using System;
public class Wall : Area2D
{
public void OnWallAreaEntered(Area2D area)
{
if (area is Ball)
{
// Oops, ball went out of game place, reset
Ball ball = (Ball)area;
ball.Reset();
}
}
}

BIN
mono/monkey_pong/ball.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 203 B

BIN
mono/monkey_pong/icon.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 956 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 200 B

186
mono/monkey_pong/pong.tscn Normal file
View File

@@ -0,0 +1,186 @@
[gd_scene load_steps=13 format=2]
[ext_resource path="res://Paddle.cs" type="Script" id=1]
[ext_resource path="res://left_pallete.png" type="Texture" id=2]
[ext_resource path="res://right_pallete.png" type="Texture" id=3]
[ext_resource path="res://Ball.cs" type="Script" id=4]
[ext_resource path="res://ball.png" type="Texture" id=5]
[ext_resource path="res://separator.png" type="Texture" id=6]
[ext_resource path="res://Wall.cs" type="Script" id=7]
[ext_resource path="res://CeilingFloor.cs" type="Script" id=8]
[sub_resource type="RectangleShape2D" id=1]
custom_solver_bias = 0.0
extents = Vector2( 4, 16 )
[sub_resource type="RectangleShape2D" id=2]
custom_solver_bias = 0.0
extents = Vector2( 4, 4 )
[sub_resource type="RectangleShape2D" id=3]
custom_solver_bias = 0.0
extents = Vector2( 10, 200 )
[sub_resource type="RectangleShape2D" id=4]
custom_solver_bias = 0.0
extents = Vector2( 320, 10 )
[node name="game" type="Node2D"]
[node name="left" type="Area2D" parent="."]
position = Vector2( 67.6285, 192.594 )
input_pickable = true
gravity_vec = Vector2( 0, 1 )
gravity = 98.0
linear_damp = 0.1
angular_damp = 1.0
audio_bus_override = false
audio_bus_name = "Master"
script = ExtResource( 1 )
ballDir = 1
[node name="sprite" type="Sprite" parent="left"]
texture = ExtResource( 2 )
_sections_unfolded = [ "Transform" ]
[node name="collision" type="CollisionShape2D" parent="left"]
shape = SubResource( 1 )
[node name="right" type="Area2D" parent="."]
position = Vector2( 563.815, 188.919 )
input_pickable = true
gravity_vec = Vector2( 0, 1 )
gravity = 98.0
linear_damp = 0.1
angular_damp = 1.0
audio_bus_override = false
audio_bus_name = "Master"
script = ExtResource( 1 )
ballDir = -1
[node name="sprite" type="Sprite" parent="right"]
texture = ExtResource( 3 )
_sections_unfolded = [ "Transform" ]
[node name="collision" type="CollisionShape2D" parent="right"]
shape = SubResource( 1 )
[node name="ball" type="Area2D" parent="."]
position = Vector2( 320.5, 191.124 )
input_pickable = true
gravity_vec = Vector2( 0, 1 )
gravity = 98.0
linear_damp = 0.1
angular_damp = 1.0
audio_bus_override = false
audio_bus_name = "Master"
script = ExtResource( 4 )
[node name="sprite" type="Sprite" parent="ball"]
texture = ExtResource( 5 )
_sections_unfolded = [ "Transform" ]
[node name="collision" type="CollisionShape2D" parent="ball"]
shape = SubResource( 2 )
[node name="separator" type="Sprite" parent="."]
position = Vector2( 320, 200 )
texture = ExtResource( 6 )
[node name="left_wall" type="Area2D" parent="."]
position = Vector2( -10, 200 )
input_pickable = true
gravity_vec = Vector2( 0, 1 )
gravity = 98.0
linear_damp = 0.1
angular_damp = 1.0
audio_bus_override = false
audio_bus_name = "Master"
script = ExtResource( 7 )
_sections_unfolded = [ "Transform" ]
[node name="collision" type="CollisionShape2D" parent="left_wall"]
shape = SubResource( 3 )
[node name="right_wall" type="Area2D" parent="."]
position = Vector2( 650, 200 )
input_pickable = true
gravity_vec = Vector2( 0, 1 )
gravity = 98.0
linear_damp = 0.1
angular_damp = 1.0
audio_bus_override = false
audio_bus_name = "Master"
script = ExtResource( 7 )
_sections_unfolded = [ "Transform" ]
[node name="collision" type="CollisionShape2D" parent="right_wall"]
shape = SubResource( 3 )
[node name="ceiling" type="Area2D" parent="."]
position = Vector2( 320, -10 )
input_pickable = true
gravity_vec = Vector2( 0, 1 )
gravity = 98.0
linear_damp = 0.1
angular_damp = 1.0
audio_bus_override = false
audio_bus_name = "Master"
script = ExtResource( 8 )
_sections_unfolded = [ "Transform" ]
yDirection = 1
[node name="collision" type="CollisionShape2D" parent="ceiling"]
shape = SubResource( 4 )
[node name="floor" type="Area2D" parent="."]
position = Vector2( 320, 410 )
input_pickable = true
gravity_vec = Vector2( 0, 1 )
gravity = 98.0
linear_damp = 0.1
angular_damp = 1.0
audio_bus_override = false
audio_bus_name = "Master"
script = ExtResource( 8 )
_sections_unfolded = [ "Transform" ]
yDirection = -1
[node name="collision" type="CollisionShape2D" parent="floor"]
shape = SubResource( 4 )
[connection signal="area_entered" from="left" to="left" method="OnAreaEntered"]
[connection signal="area_entered" from="right" to="right" method="OnAreaEntered"]
[connection signal="area_entered" from="left_wall" to="left_wall" method="OnWallAreaEntered"]
[connection signal="area_entered" from="right_wall" to="right_wall" method="OnWallAreaEntered"]
[connection signal="area_entered" from="ceiling" to="ceiling" method="OnAreaEntered"]
[connection signal="area_entered" from="floor" to="floor" method="OnAreaEntered"]

View File

@@ -0,0 +1,40 @@
; Engine configuration file.
; It's best edited using the editor UI and not directly,
; since the parameters that go here are not all obvious.
;
; Format:
; [section] ; section goes between []
; param=value ; assign values to parameters
config_version=3
[application]
config/name="Pong with C#"
run/main_scene="pong.tscn"
config/icon="res://icon.png"
[display]
window/size/width=640
window/size/height=400
window/stretch/mode="2d"
[gdnative]
singletons=[ ]
[input]
left_move_down=[ Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":0,"alt":false,"shift":false,"control":false,"meta":false,"command":false,"pressed":false,"scancode":90,"unicode":0,"echo":false,"script":null)
]
left_move_up=[ Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":0,"alt":false,"shift":false,"control":false,"meta":false,"command":false,"pressed":false,"scancode":65,"unicode":0,"echo":false,"script":null)
]
right_move_down=[ Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":0,"alt":false,"shift":false,"control":false,"meta":false,"command":false,"pressed":false,"scancode":16777234,"unicode":0,"echo":false,"script":null)
]
right_move_up=[ Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":0,"alt":false,"shift":false,"control":false,"meta":false,"command":false,"pressed":false,"scancode":16777232,"unicode":0,"echo":false,"script":null)
]
[rendering]
viewport/default_clear_color=Color( 0, 0, 0, 1 )

Binary file not shown.

After

Width:  |  Height:  |  Size: 202 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 203 B