2014-02-10 01:10:30 +00:00
|
|
|
|
|
|
|
extends Node2D
|
|
|
|
|
2015-11-21 15:13:43 +00:00
|
|
|
# Member variables
|
2014-02-10 01:10:30 +00:00
|
|
|
const INITIAL_BALL_SPEED = 80
|
|
|
|
var ball_speed = INITIAL_BALL_SPEED
|
2015-11-21 15:13:43 +00:00
|
|
|
var screen_size = Vector2(640, 400)
|
|
|
|
|
|
|
|
# Default ball direction
|
|
|
|
var direction = Vector2(-1, 0)
|
|
|
|
var pad_size = Vector2(8, 32)
|
2014-02-10 01:10:30 +00:00
|
|
|
const PAD_SPEED = 150
|
|
|
|
|
|
|
|
|
|
|
|
func _process(delta):
|
2015-11-21 15:13:43 +00:00
|
|
|
# Get ball position and pad rectangles
|
2014-02-10 01:10:30 +00:00
|
|
|
var ball_pos = get_node("ball").get_pos()
|
2015-11-21 15:13:43 +00:00
|
|
|
var left_rect = Rect2(get_node("left").get_pos() - pad_size*0.5, pad_size)
|
|
|
|
var right_rect = Rect2(get_node("right").get_pos() - pad_size*0.5, pad_size)
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2015-11-21 15:13:43 +00:00
|
|
|
# Integrate new ball postion
|
|
|
|
ball_pos += direction*ball_speed*delta
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2015-11-21 15:13:43 +00:00
|
|
|
# Flip when touching roof or floor
|
|
|
|
if ((ball_pos.y < 0 and direction.y < 0) or (ball_pos.y > screen_size.y and direction.y > 0)):
|
2014-02-10 01:10:30 +00:00
|
|
|
direction.y = -direction.y
|
2015-11-21 15:13:43 +00:00
|
|
|
|
|
|
|
# Flip, change direction and increase speed when touching pads
|
|
|
|
if ((left_rect.has_point(ball_pos) and direction.x < 0) or (right_rect.has_point(ball_pos) and direction.x > 0)):
|
|
|
|
direction.x = -direction.x
|
|
|
|
ball_speed *= 1.1
|
|
|
|
direction.y = randf()*2.0 - 1
|
2014-02-10 01:10:30 +00:00
|
|
|
direction = direction.normalized()
|
2015-11-21 15:13:43 +00:00
|
|
|
|
|
|
|
# Check gameover
|
|
|
|
if (ball_pos.x < 0 or ball_pos.x > screen_size.x):
|
|
|
|
ball_pos = screen_size*0.5
|
|
|
|
ball_speed = INITIAL_BALL_SPEED
|
|
|
|
direction = Vector2(-1, 0)
|
|
|
|
|
2014-02-10 01:10:30 +00:00
|
|
|
get_node("ball").set_pos(ball_pos)
|
2015-11-21 15:13:43 +00:00
|
|
|
|
|
|
|
# Move left pad
|
2014-02-10 01:10:30 +00:00
|
|
|
var left_pos = get_node("left").get_pos()
|
|
|
|
|
|
|
|
if (left_pos.y > 0 and Input.is_action_pressed("left_move_up")):
|
2015-11-21 15:13:43 +00:00
|
|
|
left_pos.y += -PAD_SPEED*delta
|
2014-02-10 01:10:30 +00:00
|
|
|
if (left_pos.y < screen_size.y and Input.is_action_pressed("left_move_down")):
|
2015-11-21 15:13:43 +00:00
|
|
|
left_pos.y += PAD_SPEED*delta
|
|
|
|
|
2014-02-10 01:10:30 +00:00
|
|
|
get_node("left").set_pos(left_pos)
|
2015-11-21 15:13:43 +00:00
|
|
|
|
|
|
|
# Move right pad
|
2014-02-10 01:10:30 +00:00
|
|
|
var right_pos = get_node("right").get_pos()
|
|
|
|
|
|
|
|
if (right_pos.y > 0 and Input.is_action_pressed("right_move_up")):
|
2015-11-21 15:13:43 +00:00
|
|
|
right_pos.y += -PAD_SPEED*delta
|
2014-02-10 01:10:30 +00:00
|
|
|
if (right_pos.y < screen_size.y and Input.is_action_pressed("right_move_down")):
|
2015-11-21 15:13:43 +00:00
|
|
|
right_pos.y += PAD_SPEED*delta
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2015-11-21 15:13:43 +00:00
|
|
|
get_node("right").set_pos(right_pos)
|
|
|
|
|
2014-02-10 01:10:30 +00:00
|
|
|
|
|
|
|
func _ready():
|
2015-11-21 15:13:43 +00:00
|
|
|
screen_size = get_viewport_rect().size # Get actual size
|
2014-02-10 01:10:30 +00:00
|
|
|
pad_size = get_node("left").get_texture().get_size()
|
|
|
|
set_process(true)
|