Skip to content
Snippets Groups Projects
Commit edcf7400 authored by Frederic Aust's avatar Frederic Aust
Browse files

Merge branch 'Time_independent' into 'master'

Entkopplung der Spielgeschwindigkeit von der Realzeit

See merge request !3
parents 1a89abff cdb4af3d
Branches
No related tags found
1 merge request!3Entkopplung der Spielgeschwindigkeit von der Realzeit
...@@ -19,20 +19,25 @@ func _ready(): ...@@ -19,20 +19,25 @@ func _ready():
func _physics_process(delta): #func _physics_process(delta):
func run(delta):
if playing: if playing:
#change_dy_on_wall_hit() #change_dy_on_wall_hit()
self.rotation = 0 self.rotation = 0
#self.linear_velocity = Vector2(dx, dy) * delta * speed #self.linear_velocity = Vector2(dx, dy) * delta * speed
var move = velocity.normalized() * delta * speed var move = velocity.normalized() * delta * speed
print(delta) #print(delta)
var collision = self.move_and_collide(move, false) var collision = self.move_and_collide(move, false)
if collision: if collision:
print("I collided with ", collision.collider.name) #print("I collided with ", collision.collider.name)
if collision.collider.name == "WallTop" || collision.collider.name == "WallBottom": if collision.collider.name == "WallTop" || collision.collider.name == "WallBottom":
velocity = velocity.bounce(collision.normal) velocity = velocity.bounce(collision.normal)
elif collision.collider.name == "PlayerOne" || collision.collider.name == "PlayerTwo": elif collision.collider.name == "PlayerOne":
ball_hit_paddle(get_tree().get_root().find_node("PlayerOne", true, false).position, get_tree().get_root().find_node("PlayerOne", true, false).get_shape(), true)
elif collision.collider.name == "PlayerTwo":
ball_hit_paddle(get_tree().get_root().find_node("PlayerTwo", true, false).position, get_tree().get_root().find_node("PlayerTwo", true, false).get_shape(), false)
pass pass
# velocity.x *=-1 # velocity.x *=-1
# var localCollisionPos = collision.Position - collision.collider.Position; # var localCollisionPos = collision.Position - collision.collider.Position;
...@@ -82,7 +87,7 @@ func start(pos): ...@@ -82,7 +87,7 @@ func start(pos):
velocity = Vector2(100, rand_range(-200,200)) velocity = Vector2(100, rand_range(-200,200))
if rand_range(-1,1)<0: if rand_range(-1,1)<0:
velocity.x*=-1 velocity.x*=-1
print(velocity) #print(velocity)
func get_observation(): func get_observation():
var move = velocity.normalized()*speed var move = velocity.normalized()*speed
......
...@@ -6,7 +6,7 @@ const PORT = 9080 ...@@ -6,7 +6,7 @@ const PORT = 9080
var _server = WebSocketServer.new() var _server = WebSocketServer.new()
var player_one_client_id var player_one_client_id
var player_two_client_id var player_two_client_id
var deltaN = 0
var action_player_one = InputEventAction.new() var action_player_one = InputEventAction.new()
var next_step_player_one = false var next_step_player_one = false
var action_player_two = InputEventAction.new() var action_player_two = InputEventAction.new()
...@@ -29,6 +29,8 @@ const P2_WIN = "Player 2 won!" ...@@ -29,6 +29,8 @@ const P2_WIN = "Player 2 won!"
var message = SPACE_TO_PLAY var message = SPACE_TO_PLAY
func _ready(): func _ready():
VisualServer.render_loop_enabled = false # disable rendering to create a massive boost
# Connect base signals to get notified of new client connections, # Connect base signals to get notified of new client connections,
# disconnections, and disconnect requests. # disconnections, and disconnect requests.
_server.connect("client_connected", self, "_connected") _server.connect("client_connected", self, "_connected")
...@@ -52,6 +54,7 @@ func _ready(): ...@@ -52,6 +54,7 @@ func _ready():
update_score() update_score()
pause() pause()
func _input(_event): func _input(_event):
if Input.is_key_pressed(KEY_SPACE): if Input.is_key_pressed(KEY_SPACE):
play() play()
...@@ -69,8 +72,9 @@ func play(): ...@@ -69,8 +72,9 @@ func play():
func _process(delta): func _process(delta):
# print(delta) # Call this in _process or _physics_process.
_server.poll() # in in steuerung durch Timer auslagern, damit der Process durch Pause lahm gelegt werden kann # Data transfer, and signals emission will only happen when calling this function.
_server.poll()
check_point_scored() check_point_scored()
handle_score_event() handle_score_event()
...@@ -82,11 +86,13 @@ func check_point_scored(): ...@@ -82,11 +86,13 @@ func check_point_scored():
score_player_two += 1 score_player_two += 1
reward_player_one=(-1) reward_player_one=(-1)
reward_player_two=1 reward_player_two=1
print("player two won")
if ball.position.x >= 1024: if ball.position.x >= 1024:
score_event = true score_event = true
score_player_one += 1 score_player_one += 1
reward_player_one=1 reward_player_one=1
reward_player_two=(-1) reward_player_two=(-1)
print("player one won")
update_score() update_score()
if score_player_one == 5 or score_player_two == 5: if score_player_one == 5 or score_player_two == 5:
game_done = true game_done = true
...@@ -109,9 +115,6 @@ func display_message(): ...@@ -109,9 +115,6 @@ func display_message():
$DisplayMessage.text = message $DisplayMessage.text = message
$DisplayMessage.visible = true $DisplayMessage.visible = true
func game_over():
$ScoreTimer.stop()
$MobTimer.stop()
func new_game(): func new_game():
score_player_one = 0 score_player_one = 0
...@@ -160,17 +163,14 @@ func _on_PlayerOne_hit(): ...@@ -160,17 +163,14 @@ func _on_PlayerOne_hit():
func _connected(id, proto): func _connected(id, proto):
# This is called when a new peer connects, "id" will be the assigned peer id, # This is called when a new peer connects, "id" will be the assigned peer id,
# "proto" will be the selected WebSocket sub-protocol (which is optional) # "proto" will be the selected WebSocket sub-protocol (which is optional)
#if(not player_one_client_id): #print("Client %d connected with protocol: %s" % [id, proto])
# player_one_client_id = id pass
#if(not player_two_client_id):
# player_two_client_id = id
print("Client %d connected with protocol: %s" % [id, proto])
func _close_request(id, code, reason): func _close_request(id, code, reason):
# This is called when a client notifies that it wishes to close the connection, # This is called when a client notifies that it wishes to close the connection,
# providing a reason string and close code. # providing a reason string and close code.
print("Client %d disconnecting with code: %d, reason: %s" % [id, code, reason]) #print("Client %d disconnecting with code: %d, reason: %s" % [id, code, reason])
pass
func _disconnected(id, was_clean = false): func _disconnected(id, was_clean = false):
...@@ -179,28 +179,26 @@ func _disconnected(id, was_clean = false): ...@@ -179,28 +179,26 @@ func _disconnected(id, was_clean = false):
# was correctly notified by the remote peer before closing the socket. # was correctly notified by the remote peer before closing the socket.
if( player_one_client_id == id ): if( player_one_client_id == id ):
player_one_client_id = null player_one_client_id = null
print("Player One with Client %d disconnected, clean: %s" % [id, str(was_clean)]) #print("Player One with Client %d disconnected, clean: %s" % [id, str(was_clean)])
if(player_two_client_id== id): if(player_two_client_id== id):
player_two_client_id = null player_two_client_id = null
print("Player Two with Client %d disconnected, clean: %s" % [id, str(was_clean)]) #print("Player Two with Client %d disconnected, clean: %s" % [id, str(was_clean)])
else: #else:
print("Client %d disconnected, clean: %s" % [id, str(was_clean)]) #print("Client %d disconnected, clean: %s" % [id, str(was_clean)])
func _on_data(id): func _on_data(id):
# Print the received packet, you MUST always use get_peer(id).get_packet to receive data, # Print the received packet, you MUST always use get_peer(id).get_packet to receive data,
# and not get_packet directly when not using the MultiplayerAPI. # and not get_packet directly when not using the MultiplayerAPI.
var pkt = _server.get_peer(id).get_packet() var pkt = _server.get_peer(id).get_packet()
print("Got data from client %d: %s ... echoing" % [id, pkt.get_string_from_utf8()]) #print("Got data from client %d: %s ... echoing" % [id, pkt.get_string_from_utf8()])
var data = (JSON.parse(pkt.get_string_from_utf8())).get_result() var data = (JSON.parse(pkt.get_string_from_utf8())).get_result()
#print(data) #print(data)
# var player = data[0]
# var command = data[1]
#if(command == "UP"):
if(data): if(data):
for i in data: for i in data:
print(i) #print(i)
if(i=="player_one"): if(i=="player_one"):
player_one_client_id = id player_one_client_id = id
if(i=="player_two"): if(i=="player_two"):
...@@ -244,7 +242,12 @@ func _on_data(id): ...@@ -244,7 +242,12 @@ func _on_data(id):
pass pass
if(next_step_player_one and next_step_player_two): if(next_step_player_one and next_step_player_two):
unpause() unpause()
$RunTimer.start() #if($RunTimer.is_stopped()): # Used to show how much faster the game runs independent from realtime
# $RunTimer.start()
$PlayerOne.run(0.1)
$PlayerTwo.run(0.1)
ball.run(0.1)
timeout()
func get_return_value_as_utf8_JSON(): func get_return_value_as_utf8_JSON():
...@@ -255,32 +258,35 @@ func get_return_value_as_utf8_JSON(): ...@@ -255,32 +258,35 @@ func get_return_value_as_utf8_JSON():
func _on_RunTimer_timeout(): func _on_RunTimer_timeout():
print("_RunTimer_timeout") print("_RunTimer_timeout")
pause() pass
func timeout():
pause()
action_player_one.pressed = false action_player_one.pressed = false
action_player_two.pressed = false action_player_two.pressed = false
Input.parse_input_event(action_player_one) Input.parse_input_event(action_player_one)
Input.parse_input_event(action_player_two) Input.parse_input_event(action_player_two)
next_step_player_one = false next_step_player_one = false
next_step_player_two = false next_step_player_two = false
var return_value = get_return_value_as_utf8_JSON() # important to get the return values before resetting the reward
var return_value = get_return_value_as_utf8_JSON()
reward_player_one = 0 reward_player_one = 0
reward_player_two = 0 reward_player_two = 0
#print(return_value) ##print(return_value)
if(player_one_client_id): if(player_one_client_id):
_server.get_peer(player_one_client_id).put_packet(return_value) _server.get_peer(player_one_client_id).put_packet(return_value)
if(player_two_client_id): if(player_two_client_id):
_server.get_peer(player_two_client_id).put_packet(return_value) _server.get_peer(player_two_client_id).put_packet(return_value)
func pause(): func pause():
print("pause") #print("pause")
ball.set_pause(true) ball.set_pause(true)
$PlayerOne.set_pause(true) $PlayerOne.set_pause(true)
$PlayerTwo.set_pause(true) $PlayerTwo.set_pause(true)
func unpause(): func unpause():
print("unpause") #print("unpause")
ball.set_pause(false) ball.set_pause(false)
$PlayerOne.set_pause(false) $PlayerOne.set_pause(false)
$PlayerTwo.set_pause(false) $PlayerTwo.set_pause(false)
......
...@@ -86,7 +86,7 @@ position = Vector2( 0, 600 ) ...@@ -86,7 +86,7 @@ position = Vector2( 0, 600 )
[node name="RunTimer" type="Timer" parent="."] [node name="RunTimer" type="Timer" parent="."]
pause_mode = 2 pause_mode = 2
wait_time = 0.1 wait_time = 0.01
one_shot = true one_shot = true
[connection signal="hit" from="PlayerTwo" to="." method="_on_PlayerTwo_hit"] [connection signal="hit" from="PlayerTwo" to="." method="_on_PlayerTwo_hit"]
[connection signal="hit" from="PlayerOne" to="." method="_on_PlayerOne_hit"] [connection signal="hit" from="PlayerOne" to="." method="_on_PlayerOne_hit"]
......
extends Area2D extends RigidBody2D
signal hit signal hit
# Declare member variables here. Examples: # Declare member variables here. Examples:
...@@ -12,12 +12,12 @@ func _ready(): ...@@ -12,12 +12,12 @@ func _ready():
player_size =$CollisionShape2D.shape.extents player_size =$CollisionShape2D.shape.extents
if(is_player_one): if(is_player_one):
$ColorRect.color=Color(0,0,0,1) $ColorRect.color=Color(0,0,0,1)
#hide()
pass # Replace with function body.
# Called every frame. 'delta' is the elapsed time since the previous frame. # Called every frame. 'delta' is the elapsed time since the previous frame.
func _process(delta): #func _process(delta):
func run(delta):
var velocity = Vector2() # The player's movement vector. var velocity = Vector2() # The player's movement vector.
if is_player_one: if is_player_one:
if Input.is_action_pressed("player_one_down"): if Input.is_action_pressed("player_one_down"):
...@@ -41,11 +41,12 @@ func _process(delta): ...@@ -41,11 +41,12 @@ func _process(delta):
#position.x = clamp(position.x, 0, screen_size.x) # ändert sich nicht #position.x = clamp(position.x, 0, screen_size.x) # ändert sich nicht
position.y = clamp(position.y, 0+player_size.y, screen_size.y-player_size.y) position.y = clamp(position.y, 0+player_size.y, screen_size.y-player_size.y)
func get_shape():
return $CollisionShape2D.shape.extents
func _on_Player_body_entered(body): func _on_Player_body_entered(body):
emit_signal("hit") emit_signal("hit")
#$CollisionShape2D.set_deferred("disabled", true) # TODO das muss aufgerufen werden, wenn der ball das spielfeld auf der eigenen Seite verlässt
func start(pos): func start(pos):
position = pos position = pos
......
...@@ -16,7 +16,7 @@ animations = [ { ...@@ -16,7 +16,7 @@ animations = [ {
[sub_resource type="RectangleShape2D" id=2] [sub_resource type="RectangleShape2D" id=2]
extents = Vector2( 7.51772, 60.0095 ) extents = Vector2( 7.51772, 60.0095 )
[node name="Player" type="Area2D"] [node name="Player" type="RigidBody2D"]
collision_layer = 2 collision_layer = 2
collision_mask = 8 collision_mask = 8
script = ExtResource( 4 ) script = ExtResource( 4 )
...@@ -31,7 +31,6 @@ position = Vector2( -1.5, 0 ) ...@@ -31,7 +31,6 @@ position = Vector2( -1.5, 0 )
scale = Vector2( 3, 6 ) scale = Vector2( 3, 6 )
frames = SubResource( 1 ) frames = SubResource( 1 )
animation = "wave" animation = "wave"
frame = 1
speed_scale = 2.0 speed_scale = 2.0
playing = true playing = true
...@@ -47,4 +46,3 @@ margin_bottom = 60.0 ...@@ -47,4 +46,3 @@ margin_bottom = 60.0
__meta__ = { __meta__ = {
"_edit_use_anchors_": false "_edit_use_anchors_": false
} }
[connection signal="body_entered" from="." to="." method="_on_Player_body_entered"]
...@@ -19,6 +19,11 @@ config/name="Battle Pong" ...@@ -19,6 +19,11 @@ config/name="Battle Pong"
run/main_scene="res://Main.tscn" run/main_scene="res://Main.tscn"
config/icon="res://icon.png" config/icon="res://icon.png"
[debug]
settings/gdscript/max_call_stack=4096
settings/visual_script/max_call_stack=4096
[display] [display]
window/stretch/mode="2d" window/stretch/mode="2d"
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment