diff options
40 files changed, 841 insertions, 1101 deletions
diff --git a/demos/2d/area_input/input.gd b/demos/2d/area_input/input.gd index 3f719fc853..e9cc9f3c1d 100644 --- a/demos/2d/area_input/input.gd +++ b/demos/2d/area_input/input.gd @@ -1,16 +1,15 @@ extends Area2D -#virtual from CollisionObject2D (also available as signal) + +# Virtual from CollisionObject2D (also available as signal) func _input_event(viewport, event, shape_idx): - #convert event to local coordinates - if (event.type==InputEvent.MOUSE_MOTION): - event = make_input_local( event ) + # Convert event to local coordinates + if (event.type == InputEvent.MOUSE_MOTION): + event = make_input_local(event) get_node("label").set_text(str(event.pos)) - -#virtual from CollisionObject2D (also available as signal) -func _mouse_exit(): - get_node("label").set_text("") - +# Virtual from CollisionObject2D (also available as signal) +func _mouse_exit(): + get_node("label").set_text("") diff --git a/demos/2d/dynamic_collision_shapes/ball.gd b/demos/2d/dynamic_collision_shapes/ball.gd index c17b20f9c8..8ffa33f746 100644 --- a/demos/2d/dynamic_collision_shapes/ball.gd +++ b/demos/2d/dynamic_collision_shapes/ball.gd @@ -1,21 +1,18 @@ extends RigidBody2D -# member variables here, example: -# var a=2 -# var b="textvar" +# Member variables +var timeout = 5 -var timeout=5 func _process(delta): - timeout-=delta - if (timeout<1): + timeout -= delta + if (timeout < 1): set_opacity(timeout) - if (timeout<0): + if (timeout < 0): queue_free() -func _ready(): - set_process(true) - # Initialization here - pass +func _ready(): + # Initialization here + set_process(true) diff --git a/demos/2d/dynamic_collision_shapes/dynamic_colobjs.gd b/demos/2d/dynamic_collision_shapes/dynamic_colobjs.gd index a6a42a1914..1595195c74 100644 --- a/demos/2d/dynamic_collision_shapes/dynamic_colobjs.gd +++ b/demos/2d/dynamic_collision_shapes/dynamic_colobjs.gd @@ -1,23 +1,20 @@ extends Node2D -# member variables here, example: -# var a=2 -# var b="textvar" -const EMIT_INTERVAL=0.1 -var timeout=EMIT_INTERVAL +# Member variables +const EMIT_INTERVAL = 0.1 +var timeout = EMIT_INTERVAL + func _process(delta): - timeout-=delta - if (timeout<0): - timeout=EMIT_INTERVAL + timeout -= delta + if (timeout < 0): + timeout = EMIT_INTERVAL var ball = preload("res://ball.scn").instance() - ball.set_pos( Vector2(randf() * get_viewport_rect().size.x, 0) ) + ball.set_pos(Vector2(randf()*get_viewport_rect().size.x, 0)) add_child(ball) - -func _ready(): - set_process(true) - # Initialization here - pass +func _ready(): + # Initialization here + set_process(true) diff --git a/demos/2d/fog_of_war/fog.gd b/demos/2d/fog_of_war/fog.gd index 9da5680e4d..82979264c4 100644 --- a/demos/2d/fog_of_war/fog.gd +++ b/demos/2d/fog_of_war/fog.gd @@ -1,86 +1,79 @@ extends TileMap -# member variables here, example: -# var a=2 -# var b="textvar" +# Member variables -# boundarys for the fog rectangle -var x_min = -20 # left start tile -var x_max = 20 # right end tile -var y_min = -20 # top start tile -var y_max = 20 # bottom end tile +# Boundaries for the fog rectangle +var x_min = -20 # Left start tile +var x_max = 20 # Right end tile +var y_min = -20 # Top start tile +var y_max = 20 # Bottom end tile -var position # players position +var position # Player's position -# iteration variables +# Iteration variables var x var y -# variable to check if player moved +# Variables to check if the player moved var x_old var y_old -# array to build up the visible area like a square -# first value determines the width/height of the tip -# here it would be 2*2 + 1 = 5 tiles wide/high -# second value determines the total squares size -# here it would be 5*2 + 1 = 10 tiles wide/high -var l = range(2,5) +# Array to build up the visible area like a square. +# First value determines the width/height of the tip. +# Here it would be 2*2 + 1 = 5 tiles wide/high. +# Second value determines the total squares size. +# Here it would be 5*2 + 1 = 10 tiles wide/high. +var l = range(2, 5) -# process that runs in realtime + +# Process that runs in realtime func _fixed_process(delta): position = get_node("../troll").get_pos() - # calculate the corresponding tile + # Calculate the corresponding tile # from the players position x = int(position.x/get_cell_size().x) - # switching from positive to negative tile positions + # Switching from positive to negative tile positions # causes problems because of rounding problems if position.x < 0: - x -= 1 # correct negative values + x -= 1 # Correct negative values y = int(position.y/get_cell_size().y) - if position.y < 0: + if (position.y < 0): y -= 1 - - # check if the player moved one tile further - if (x_old != x) or (y_old != y): - - # create the transparent part (visited area) - var end = l.size()-1 + + # Check if the player moved one tile further + if ((x_old != x) or (y_old != y)): + # Create the transparent part (visited area) + var end = l.size() - 1 var start = 0 for steps in range(l.size()): - for m in range(x-l[end]-1,x+l[end]+2): - for n in range(y-l[start]-1,y+l[start]+2): - if get_cell(m,n) != 0: - set_cell(m,n,1,0,0) + for m in range(x - l[end] - 1, x + l[end] + 2): + for n in range(y - l[start] - 1, y + l[start] + 2): + if (get_cell(m, n) != 0): + set_cell(m, n, 1, 0, 0) end -= 1 start += 1 - - # create the actual and active visible part - var end = l.size()-1 + + # Create the actual and active visible part + var end = l.size() - 1 var start = 0 for steps in range(l.size()): - for m in range(x-l[end],x+l[end]+1): - for n in range(y-l[start],y+l[start]+1): - set_cell(m,n,-1) + for m in range(x - l[end], x + l[end] + 1): + for n in range(y - l[start], y + l[start] + 1): + set_cell(m, n, -1) end -= 1 start += 1 - + x_old = x y_old = y - - pass + func _ready(): # Initalization here - - # create a square filled with the 100% opaque fog - for x in range(x_min,x_max): - for y in range(y_min,y_max): - set_cell(x,y,0,0,0) + # Create a square filled with the 100% opaque fog + for x in range(x_min, x_max): + for y in range(y_min, y_max): + set_cell(x, y, 0, 0, 0) set_fixed_process(true) - pass - - diff --git a/demos/2d/fog_of_war/troll.gd b/demos/2d/fog_of_war/troll.gd index d118d3a2ba..454a3ac3a3 100644 --- a/demos/2d/fog_of_war/troll.gd +++ b/demos/2d/fog_of_war/troll.gd @@ -2,42 +2,38 @@ extends KinematicBody2D # This is a simple collision demo showing how -# the kinematic cotroller works. +# the kinematic controller works. # move() will allow to move the node, and will -# always move it to a non-colliding spot, +# always move it to a non-colliding spot, # as long as it starts from a non-colliding spot too. +# Member variables +const MOTION_SPEED = 160 # Pixels/second -#pixels / second -const MOTION_SPEED=160 func _fixed_process(delta): - var motion = Vector2() if (Input.is_action_pressed("move_up")): - motion+=Vector2(0,-1) + motion += Vector2(0, -1) if (Input.is_action_pressed("move_bottom")): - motion+=Vector2(0,1) + motion += Vector2(0, 1) if (Input.is_action_pressed("move_left")): - motion+=Vector2(-1,0) + motion += Vector2(-1, 0) if (Input.is_action_pressed("move_right")): - motion+=Vector2(1,0) + motion += Vector2(1, 0) - motion = motion.normalized() * MOTION_SPEED * delta + motion = motion.normalized()*MOTION_SPEED*delta motion = move(motion) - #make character slide nicely through the world + # Make character slide nicely through the world var slide_attempts = 4 - while(is_colliding() and slide_attempts>0): + while(is_colliding() and slide_attempts > 0): motion = get_collision_normal().slide(motion) - motion=move(motion) - slide_attempts-=1 - + motion = move(motion) + slide_attempts -= 1 + func _ready(): # Initalization here set_fixed_process(true) - pass - - diff --git a/demos/2d/hdr/beach_cave.gd b/demos/2d/hdr/beach_cave.gd index 9dffbc4662..9829bd3611 100644 --- a/demos/2d/hdr/beach_cave.gd +++ b/demos/2d/hdr/beach_cave.gd @@ -1,26 +1,22 @@ extends Node2D -# member variables here, example: -# var a=2 -# var b="textvar" -const CAVE_LIMIT=1000 +# Member variables +const CAVE_LIMIT = 1000 -func _input(ev): - if (ev.type==InputEvent.MOUSE_MOTION and ev.button_mask&1): - var rel_x = ev.relative_x + +func _input(event): + if (event.type == InputEvent.MOUSE_MOTION and event.button_mask&1): + var rel_x = event.relative_x var cavepos = get_node("cave").get_pos() - cavepos.x+=rel_x - if (cavepos.x<-CAVE_LIMIT): - cavepos.x=-CAVE_LIMIT - elif (cavepos.x>0): - cavepos.x=0 + cavepos.x += rel_x + if (cavepos.x < -CAVE_LIMIT): + cavepos.x = -CAVE_LIMIT + elif (cavepos.x > 0): + cavepos.x = 0 get_node("cave").set_pos(cavepos) - + func _ready(): - set_process_input(true) # Initialization here - pass - - + set_process_input(true) diff --git a/demos/2d/hexamap/troll.gd b/demos/2d/hexamap/troll.gd index d118d3a2ba..0e4d7ce535 100644 --- a/demos/2d/hexamap/troll.gd +++ b/demos/2d/hexamap/troll.gd @@ -2,42 +2,38 @@ extends KinematicBody2D # This is a simple collision demo showing how -# the kinematic cotroller works. +# the kinematic controller works. # move() will allow to move the node, and will # always move it to a non-colliding spot, # as long as it starts from a non-colliding spot too. +# Member variables +const MOTION_SPEED = 160 # Pixels/second -#pixels / second -const MOTION_SPEED=160 func _fixed_process(delta): - var motion = Vector2() if (Input.is_action_pressed("move_up")): - motion+=Vector2(0,-1) + motion += Vector2(0, -1) if (Input.is_action_pressed("move_bottom")): - motion+=Vector2(0,1) + motion += Vector2(0, 1) if (Input.is_action_pressed("move_left")): - motion+=Vector2(-1,0) + motion += Vector2(-1, 0) if (Input.is_action_pressed("move_right")): - motion+=Vector2(1,0) + motion += Vector2(1, 0) - motion = motion.normalized() * MOTION_SPEED * delta + motion = motion.normalized()*MOTION_SPEED*delta motion = move(motion) - #make character slide nicely through the world + # Make character slide nicely through the world var slide_attempts = 4 - while(is_colliding() and slide_attempts>0): + while(is_colliding() and slide_attempts > 0): motion = get_collision_normal().slide(motion) - motion=move(motion) - slide_attempts-=1 - + motion = move(motion) + slide_attempts -= 1 + func _ready(): # Initalization here set_fixed_process(true) - pass - - diff --git a/demos/2d/isometric/troll.gd b/demos/2d/isometric/troll.gd index d118d3a2ba..c44a622899 100644 --- a/demos/2d/isometric/troll.gd +++ b/demos/2d/isometric/troll.gd @@ -2,42 +2,38 @@ extends KinematicBody2D # This is a simple collision demo showing how -# the kinematic cotroller works. +# the kinematic controller works. # move() will allow to move the node, and will # always move it to a non-colliding spot, # as long as it starts from a non-colliding spot too. +# Member variables +const MOTION_SPEED = 160 # Pixels/seconds -#pixels / second -const MOTION_SPEED=160 func _fixed_process(delta): - var motion = Vector2() if (Input.is_action_pressed("move_up")): - motion+=Vector2(0,-1) + motion += Vector2(0, -1) if (Input.is_action_pressed("move_bottom")): - motion+=Vector2(0,1) + motion += Vector2(0, 1) if (Input.is_action_pressed("move_left")): - motion+=Vector2(-1,0) + motion += Vector2(-1, 0) if (Input.is_action_pressed("move_right")): - motion+=Vector2(1,0) + motion += Vector2(1, 0) - motion = motion.normalized() * MOTION_SPEED * delta + motion = motion.normalized()*MOTION_SPEED*delta motion = move(motion) - #make character slide nicely through the world + # Make character slide nicely through the world var slide_attempts = 4 - while(is_colliding() and slide_attempts>0): + while(is_colliding() and slide_attempts > 0): motion = get_collision_normal().slide(motion) - motion=move(motion) - slide_attempts-=1 - + motion = move(motion) + slide_attempts -= 1 + func _ready(): # Initalization here set_fixed_process(true) - pass - - diff --git a/demos/2d/isometric_light/cubio.gd b/demos/2d/isometric_light/cubio.gd index 30c766936c..7e2ff2a5c4 100644 --- a/demos/2d/isometric_light/cubio.gd +++ b/demos/2d/isometric_light/cubio.gd @@ -1,96 +1,86 @@ extends KinematicBody2D -# member variables here, example: -# var a=2 -# var b="textvar" - +# Member variables const MAX_SPEED = 300.0 const IDLE_SPEED = 10.0 -const ACCEL=5.0 -const VSCALE=0.5 -const SHOOT_INTERVAL=0.3 +const ACCEL = 5.0 +const VSCALE = 0.5 +const SHOOT_INTERVAL = 0.3 + +var speed = Vector2() +var current_anim = "" +var current_mirror = false -var speed=Vector2() -var current_anim="" -var current_mirror=false +var shoot_countdown = 0 -var shoot_countdown=0 -func _input(ev): - if (ev.type==InputEvent.MOUSE_BUTTON and ev.button_index==1 and ev.pressed and shoot_countdown<=0): - var pos = get_canvas_transform().affine_inverse() * ev.pos - var dir = (pos-get_global_pos()).normalized() +func _input(event): + if (event.type == InputEvent.MOUSE_BUTTON and event.button_index == 1 and event.pressed and shoot_countdown <= 0): + var pos = get_canvas_transform().affine_inverse()*event.pos + var dir = (pos - get_global_pos()).normalized() var bullet = preload("res://shoot.scn").instance() - bullet.advance_dir=dir - bullet.set_pos( get_global_pos() + dir * 60 ) + bullet.advance_dir = dir + bullet.set_pos(get_global_pos() + dir*60) get_parent().add_child(bullet) - shoot_countdown=SHOOT_INTERVAL - - - + shoot_countdown = SHOOT_INTERVAL + func _fixed_process(delta): - - shoot_countdown-=delta + shoot_countdown -= delta var dir = Vector2() if (Input.is_action_pressed("up")): - dir+=Vector2(0,-1) + dir += Vector2(0, -1) if (Input.is_action_pressed("down")): - dir+=Vector2(0,1) + dir += Vector2(0, 1) if (Input.is_action_pressed("left")): - dir+=Vector2(-1,0) + dir += Vector2(-1, 0) if (Input.is_action_pressed("right")): - dir+=Vector2(1,0) - - if (dir!=Vector2()): - dir=dir.normalized() - speed = speed.linear_interpolate(dir*MAX_SPEED,delta*ACCEL) - var motion = speed * delta - motion.y*=VSCALE - motion=move(motion) + dir += Vector2(1, 0) + + if (dir != Vector2()): + dir = dir.normalized() + speed = speed.linear_interpolate(dir*MAX_SPEED, delta*ACCEL) + var motion = speed*delta + motion.y *= VSCALE + motion = move(motion) if (is_colliding()): var n = get_collision_normal() - motion=n.slide(motion) + motion = n.slide(motion) move(motion) - var next_anim="" - var next_mirror=false + var next_anim = "" + var next_mirror = false - if (dir==Vector2() and speed.length()<IDLE_SPEED): - next_anim="idle" - next_mirror=false - elif (speed.length()>IDLE_SPEED*0.1): - var angle = atan2(abs(speed.x),speed.y) + if (dir == Vector2() and speed.length() < IDLE_SPEED): + next_anim = "idle" + next_mirror = false + elif (speed.length() > IDLE_SPEED*0.1): + var angle = atan2(abs(speed.x), speed.y) - next_mirror = speed.x>0 - if (angle<PI/8): - next_anim="bottom" - next_mirror=false - elif (angle<PI/4+PI/8): - next_anim="bottom_left" - elif (angle<PI*2/4+PI/8): - next_anim="left" - elif (angle<PI*3/4+PI/8): - next_anim="top_left" + next_mirror = speed.x > 0 + if (angle < PI/8): + next_anim = "bottom" + next_mirror = false + elif (angle < PI/4 + PI/8): + next_anim = "bottom_left" + elif (angle < PI*2/4 + PI/8): + next_anim = "left" + elif (angle < PI*3/4 + PI/8): + next_anim = "top_left" else: - next_anim="top" - next_mirror=false - - - if (next_anim!=current_anim or next_mirror!=current_mirror): + next_anim = "top" + next_mirror = false + + if (next_anim != current_anim or next_mirror != current_mirror): get_node("frames").set_flip_h(next_mirror) get_node("anim").play(next_anim) - current_anim=next_anim - current_mirror=next_mirror - + current_anim = next_anim + current_mirror = next_mirror func _ready(): # Initialization here set_fixed_process(true) set_process_input(true) - pass - - diff --git a/demos/2d/isometric_light/map.gd b/demos/2d/isometric_light/map.gd index f712aeeaec..6b790ddf1c 100644 --- a/demos/2d/isometric_light/map.gd +++ b/demos/2d/isometric_light/map.gd @@ -1,18 +1,7 @@ extends Node2D -# member variables here, example: -# var a=2 -# var b="textvar" -func _ready(): - # Initialization here - pass - - - - -func _on_prince_area_body_enter( body ): - if (body.get_name()=="cubio"): +func _on_prince_area_body_enter(body): + if (body.get_name() == "cubio"): get_node("message").show() - pass # replace with function body diff --git a/demos/2d/isometric_light/shoot.gd b/demos/2d/isometric_light/shoot.gd index 0486bbb658..bf0b09dc0b 100644 --- a/demos/2d/isometric_light/shoot.gd +++ b/demos/2d/isometric_light/shoot.gd @@ -1,27 +1,22 @@ extends KinematicBody2D -# member variables here, example: -# var a=2 -# var b="textvar" - -var advance_dir=Vector2(1,0) +# Member variables const ADVANCE_SPEED = 500.0 -var hit=false +var advance_dir = Vector2(1, 0) +var hit = false + func _fixed_process(delta): - if (hit): return - move(advance_dir*delta*ADVANCE_SPEED) + move(advance_dir*delta*ADVANCE_SPEED) if (is_colliding()): get_node("anim").play("explode") - hit=true + hit = true + func _ready(): # Initialization here set_fixed_process(true) - pass - - diff --git a/demos/2d/kinematic_char/colworld.gd b/demos/2d/kinematic_char/colworld.gd index fe2dc30bb6..7926ef9d54 100644 --- a/demos/2d/kinematic_char/colworld.gd +++ b/demos/2d/kinematic_char/colworld.gd @@ -1,18 +1,8 @@ extends Node2D -#member variables here, example: -#var a=2 -#var b="textvar" -func _ready(): - #Initalization here - pass - - - - -func _on_princess_body_enter( body ): - #the name of this editor-generated callback is unfortunate - if (body.get_name()=="player"): +func _on_princess_body_enter(body): + # The name of this editor-generated callback is unfortunate + if (body.get_name() == "player"): get_node("youwin").show() diff --git a/demos/2d/kinematic_char/engine.cfg b/demos/2d/kinematic_char/engine.cfg index 4ce8f836ae..8bdd5e2fc5 100644 --- a/demos/2d/kinematic_char/engine.cfg +++ b/demos/2d/kinematic_char/engine.cfg @@ -1,6 +1,6 @@ [application] -name="Kinematic Collision" +name="Kinematic Character" main_scene="res://colworld.scn" icon="res://icon.png" diff --git a/demos/2d/kinematic_char/player.gd b/demos/2d/kinematic_char/player.gd index 329382408b..a5c64a303a 100644 --- a/demos/2d/kinematic_char/player.gd +++ b/demos/2d/kinematic_char/player.gd @@ -1,137 +1,124 @@ extends KinematicBody2D -#This is a simple collision demo showing how -#the kinematic cotroller works. -#move() will allow to move the node, and will -#always move it to a non-colliding spot, -#as long as it starts from a non-colliding spot too. +# This is a simple collision demo showing how +# the kinematic controller works. +# move() will allow to move the node, and will +# always move it to a non-colliding spot, +# as long as it starts from a non-colliding spot too. +# Member variables +const GRAVITY = 500.0 # Pixels/second -#pixels / second -const GRAVITY = 500.0 - -#Angle in degrees towards either side that the player can -#consider "floor". +# Angle in degrees towards either side that the player can consider "floor" const FLOOR_ANGLE_TOLERANCE = 40 const WALK_FORCE = 600 -const WALK_MIN_SPEED=10 +const WALK_MIN_SPEED = 10 const WALK_MAX_SPEED = 200 const STOP_FORCE = 1300 const JUMP_SPEED = 200 -const JUMP_MAX_AIRBORNE_TIME=0.2 +const JUMP_MAX_AIRBORNE_TIME = 0.2 -const SLIDE_STOP_VELOCITY=1.0 #one pixel per second -const SLIDE_STOP_MIN_TRAVEL=1.0 #one pixel +const SLIDE_STOP_VELOCITY = 1.0 # One pixel per second +const SLIDE_STOP_MIN_TRAVEL = 1.0 # One pixel var velocity = Vector2() -var on_air_time=100 -var jumping=false +var on_air_time = 100 +var jumping = false -var prev_jump_pressed=false +var prev_jump_pressed = false -func _fixed_process(delta): - #create forces - var force = Vector2(0,GRAVITY) +func _fixed_process(delta): + # Create forces + var force = Vector2(0, GRAVITY) var walk_left = Input.is_action_pressed("move_left") var walk_right = Input.is_action_pressed("move_right") var jump = Input.is_action_pressed("jump") - - var stop=true + + var stop = true if (walk_left): - if (velocity.x<=WALK_MIN_SPEED and velocity.x > -WALK_MAX_SPEED): - force.x-=WALK_FORCE - stop=false - + if (velocity.x <= WALK_MIN_SPEED and velocity.x > -WALK_MAX_SPEED): + force.x -= WALK_FORCE + stop = false elif (walk_right): - if (velocity.x>=-WALK_MIN_SPEED and velocity.x < WALK_MAX_SPEED): - force.x+=WALK_FORCE - stop=false + if (velocity.x >= -WALK_MIN_SPEED and velocity.x < WALK_MAX_SPEED): + force.x += WALK_FORCE + stop = false if (stop): var vsign = sign(velocity.x) var vlen = abs(velocity.x) - vlen -= STOP_FORCE * delta - if (vlen<0): - vlen=0 - - velocity.x=vlen*vsign + vlen -= STOP_FORCE*delta + if (vlen < 0): + vlen = 0 - - - #integrate forces to velocity - velocity += force * delta + velocity.x = vlen*vsign - #integrate velocity into motion and move - var motion = velocity * delta - - #move and consume motion + # Integrate forces to velocity + velocity += force*delta + + # Integrate velocity into motion and move + var motion = velocity*delta + + # Move and consume motion motion = move(motion) - - - var floor_velocity=Vector2() - + + var floor_velocity = Vector2() + if (is_colliding()): - # you can check which tile was collision against with this + # You can check which tile was collision against with this # print(get_collider_metadata()) - - #ran against something, is it the floor? get normal + + # Ran against something, is it the floor? Get normal var n = get_collision_normal() - - if ( rad2deg(acos(n.dot( Vector2(0,-1)))) < FLOOR_ANGLE_TOLERANCE ): - #if angle to the "up" vectors is < angle tolerance - #char is on floor - on_air_time=0 - floor_velocity=get_collider_velocity() - - - if (on_air_time==0 and force.x==0 and get_travel().length() < SLIDE_STOP_MIN_TRAVEL and abs(velocity.x) < SLIDE_STOP_VELOCITY and get_collider_velocity()==Vector2()): - #Since this formula will always slide the character around, - #a special case must be considered to to stop it from moving - #if standing on an inclined floor. Conditions are: - # 1) Standing on floor (on_air_time==0) + + if (rad2deg(acos(n.dot(Vector2(0, -1)))) < FLOOR_ANGLE_TOLERANCE): + # If angle to the "up" vectors is < angle tolerance + # char is on floor + on_air_time = 0 + floor_velocity = get_collider_velocity() + + if (on_air_time == 0 and force.x == 0 and get_travel().length() < SLIDE_STOP_MIN_TRAVEL and abs(velocity.x) < SLIDE_STOP_VELOCITY and get_collider_velocity() == Vector2()): + # Since this formula will always slide the character around, + # a special case must be considered to to stop it from moving + # if standing on an inclined floor. Conditions are: + # 1) Standing on floor (on_air_time == 0) # 2) Did not move more than one pixel (get_travel().length() < SLIDE_STOP_MIN_TRAVEL) # 3) Not moving horizontally (abs(velocity.x) < SLIDE_STOP_VELOCITY) # 4) Collider is not moving - + revert_motion() - velocity.y=0.0 - + velocity.y = 0.0 else: - #For every other case of motion,our motion was interrupted. - #Try to complete the motion by "sliding" - #by the normal - + # For every other case of motion, our motion was interrupted. + # Try to complete the motion by "sliding" by the normal motion = n.slide(motion) - velocity = n.slide(velocity) - #then move again + velocity = n.slide(velocity) + # Then move again move(motion) - - if (floor_velocity!=Vector2()): - #if floor moves, move with floor + + if (floor_velocity != Vector2()): + # If floor moves, move with floor move(floor_velocity*delta) - - if (jumping and velocity.y>0): - #if falling, no longer jumping - jumping=false - - if (on_air_time<JUMP_MAX_AIRBORNE_TIME and jump and not prev_jump_pressed and not jumping): - # Jump must also be allowed to happen if the - # character left the floor a little bit ago. + + if (jumping and velocity.y > 0): + # If falling, no longer jumping + jumping = false + + if (on_air_time < JUMP_MAX_AIRBORNE_TIME and jump and not prev_jump_pressed and not jumping): + # Jump must also be allowed to happen if the character left the floor a little bit ago. # Makes controls more snappy. - velocity.y=-JUMP_SPEED - jumping=true - - on_air_time+=delta - prev_jump_pressed=jump + velocity.y = -JUMP_SPEED + jumping = true + + on_air_time += delta + prev_jump_pressed = jump + func _ready(): #Initalization here set_fixed_process(true) - pass - - diff --git a/demos/2d/kinematic_col/player.gd b/demos/2d/kinematic_col/player.gd index 36784a9d9f..48d54d1a33 100644 --- a/demos/2d/kinematic_col/player.gd +++ b/demos/2d/kinematic_col/player.gd @@ -2,35 +2,31 @@ extends KinematicBody2D # This is a simple collision demo showing how -# the kinematic cotroller works. +# the kinematic controller works. # move() will allow to move the node, and will # always move it to a non-colliding spot, # as long as it starts from a non-colliding spot too. +# Member variables +const MOTION_SPEED = 160 # Pixels/second -#pixels / second -const MOTION_SPEED=160 func _fixed_process(delta): - var motion = Vector2() if (Input.is_action_pressed("move_up")): - motion+=Vector2(0,-1) + motion += Vector2(0, -1) if (Input.is_action_pressed("move_bottom")): - motion+=Vector2(0,1) + motion += Vector2(0, 1) if (Input.is_action_pressed("move_left")): - motion+=Vector2(-1,0) + motion += Vector2(-1, 0) if (Input.is_action_pressed("move_right")): - motion+=Vector2(1,0) + motion += Vector2(1, 0) - motion = motion.normalized() * MOTION_SPEED * delta + motion = motion.normalized()*MOTION_SPEED*delta move(motion) - + func _ready(): # Initalization here set_fixed_process(true) - pass - - diff --git a/demos/2d/lookat/lookat.gd b/demos/2d/lookat/lookat.gd index 742c5b0671..25b5e638d7 100644 --- a/demos/2d/lookat/lookat.gd +++ b/demos/2d/lookat/lookat.gd @@ -1,43 +1,34 @@ extends Sprite -# member variables here, example: -# var a=2 -# var b="textvar" - -const MODE_DIRECT=0 -const MODE_CONSTANT=1 -const MODE_SMOOTH=2 +# Member variables +const MODE_DIRECT = 0 +const MODE_CONSTANT = 1 +const MODE_SMOOTH = 2 const ROTATION_SPEED = 1 const SMOOTH_SPEED = 2.0 -export(int,"Direct","Constant","Smooth") var mode=MODE_DIRECT +export(int, "Direct", "Constant", "Smooth") var mode = MODE_DIRECT + func _process(delta): var mpos = get_viewport().get_mouse_pos() - if (mode==MODE_DIRECT): - + if (mode == MODE_DIRECT): look_at(mpos) - - elif (mode==MODE_CONSTANT): - + elif (mode == MODE_CONSTANT): var ang = get_angle_to(mpos) var s = sign(ang) - ang=abs(ang) - - rotate( min(ang,ROTATION_SPEED*delta)*s ) + ang = abs(ang) - elif (mode==MODE_SMOOTH): - - var ang = get_angle_to(mpos) + rotate(min(ang, ROTATION_SPEED*delta)*s) + elif (mode == MODE_SMOOTH): + var ang = get_angle_to(mpos) - rotate( ang*delta*SMOOTH_SPEED ) + rotate(ang*delta*SMOOTH_SPEED) + func _ready(): # Initialization here set_process(true) - pass - - diff --git a/demos/2d/motion/motion.gd b/demos/2d/motion/motion.gd index 8f8f56a889..a7031b24e6 100644 --- a/demos/2d/motion/motion.gd +++ b/demos/2d/motion/motion.gd @@ -1,31 +1,30 @@ extends Sprite - -export var use_idle=true - -# member variables here, example: -# var a=2 -# var b="textvar" +# Member variables const BEGIN = -113 const END = 907 -const TIME = 5.0 # seconds -const SPEED = (END-BEGIN)/TIME +const TIME = 5.0 # Seconds +const SPEED = (END - BEGIN)/TIME + +export var use_idle = true + func _process(delta): var ofs = get_pos() - ofs.x+=delta*SPEED - if (ofs.x>END): - ofs.x=BEGIN + ofs.x += delta*SPEED + if (ofs.x > END): + ofs.x = BEGIN set_pos(ofs) - + + func _fixed_process(delta): var ofs = get_pos() - ofs.x+=delta*SPEED - if (ofs.x>END): - ofs.x=BEGIN + ofs.x += delta*SPEED + if (ofs.x > END): + ofs.x = BEGIN set_pos(ofs) - + func _ready(): # Initialization here @@ -33,6 +32,3 @@ func _ready(): set_process(true) else: set_fixed_process(true) - pass - - diff --git a/demos/2d/navpoly/navigation.gd b/demos/2d/navpoly/navigation.gd index 9c3dc2921d..50ea4c8965 100644 --- a/demos/2d/navpoly/navigation.gd +++ b/demos/2d/navpoly/navigation.gd @@ -1,63 +1,54 @@ extends Navigation2D -# member variables here, example: -# var a=2 -# var b="textvar" -var begin=Vector2() -var end=Vector2() -var path=[] +# Member variables +const SPEED = 200.0 -const SPEED=200.0 - -func _process(delta): +var begin = Vector2() +var end = Vector2() +var path = [] - if (path.size()>1): - +func _process(delta): + if (path.size() > 1): var to_walk = delta*SPEED - while(to_walk>0 and path.size()>=2): - var pfrom = path[path.size()-1] - var pto = path[path.size()-2] + while(to_walk > 0 and path.size() >= 2): + var pfrom = path[path.size() - 1] + var pto = path[path.size() - 2] var d = pfrom.distance_to(pto) - if (d<=to_walk): - path.remove(path.size()-1) - to_walk-=d + if (d <= to_walk): + path.remove(path.size() - 1) + to_walk -= d else: - path[path.size()-1] = pfrom.linear_interpolate(pto,to_walk/d) - to_walk=0 - - var atpos = path[path.size()-1] + path[path.size() - 1] = pfrom.linear_interpolate(pto, to_walk/d) + to_walk = 0 + + var atpos = path[path.size() - 1] get_node("agent").set_pos(atpos) - if (path.size()<2): - path=[] + if (path.size() < 2): + path = [] set_process(false) - else: set_process(false) - func _update_path(): - - var p = get_simple_path(begin,end,true) - path=Array(p) # Vector2array to complex to use, convert to regular array + var p = get_simple_path(begin, end, true) + path = Array(p) # Vector2array too complex to use, convert to regular array path.invert() set_process(true) -func _input(ev): - if (ev.type==InputEvent.MOUSE_BUTTON and ev.pressed and ev.button_index==1): - begin=get_node("agent").get_pos() - #mouse to local navigatio cooards - end=ev.pos - get_pos() +func _input(event): + if (event.type == InputEvent.MOUSE_BUTTON and event.pressed and event.button_index == 1): + begin = get_node("agent").get_pos() + # Mouse to local navigation coordinates + end = event.pos - get_pos() _update_path() + func _ready(): # Initialization here set_process_input(true) - pass - - diff --git a/demos/2d/platformer/bullet.gd b/demos/2d/platformer/bullet.gd index 9aacc9809d..f66bca6b24 100644 --- a/demos/2d/platformer/bullet.gd +++ b/demos/2d/platformer/bullet.gd @@ -1,21 +1,17 @@ extends RigidBody2D -# member variables here, example: -# var a=2 -# var b="textvar" +# Member variables +var disabled = false -var disabled=false func disable(): if (disabled): return get_node("anim").play("shutdown") - disabled=true + disabled = true + func _ready(): # Initalization here get_node("Timer").start() - pass - - diff --git a/demos/2d/platformer/coin.gd b/demos/2d/platformer/coin.gd index 983cd46d88..1118732707 100644 --- a/demos/2d/platformer/coin.gd +++ b/demos/2d/platformer/coin.gd @@ -1,28 +1,19 @@ extends Area2D -# member variables here, example: -# var a=2 -# var b="textvar" - -var taken=false +# Member variables +var taken = false func _on_body_enter( body ): if (not taken and body extends preload("res://player.gd")): get_node("anim").play("taken") - taken=true - - -func _ready(): - # Initalization here - pass - + taken = true -func _on_coin_area_enter( area ): +func _on_coin_area_enter(area): pass # replace with function body -func _on_coin_area_enter_shape( area_id, area, area_shape, area_shape ): +func _on_coin_area_enter_shape(area_id, area, area_shape, area_shape): pass # replace with function body diff --git a/demos/2d/platformer/enemy.gd b/demos/2d/platformer/enemy.gd index a264cd0cff..75f13e3ee9 100644 --- a/demos/2d/platformer/enemy.gd +++ b/demos/2d/platformer/enemy.gd @@ -1,98 +1,84 @@ extends RigidBody2D -# member variables here, example: -# var a=2 -# var b="textvar" - +# Member variables const STATE_WALKING = 0 const STATE_DYING = 1 - var state = STATE_WALKING - var direction = -1 -var anim="" +var anim = "" -var rc_left=null -var rc_right=null +var rc_left = null +var rc_right = null var WALK_SPEED = 50 var bullet_class = preload("res://bullet.gd") + func _die(): queue_free() + func _pre_explode(): - #stay there + # Stay there clear_shapes() set_mode(MODE_STATIC) get_node("sound").play("explode") - -func _integrate_forces(s): +func _integrate_forces(s): var lv = s.get_linear_velocity() - var new_anim=anim + var new_anim = anim - if (state==STATE_DYING): - new_anim="explode" - elif (state==STATE_WALKING): - - new_anim="walk" + if (state == STATE_DYING): + new_anim = "explode" + elif (state == STATE_WALKING): + new_anim = "walk" - var wall_side=0.0 + var wall_side = 0.0 for i in range(s.get_contact_count()): var cc = s.get_contact_collider_object(i) var dp = s.get_contact_local_normal(i) if (cc): - - if (cc extends bullet_class and not cc.disabled): set_mode(MODE_RIGID) - state=STATE_DYING - #lv=s.get_contact_local_normal(i)*400 + state = STATE_DYING + #lv = s.get_contact_local_normal(i)*400 s.set_angular_velocity(sign(dp.x)*33.0) set_friction(1) cc.disable() get_node("sound").play("hit") - break - - - if (dp.x>0.9): - wall_side=1.0 - elif (dp.x<-0.9): - wall_side=-1.0 - - if (wall_side!=0 and wall_side!=direction): - - direction=-direction - get_node("sprite").set_scale( Vector2(-direction,1) ) - if (direction<0 and not rc_left.is_colliding() and rc_right.is_colliding()): - direction=-direction - get_node("sprite").set_scale( Vector2(-direction,1) ) - elif (direction>0 and not rc_right.is_colliding() and rc_left.is_colliding()): - direction=-direction - get_node("sprite").set_scale( Vector2(-direction,1) ) - - - lv.x = direction * WALK_SPEED - if( anim!=new_anim ): - anim=new_anim + if (dp.x > 0.9): + wall_side = 1.0 + elif (dp.x < -0.9): + wall_side = -1.0 + + if (wall_side != 0 and wall_side != direction): + direction = -direction + get_node("sprite").set_scale(Vector2(-direction, 1)) + if (direction < 0 and not rc_left.is_colliding() and rc_right.is_colliding()): + direction = -direction + get_node("sprite").set_scale(Vector2(-direction, 1)) + elif (direction > 0 and not rc_right.is_colliding() and rc_left.is_colliding()): + direction = -direction + get_node("sprite").set_scale(Vector2(-direction, 1)) + + lv.x = direction*WALK_SPEED + + if(anim != new_anim): + anim = new_anim get_node("anim").play(anim) - + s.set_linear_velocity(lv) func _ready(): # Initalization here - rc_left=get_node("raycast_left") - rc_right=get_node("raycast_right") - - - + rc_left = get_node("raycast_left") + rc_right = get_node("raycast_right") diff --git a/demos/2d/platformer/moving_platform.gd b/demos/2d/platformer/moving_platform.gd index 719d9e460e..db75f6db6c 100644 --- a/demos/2d/platformer/moving_platform.gd +++ b/demos/2d/platformer/moving_platform.gd @@ -1,27 +1,21 @@ extends Node2D -# member variables here, example: -# var a=2 -# var b="textvar" - +# Member variables export var motion = Vector2() export var cycle = 1.0 -var accum=0.0 +var accum = 0.0 -func _fixed_process(delta): - accum += delta * (1.0/cycle) * PI * 2.0 - accum = fmod(accum,PI*2.0) +func _fixed_process(delta): + accum += delta*(1.0/cycle)*PI*2.0 + accum = fmod(accum, PI*2.0) var d = sin(accum) var xf = Matrix32() - xf[2]= motion * d + xf[2]= motion*d get_node("platform").set_transform(xf) - + func _ready(): # Initalization here set_fixed_process(true) - pass - - diff --git a/demos/2d/platformer/player.gd b/demos/2d/platformer/player.gd index 9ee189df21..3bf4f0f94c 100644 --- a/demos/2d/platformer/player.gd +++ b/demos/2d/platformer/player.gd @@ -1,3 +1,4 @@ + extends RigidBody2D # Character Demo, written by Juan Linietsky. @@ -24,42 +25,40 @@ extends RigidBody2D # -Friction cant be used, so floor velocity must be considered # for moving platforms. -var anim="" -var siding_left=false -var jumping=false -var stopping_jump=false -var shooting=false +# Member variables +var anim = "" +var siding_left = false +var jumping = false +var stopping_jump = false +var shooting = false var WALK_ACCEL = 800.0 -var WALK_DEACCEL= 800.0 -var WALK_MAX_VELOCITY= 200.0 +var WALK_DEACCEL = 800.0 +var WALK_MAX_VELOCITY = 200.0 var AIR_ACCEL = 200.0 -var AIR_DEACCEL= 200.0 -var JUMP_VELOCITY=460 -var STOP_JUMP_FORCE=900.0 +var AIR_DEACCEL = 200.0 +var JUMP_VELOCITY = 460 +var STOP_JUMP_FORCE = 900.0 var MAX_FLOOR_AIRBORNE_TIME = 0.15 -var airborne_time=1e20 -var shoot_time=1e20 +var airborne_time = 1e20 +var shoot_time = 1e20 var MAX_SHOOT_POSE_TIME = 0.3 var bullet = preload("res://bullet.xml") -var floor_h_velocity=0.0 +var floor_h_velocity = 0.0 var enemy -func _integrate_forces(s): - - +func _integrate_forces(s): var lv = s.get_linear_velocity() var step = s.get_step() - var new_anim=anim - var new_siding_left=siding_left - + var new_anim = anim + var new_siding_left = siding_left # Get the controls var move_left = Input.is_action_pressed("move_left") @@ -74,188 +73,161 @@ func _integrate_forces(s): p.y = p.y - 100 e.set_pos(p) get_parent().add_child(e) - - - #deapply prev floor velocity - lv.x-=floor_h_velocity - floor_h_velocity=0.0 + # Deapply prev floor velocity + lv.x -= floor_h_velocity + floor_h_velocity = 0.0 # Find the floor (a contact with upwards facing collision normal) - var found_floor=false - var floor_index=-1 + var found_floor = false + var floor_index = -1 for x in range(s.get_contact_count()): - var ci = s.get_contact_local_normal(x) - if (ci.dot(Vector2(0,-1))>0.6): - found_floor=true - floor_index=x + if (ci.dot(Vector2(0, -1)) > 0.6): + found_floor = true + floor_index = x # A good idea when impementing characters of all kinds, - # Compensates for physics imprecission, as well as human - # reaction delay. - + # compensates for physics imprecission, as well as human reaction delay. if (shoot and not shooting): - shoot_time=0 + shoot_time = 0 var bi = bullet.instance() var ss if (siding_left): - ss=-1.0 + ss = -1.0 else: - ss=1.0 - var pos = get_pos() + get_node("bullet_shoot").get_pos()*Vector2(ss,1.0) - + ss = 1.0 + var pos = get_pos() + get_node("bullet_shoot").get_pos()*Vector2(ss, 1.0) + bi.set_pos(pos) get_parent().add_child(bi) - - bi.set_linear_velocity( Vector2(800.0*ss,-80) ) - get_node("sprite/smoke").set_emitting(true) + + bi.set_linear_velocity(Vector2(800.0*ss, -80)) + get_node("sprite/smoke").set_emitting(true) get_node("sound").play("shoot") - PS2D.body_add_collision_exception(bi.get_rid(),get_rid()) # make bullet and this not collide - - + PS2D.body_add_collision_exception(bi.get_rid(), get_rid()) # Make bullet and this not collide else: - shoot_time+=step - + shoot_time += step if (found_floor): - airborne_time=0.0 + airborne_time = 0.0 else: - airborne_time+=step #time it spent in the air - + airborne_time += step # Time it spent in the air + var on_floor = airborne_time < MAX_FLOOR_AIRBORNE_TIME - # Process jump + # Process jump if (jumping): - if (lv.y>0): - #set off the jumping flag if going down - jumping=false + if (lv.y > 0): + # Set off the jumping flag if going down + jumping = false elif (not jump): - stopping_jump=true - - if (stopping_jump): - lv.y+=STOP_JUMP_FORCE*step + stopping_jump = true + if (stopping_jump): + lv.y += STOP_JUMP_FORCE*step + if (on_floor): - # Process logic when character is on floor - if (move_left and not move_right): if (lv.x > -WALK_MAX_VELOCITY): - lv.x-=WALK_ACCEL*step + lv.x -= WALK_ACCEL*step elif (move_right and not move_left): if (lv.x < WALK_MAX_VELOCITY): - lv.x+=WALK_ACCEL*step + lv.x += WALK_ACCEL*step else: var xv = abs(lv.x) - xv-=WALK_DEACCEL*step - if (xv<0): - xv=0 - lv.x=sign(lv.x)*xv - - #Check jump + xv -= WALK_DEACCEL*step + if (xv < 0): + xv = 0 + lv.x = sign(lv.x)*xv + + # Check jump if (not jumping and jump): - lv.y=-JUMP_VELOCITY - jumping=true - stopping_jump=false + lv.y = -JUMP_VELOCITY + jumping = true + stopping_jump = false get_node("sound").play("jump") - - #check siding + # Check siding if (lv.x < 0 and move_left): - new_siding_left=true + new_siding_left = true elif (lv.x > 0 and move_right): - new_siding_left=false + new_siding_left = false if (jumping): - new_anim="jumping" - elif (abs(lv.x)<0.1): - if (shoot_time<MAX_SHOOT_POSE_TIME): - new_anim="idle_weapon" + new_anim = "jumping" + elif (abs(lv.x) < 0.1): + if (shoot_time < MAX_SHOOT_POSE_TIME): + new_anim = "idle_weapon" else: - new_anim="idle" + new_anim = "idle" else: - if (shoot_time<MAX_SHOOT_POSE_TIME): - new_anim="run_weapon" + if (shoot_time < MAX_SHOOT_POSE_TIME): + new_anim = "run_weapon" else: - new_anim="run" + new_anim = "run" else: - # Process logic when the character is in the air - if (move_left and not move_right): if (lv.x > -WALK_MAX_VELOCITY): - lv.x-=AIR_ACCEL*step + lv.x -= AIR_ACCEL*step elif (move_right and not move_left): if (lv.x < WALK_MAX_VELOCITY): - lv.x+=AIR_ACCEL*step + lv.x += AIR_ACCEL*step else: var xv = abs(lv.x) - xv-=AIR_DEACCEL*step - if (xv<0): - xv=0 - lv.x=sign(lv.x)*xv - - if (lv.y<0): - if (shoot_time<MAX_SHOOT_POSE_TIME): - new_anim="jumping_weapon" + xv -= AIR_DEACCEL*step + if (xv < 0): + xv = 0 + lv.x = sign(lv.x)*xv + + if (lv.y < 0): + if (shoot_time < MAX_SHOOT_POSE_TIME): + new_anim = "jumping_weapon" else: - new_anim="jumping" + new_anim = "jumping" else: - if (shoot_time<MAX_SHOOT_POSE_TIME): - new_anim="falling_weapon" + if (shoot_time < MAX_SHOOT_POSE_TIME): + new_anim = "falling_weapon" else: - new_anim="falling" - - - #Update siding + new_anim = "falling" - if (new_siding_left!=siding_left): + # Update siding + if (new_siding_left != siding_left): if (new_siding_left): - get_node("sprite").set_scale( Vector2(-1,1) ) + get_node("sprite").set_scale(Vector2(-1, 1)) else: - get_node("sprite").set_scale( Vector2(1,1) ) - - siding_left=new_siding_left - - #Change animation - if (new_anim!=anim): - anim=new_anim - get_node("anim").play(anim) - - shooting=shoot - - # Apply floor velocity - if (found_floor): - floor_h_velocity=s.get_contact_collider_velocity_at_pos(floor_index).x - lv.x+=floor_h_velocity + get_node("sprite").set_scale(Vector2(1, 1)) + siding_left = new_siding_left - - - #Finally, apply gravity and set back the linear velocity - lv+=s.get_total_gravity()*step - s.set_linear_velocity(lv) - + # Change animation + if (new_anim != anim): + anim = new_anim + get_node("anim").play(anim) - + shooting = shoot + # Apply floor velocity + if (found_floor): + floor_h_velocity = s.get_contact_collider_velocity_at_pos(floor_index).x + lv.x += floor_h_velocity + # Finally, apply gravity and set back the linear velocity + lv += s.get_total_gravity()*step + s.set_linear_velocity(lv) func _ready(): # Initalization here - + enemy = ResourceLoader.load("res://enemy.xml") + # if !Globals.has_singleton("Facebook"): -# return +# return # var Facebook = Globals.get_singleton("Facebook") # var link = Globals.get("facebook/link") # var icon = Globals.get("facebook/icon") # var msg = "I just sneezed on your wall! Beat my score and Stop the Running nose!" # var title = "I just sneezed on your wall!" # Facebook.post("feed", msg, title, link, icon) - enemy = ResourceLoader.load("res://enemy.xml") - pass - - - diff --git a/demos/2d/polygon_path_finder/polygonpathfinder.gd b/demos/2d/polygon_path_finder/polygonpathfinder.gd index a0e71dd127..1e843043da 100644 --- a/demos/2d/polygon_path_finder/polygonpathfinder.gd +++ b/demos/2d/polygon_path_finder/polygonpathfinder.gd @@ -1,37 +1,38 @@ extends Spatial + func _ready(): var pf = PolygonPathFinder.new() var points = Vector2Array() var connections = IntArray() - # poly 1 - points.push_back(Vector2(0, 0)) #0 - points.push_back(Vector2(10, 0)) #1 - points.push_back(Vector2(10, 10)) #2 - points.push_back(Vector2(0, 10)) #3 - - connections.push_back(0) # connect vertex 0 ... + # Poly 1 + points.push_back(Vector2(0, 0)) # 0 + points.push_back(Vector2(10, 0)) # 1 + points.push_back(Vector2(10, 10)) # 2 + points.push_back(Vector2(0, 10)) # 3 + + connections.push_back(0) # Connect vertex 0... connections.push_back(1) # ... to 1 drawLine(points[0], points[1], get_node("/root/Spatial/Polys")) - connections.push_back(1) # connect vertex 1 ... + connections.push_back(1) # Connect vertex 1... connections.push_back(2) # ... to 2 drawLine(points[1], points[2], get_node("/root/Spatial/Polys")) - connections.push_back(2) # etc. + connections.push_back(2) # Etc. connections.push_back(3) drawLine(points[2], points[3], get_node("/root/Spatial/Polys")) - connections.push_back(3) # connect vertex 3 ... - connections.push_back(0) # back to vertex 0, to close the polygon + connections.push_back(3) # Connect vertex 3... + connections.push_back(0) # ... back to vertex 0, to close the polygon drawLine(points[3], points[0], get_node("/root/Spatial/Polys")) - - # poly 2, as obstacle inside poly 1 - points.push_back(Vector2(2, 0.5)) #4 - points.push_back(Vector2(4, 0.5)) #5 - points.push_back(Vector2(4, 9.5)) #6 - points.push_back(Vector2(2, 9.5)) #7 - + + # Poly 2, as obstacle inside poly 1 + points.push_back(Vector2(2, 0.5)) # 4 + points.push_back(Vector2(4, 0.5)) # 5 + points.push_back(Vector2(4, 9.5)) # 6 + points.push_back(Vector2(2, 9.5)) # 7 + connections.push_back(4) connections.push_back(5) drawLine(points[4], points[5], get_node("/root/Spatial/Polys")) @@ -44,24 +45,22 @@ func _ready(): connections.push_back(7) connections.push_back(4) drawLine(points[7], points[4], get_node("/root/Spatial/Polys")) - - print("points: ",points) - print("connections: ",connections) + print("points: ", points) + print("connections: ", connections) pf.setup(points, connections) var path = pf.find_path(Vector2(1, 5), Vector2(8, 5)) var lastStep = null - print("path: ",path) + print("path: ", path) for step in path: - print("step: ",step) + print("step: ", step) if (lastStep != null): var currPathSegment = Vector2Array() drawLine(lastStep, step, get_node("/root/Spatial/Path")) lastStep = step - func drawLine(pointA, pointB, immediateGeo): @@ -76,5 +75,3 @@ func drawLine(pointA, pointB, immediateGeo): im.add_vertex(Vector3(pointA.x, drawPosY, pointA.y)) im.add_vertex(Vector3(pointB.x, drawPosY, pointB.y)) im.end() - - diff --git a/demos/2d/pong/pong.gd b/demos/2d/pong/pong.gd index cf6003c659..b31e3c3d71 100644 --- a/demos/2d/pong/pong.gd +++ b/demos/2d/pong/pong.gd @@ -1,73 +1,68 @@ extends Node2D -#member variables here, example: -#var a=2 -#var b="textvar" +# Member variables const INITIAL_BALL_SPEED = 80 var ball_speed = INITIAL_BALL_SPEED -var screen_size = Vector2(640,400) -#default ball direction -var direction = Vector2(-1,0) -var pad_size = Vector2(8,32) +var screen_size = Vector2(640, 400) + +# Default ball direction +var direction = Vector2(-1, 0) +var pad_size = Vector2(8, 32) const PAD_SPEED = 150 func _process(delta): - - - #get ball position and pad rectangles + # Get ball position and pad rectangles var ball_pos = get_node("ball").get_pos() - 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 ) + 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) - #integrate new ball postion - ball_pos+=direction*ball_speed*delta + # Integrate new ball postion + ball_pos += direction*ball_speed*delta - #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)): + # 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)): direction.y = -direction.y - - #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 + + # 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 direction = direction.normalized() - - #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) - - + + # 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) + get_node("ball").set_pos(ball_pos) - - #move left pad + + # Move left pad var left_pos = get_node("left").get_pos() if (left_pos.y > 0 and Input.is_action_pressed("left_move_up")): - left_pos.y+=-PAD_SPEED*delta + left_pos.y += -PAD_SPEED*delta if (left_pos.y < screen_size.y and Input.is_action_pressed("left_move_down")): - left_pos.y+=PAD_SPEED*delta - + left_pos.y += PAD_SPEED*delta + get_node("left").set_pos(left_pos) - - #move right pad + + # Move right pad var right_pos = get_node("right").get_pos() if (right_pos.y > 0 and Input.is_action_pressed("right_move_up")): - right_pos.y+=-PAD_SPEED*delta + right_pos.y += -PAD_SPEED*delta if (right_pos.y < screen_size.y and Input.is_action_pressed("right_move_down")): - right_pos.y+=PAD_SPEED*delta - - get_node("right").set_pos(right_pos) + right_pos.y += PAD_SPEED*delta - + get_node("right").set_pos(right_pos) + func _ready(): - screen_size = get_viewport_rect().size #get actual size + # Initalization here + screen_size = get_viewport_rect().size # Get actual size pad_size = get_node("left").get_texture().get_size() set_process(true) - diff --git a/demos/2d/screen_space_shaders/screen_shaders.gd b/demos/2d/screen_space_shaders/screen_shaders.gd index 4e8a548539..e2fb50048f 100644 --- a/demos/2d/screen_space_shaders/screen_shaders.gd +++ b/demos/2d/screen_space_shaders/screen_shaders.gd @@ -1,32 +1,26 @@ extends Control -# member variables here, example: -# var a=2 -# var b="textvar" func _ready(): # Initialization here for c in get_node("pictures").get_children(): - get_node("picture").add_item("PIC: "+c.get_name()) + get_node("picture").add_item("PIC: " + c.get_name()) for c in get_node("effects").get_children(): - get_node("effect").add_item("FX: "+c.get_name()) - pass + get_node("effect").add_item("FX: " + c.get_name()) - - -func _on_picture_item_selected( ID ): +func _on_picture_item_selected(ID): for c in range(get_node("pictures").get_child_count()): - if (ID==c): + if (ID == c): get_node("pictures").get_child(c).show() else: get_node("pictures").get_child(c).hide() -func _on_effect_item_selected( ID ): +func _on_effect_item_selected(ID): for c in range(get_node("effects").get_child_count()): - if (ID==c): + if (ID == c): get_node("effects").get_child(c).show() else: get_node("effects").get_child(c).hide() diff --git a/demos/2d/shower_of_bullets/bullets.gd b/demos/2d/shower_of_bullets/bullets.gd index 79f4faaae6..b62c796f32 100644 --- a/demos/2d/shower_of_bullets/bullets.gd +++ b/demos/2d/shower_of_bullets/bullets.gd @@ -4,73 +4,69 @@ extends Node2D # This demo is an example of controling a high number of 2D objects with logic and collision without using scene nodes. # This technique is a lot more efficient than using instancing and nodes, but requires more programming and is less visual +# Member variables const BULLET_COUNT = 500 const SPEED_MIN = 20 const SPEED_MAX = 50 -var bullets=[] +var bullets = [] var shape + +# Inner classes class Bullet: var pos = Vector2() var speed = 1.0 var body = RID() - -func _draw(): +func _draw(): var t = preload("res://bullet.png") var tofs = -t.get_size()*0.5 for b in bullets: - draw_texture(t,b.pos+tofs) - - + draw_texture(t, b.pos + tofs) + + func _process(delta): var width = get_viewport_rect().size.x*2.0 var mat = Matrix32() for b in bullets: - b.pos.x-=b.speed*delta + b.pos.x -= b.speed*delta if (b.pos.x < -30): - b.pos.x+=width - mat.o=b.pos - - Physics2DServer.body_set_state(b.body,Physics2DServer.BODY_STATE_TRANSFORM,mat) + b.pos.x += width + mat.o = b.pos + Physics2DServer.body_set_state(b.body, Physics2DServer.BODY_STATE_TRANSFORM, mat) + update() - - -func _ready(): - shape = Physics2DServer.shape_create(Physics2DServer.SHAPE_CIRCLE) - Physics2DServer.shape_set_data(shape,8) #radius +func _ready(): + # Initialization here + shape = Physics2DServer.shape_create(Physics2DServer.SHAPE_CIRCLE) + Physics2DServer.shape_set_data(shape, 8) # Radius + for i in range(BULLET_COUNT): var b = Bullet.new() - b.speed=rand_range(SPEED_MIN,SPEED_MAX) + b.speed = rand_range(SPEED_MIN, SPEED_MAX) b.body = Physics2DServer.body_create(Physics2DServer.BODY_MODE_KINEMATIC) - Physics2DServer.body_set_space(b.body,get_world_2d().get_space()) - Physics2DServer.body_add_shape(b.body,shape) + Physics2DServer.body_set_space(b.body, get_world_2d().get_space()) + Physics2DServer.body_add_shape(b.body, shape) - b.pos = Vector2( get_viewport_rect().size * Vector2(randf()*2.0,randf()) ) #twice as long - b.pos.x += get_viewport_rect().size.x # start outside + b.pos = Vector2(get_viewport_rect().size * Vector2(randf()*2.0, randf())) # Twice as long + b.pos.x += get_viewport_rect().size.x # Start outside var mat = Matrix32() - mat.o=b.pos - Physics2DServer.body_set_state(b.body,Physics2DServer.BODY_STATE_TRANSFORM,mat) + mat.o = b.pos + Physics2DServer.body_set_state(b.body, Physics2DServer.BODY_STATE_TRANSFORM, mat) bullets.append(b) - - - set_process(true) - + set_process(true) + + func _exit_tree(): for b in bullets: Physics2DServer.free_rid(b.body) Physics2DServer.free_rid(shape) - # Initalization here bullets.clear() - - pass - - diff --git a/demos/2d/shower_of_bullets/shower.gd b/demos/2d/shower_of_bullets/shower.gd index bba8431764..0f87c60bcd 100644 --- a/demos/2d/shower_of_bullets/shower.gd +++ b/demos/2d/shower_of_bullets/shower.gd @@ -1,32 +1,26 @@ extends Node2D -# member variables here, example: -# var a=2 -# var b="textvar" +# Member variables +var touching = 0 -var touching=0 +func _input(event): + if (event.type == InputEvent.MOUSE_MOTION): + get_node("player").set_pos(event.pos - Vector2(0, 16)) -func _input(ev): - if (ev.type==InputEvent.MOUSE_MOTION): - get_node("player").set_pos(ev.pos-Vector2(0,16)) - - -func _on_player_body_enter_shape( body_id, body, body_shape, area_shape ): - - touching+=1 - if (touching==1): +func _on_player_body_enter_shape(body_id, body, body_shape, area_shape): + touching += 1 + if (touching == 1): get_node("player/sprite").set_frame(1) -func _on_player_body_exit_shape( body_id, body, body_shape, area_shape ): - - touching-=1 - if (touching==0): +func _on_player_body_exit_shape(body_id, body, body_shape, area_shape): + touching -= 1 + if (touching == 0): get_node("player/sprite").set_frame(0) func _ready(): + # Initialization here set_process_input(true) - pass diff --git a/demos/2d/space_shooter/asteroid.gd b/demos/2d/space_shooter/asteroid.gd index f21b9777bb..0a54e4e780 100644 --- a/demos/2d/space_shooter/asteroid.gd +++ b/demos/2d/space_shooter/asteroid.gd @@ -1,49 +1,44 @@ extends Area2D -# member variables here, example: -# var a=2 -# var b="textvar" +# Member variables +const SPEED = -200 +const Y_RANDOM = 10 -const SPEED=-200 -const Y_RANDOM=10 +var points = 1 +var speed_y = 0.0 +var destroyed = false -var points=1 - - -var speed_y=0.0 func _process(delta): + translate(Vector2(SPEED, speed_y)*delta) - translate( Vector2(SPEED,speed_y) * delta ) func _ready(): # Initialization here - speed_y=rand_range(-Y_RANDOM,Y_RANDOM) - pass + speed_y = rand_range(-Y_RANDOM, Y_RANDOM) -var destroyed=false func destroy(): if (destroyed): - return - destroyed=true + return + destroyed = true get_node("anim").play("explode") set_process(false) get_node("sfx").play("sound_explode") - #accum points - get_node("/root/game_state").points+=1 - + # Accumulate points + get_node("/root/game_state").points += 1 + + func is_enemy(): - return not destroyed - + return not destroyed + func _on_visibility_enter_screen(): set_process(true) - #make it spin! + # Make it spin! get_node("anim").play("spin") - + func _on_visibility_exit_screen(): queue_free() - pass # replace with function body diff --git a/demos/2d/space_shooter/enemy1.gd b/demos/2d/space_shooter/enemy1.gd index 051798742a..204995c05d 100644 --- a/demos/2d/space_shooter/enemy1.gd +++ b/demos/2d/space_shooter/enemy1.gd @@ -1,17 +1,15 @@ extends Area2D -# member variables here, example: -# var a=2 -# var b="textvar" +# Member variables +const SPEED = -200 + +var destroyed=false -const SPEED=-200 func _process(delta): - get_parent().translate(Vector2(SPEED*delta,0)) + get_parent().translate(Vector2(SPEED*delta, 0)) - -var destroyed=false func is_enemy(): return not destroyed @@ -19,19 +17,20 @@ func is_enemy(): func destroy(): if (destroyed): - return - destroyed=true + return + destroyed = true get_node("anim").play("explode") - set_process(false) + set_process(false) get_node("sfx").play("sound_explode") - #accum points - get_node("/root/game_state").points+=5 + # Accumulate points + get_node("/root/game_state").points += 5 + func _on_visibility_enter_screen(): set_process(true) - get_node("anim").play("zigzag") - get_node("anim").seek(randf()*2.0) #make it start from any pos + get_node("anim").play("zigzag") + get_node("anim").seek(randf()*2.0) # Make it start from any pos + func _on_visibility_exit_screen(): queue_free() - diff --git a/demos/2d/space_shooter/enemy2.gd b/demos/2d/space_shooter/enemy2.gd index 4f632a053d..ca35f45faa 100644 --- a/demos/2d/space_shooter/enemy2.gd +++ b/demos/2d/space_shooter/enemy2.gd @@ -1,56 +1,52 @@ extends Area2D -# member variables here, example: -# var a=2 -# var b="textvar" -const SPEED=-220 -const SHOOT_INTERVAL=1 -var shoot_timeout=0 +# Member variables +const SPEED = -220 +const SHOOT_INTERVAL = 1 + +var shoot_timeout = 0 +var destroyed=false + func _process(delta): - translate( Vector2(SPEED*delta,0) ) - shoot_timeout-=delta - - if (shoot_timeout<0): + translate(Vector2(SPEED*delta, 0)) + shoot_timeout -= delta - shoot_timeout=SHOOT_INTERVAL + if (shoot_timeout < 0): + shoot_timeout = SHOOT_INTERVAL - #instance a shot + # Instance a shot var shot = preload("res://enemy_shot.scn").instance() - #set pos as "shoot_from" Position2D node - shot.set_pos( get_node("shoot_from").get_global_pos() ) - #add it to parent, so it has world coordinates + # Set pos as "shoot_from" Position2D node + shot.set_pos(get_node("shoot_from").get_global_pos()) + # Add it to parent, so it has world coordinates get_parent().add_child(shot) - -var destroyed=false + func is_enemy(): return not destroyed + func destroy(): if (destroyed): - return - destroyed=true + return + destroyed = true get_node("anim").play("explode") - set_process(false) + set_process(false) get_node("sfx").play("sound_explode") - #accum points - get_node("/root/game_state").points+=10 + # Accumulate points + get_node("/root/game_state").points += 10 + func _ready(): - set_fixed_process(true) # Initialization here - pass - - + set_fixed_process(true) func _on_visibility_enter_screen(): set_process(true) - pass # replace with function body func _on_visibility_exit_screen(): queue_free() - pass # replace with function body diff --git a/demos/2d/space_shooter/enemy_shot.gd b/demos/2d/space_shooter/enemy_shot.gd index 238d24e4a2..8a27a4ca22 100644 --- a/demos/2d/space_shooter/enemy_shot.gd +++ b/demos/2d/space_shooter/enemy_shot.gd @@ -1,32 +1,32 @@ extends Area2D -# member variables here, example: -# var a=2 -# var b="textvar" - +# Member variables const SPEED = -800 +var hit = false + + func _process(delta): - translate(Vector2(delta*SPEED,0)) + translate(Vector2(delta*SPEED, 0)) + func _ready(): # Initialization here set_process(true) -var hit=false - func is_enemy(): return true + func _hit_something(): if (hit): return - hit=true + hit = true set_process(false) get_node("anim").play("splash") + func _on_visibility_exit_screen(): queue_free() - diff --git a/demos/2d/space_shooter/game_state.gd b/demos/2d/space_shooter/game_state.gd index f66d0fa8fa..0aa5e1f42b 100644 --- a/demos/2d/space_shooter/game_state.gd +++ b/demos/2d/space_shooter/game_state.gd @@ -1,24 +1,22 @@ -extends Node +extends Node +# Member variables var points = 0 var max_points = 0 func _ready(): var f = File.new() - #load high score - - if (f.open("user://highscore",File.READ)==OK): - - max_points=f.get_var() + # Load high score + if (f.open("user://highscore", File.READ) == OK): + max_points = f.get_var() func game_over(): - if (points>max_points): - max_points=points - #save high score + if (points > max_points): + max_points = points + # Save high score var f = File.new() - f.open("user://highscore",File.WRITE) + f.open("user://highscore", File.WRITE) f.store_var(max_points) -
\ No newline at end of file diff --git a/demos/2d/space_shooter/main_menu.gd b/demos/2d/space_shooter/main_menu.gd index 52221aba1b..8b3162389e 100644 --- a/demos/2d/space_shooter/main_menu.gd +++ b/demos/2d/space_shooter/main_menu.gd @@ -1,20 +1,12 @@ extends Control -# member variables here, example: -# var a=2 -# var b="textvar" func _ready(): - - get_node("score").set_text( "HIGH SCORE: "+str( get_node("/root/game_state").max_points ) ) # Initialization here - pass - - + get_node("score").set_text("HIGH SCORE: " + str(get_node("/root/game_state").max_points)) func _on_play_pressed(): - get_node("/root/game_state").points=0 + get_node("/root/game_state").points = 0 get_tree().change_scene("res://level.scn") - pass # replace with function body diff --git a/demos/2d/space_shooter/rail.gd b/demos/2d/space_shooter/rail.gd index 22ebd02670..b00684a98f 100644 --- a/demos/2d/space_shooter/rail.gd +++ b/demos/2d/space_shooter/rail.gd @@ -1,25 +1,20 @@ extends Node2D +# Member variables +const SPEED = 200 +var offset = 0 -const SPEED=200 -# member variables here, example: -# var a=2 -# var b="textvar" func stop(): set_process(false) -var offset=0 - func _process(delta): - offset+=delta*SPEED - set_pos(Vector2(offset,0)) + offset += delta*SPEED + set_pos(Vector2(offset, 0)) + func _ready(): - set_process(true) # Initialization here - - - + set_process(true) diff --git a/demos/2d/space_shooter/ship.gd b/demos/2d/space_shooter/ship.gd index fa444868a4..b9020d6fe1 100644 --- a/demos/2d/space_shooter/ship.gd +++ b/demos/2d/space_shooter/ship.gd @@ -1,71 +1,66 @@ extends Area2D -# member variables here, example: -# var a=2 -# var b="textvar" - +# Member variables const SPEED = 200 var screen_size +var prev_shooting = false +var killed = false -var prev_shooting=false func _process(delta): - var motion = Vector2() if Input.is_action_pressed("move_up"): - motion+=Vector2(0,-1) + motion += Vector2(0, -1) if Input.is_action_pressed("move_down"): - motion+=Vector2(0,1) + motion += Vector2(0, 1) if Input.is_action_pressed("move_left"): - motion+=Vector2(-1,0) + motion += Vector2(-1, 0) if Input.is_action_pressed("move_right"): - motion+=Vector2(1,0) + motion += Vector2(1, 0) var shooting = Input.is_action_pressed("shoot") - + var pos = get_pos() - pos+=motion*delta*SPEED - if (pos.x<0): - pos.x=0 - if (pos.x>screen_size.x): - pos.x=screen_size.x - if (pos.y<0): - pos.y=0 - if (pos.y>screen_size.y): - pos.y=screen_size.y - + pos += motion*delta*SPEED + if (pos.x < 0): + pos.x = 0 + if (pos.x > screen_size.x): + pos.x = screen_size.x + if (pos.y < 0): + pos.y = 0 + if (pos.y > screen_size.y): + pos.y = screen_size.y + set_pos(pos) if (shooting and not prev_shooting): - # just pressed + # Just pressed var shot = preload("res://shot.scn").instance() - #use the position3d as reference - shot.set_pos( get_node("shootfrom").get_global_pos() ) - #put it two parents above, so it is not moved by us + # Use the Position2D as reference + shot.set_pos(get_node("shootfrom").get_global_pos()) + # Put it two parents above, so it is not moved by us get_node("../..").add_child(shot) - #play sound + # Play sound get_node("sfx").play("shoot") - prev_shooting = shooting + + # Update points counter + get_node("../hud/score_points").set_text(str(get_node("/root/game_state").points)) - #update points counter - get_node("../hud/score_points").set_text( str(get_node("/root/game_state").points) ) func _ready(): # Initialization here screen_size = get_viewport().get_rect().size set_process(true) - pass -var killed=false func _hit_something(): if (killed): return - killed=true + killed = true get_node("anim").play("explode") get_node("sfx").play("sound_explode") get_node("../hud/game_over").show() @@ -74,15 +69,14 @@ func _hit_something(): set_process(false) -func _on_ship_body_enter( body ): +func _on_ship_body_enter(body): _hit_something() -func _on_ship_area_enter( area ): +func _on_ship_area_enter(area): if (area.has_method("is_enemy") and area.is_enemy()): _hit_something() func _on_back_to_menu_pressed(): get_tree().change_scene("res://main_menu.scn") - pass # replace with function body diff --git a/demos/2d/space_shooter/shot.gd b/demos/2d/space_shooter/shot.gd index 28b67bd26d..31b5b24367 100644 --- a/demos/2d/space_shooter/shot.gd +++ b/demos/2d/space_shooter/shot.gd @@ -1,48 +1,41 @@ extends Area2D -# member variables here, example: -# var a=2 -# var b="textvar" - +# Member variables const SPEED = 800 +var hit = false + + func _process(delta): - translate(Vector2(delta*SPEED,0)) + translate(Vector2(delta*SPEED, 0)) + func _ready(): # Initialization here set_process(true) - pass -var hit=false func _hit_something(): if (hit): return - hit=true + hit = true set_process(false) get_node("anim").play("splash") + func _on_visibility_exit_screen(): queue_free() - pass # replace with function body - -func _on_shot_area_enter( area ): - #hit an enemy or asteroid +func _on_shot_area_enter(area): + # Hit an enemy or asteroid if (area.has_method("destroy")): - #duck typing at it's best + # Duck typing at it's best area.destroy() _hit_something() - - - pass -func _on_shot_body_enter( body ): - #hit the tilemap +func _on_shot_body_enter(body): + # Hit the tilemap _hit_something() - pass # replace with function body - diff --git a/demos/2d/tetris/grid.gd b/demos/2d/tetris/grid.gd index 8708d168e4..73607d7cf6 100644 --- a/demos/2d/tetris/grid.gd +++ b/demos/2d/tetris/grid.gd @@ -1,82 +1,76 @@ - extends Control # Simple Tetris-like demo, (c) 2012 Juan Linietsky # Implemented by using a regular Control and drawing on it during the _draw() callback. # The drawing surface is updated only when changes happen (by calling update()) - +# Member variables var score = 0 -var score_label=null +var score_label = null const MAX_SHAPES = 7 var block = preload("block.png") -var block_colors=[ - Color(1,0.5,0.5), - Color(0.5,1,0.5), - Color(0.5,0.5,1), - Color(0.8,0.4,0.8), - Color(0.8,0.8,0.4), - Color(0.4,0.8,0.8), - Color(0.7,0.7,0.7)] - -var block_shapes=[ - [ Vector2(0,-1),Vector2(0,0),Vector2(0,1),Vector2(0,2) ], # I - [ Vector2(0,0),Vector2(1,0),Vector2(1,1),Vector2(0,1) ], # O - [ Vector2(-1,1),Vector2(0,1),Vector2(0,0),Vector2(1,0) ], # S - [ Vector2(1,1),Vector2(0,1),Vector2(0,0),Vector2(-1,0) ], # Z - [ Vector2(-1,1),Vector2(-1,0),Vector2(0,0),Vector2(1,0) ], # L - [ Vector2(1,1),Vector2(1,0),Vector2(0,0),Vector2(-1,0) ], # J - [ Vector2(0,1),Vector2(1,0),Vector2(0,0),Vector2(-1,0) ]] # T - +var block_colors = [ + Color(1, 0.5, 0.5), + Color(0.5, 1, 0.5), + Color(0.5, 0.5, 1), + Color(0.8, 0.4, 0.8), + Color(0.8, 0.8, 0.4), + Color(0.4, 0.8, 0.8), + Color(0.7, 0.7, 0.7)] -var block_rotations=[ - Matrix32( Vector2(1,0),Vector2(0,1), Vector2() ), - Matrix32( Vector2(0,1),Vector2(-1,0), Vector2() ), - Matrix32( Vector2(-1,0),Vector2(0,-1), Vector2() ), - Matrix32( Vector2(0,-1),Vector2(1,0), Vector2() ) -] - +var block_shapes = [ + [ Vector2(0, -1), Vector2(0, 0), Vector2(0, 1), Vector2(0, 2) ], # I + [ Vector2(0, 0), Vector2(1, 0), Vector2(1, 1), Vector2(0, 1) ], # O + [ Vector2(-1, 1), Vector2(0, 1), Vector2(0, 0), Vector2(1, 0) ], # S + [ Vector2(1, 1), Vector2(0, 1), Vector2(0, 0), Vector2(-1, 0) ], # Z + [ Vector2(-1, 1), Vector2(-1, 0), Vector2(0, 0), Vector2(1, 0) ], # L + [ Vector2(1, 1), Vector2(1, 0), Vector2(0, 0), Vector2(-1, 0) ], # J + [ Vector2(0, 1), Vector2(1, 0), Vector2(0, 0), Vector2(-1, 0) ]] # T -var width=0 -var height=0 +var block_rotations = [ + Matrix32(Vector2(1, 0), Vector2(0, 1), Vector2()), + Matrix32(Vector2(0, 1), Vector2(-1, 0), Vector2()), + Matrix32(Vector2(-1, 0), Vector2(0, -1), Vector2()), + Matrix32(Vector2(0, -1), Vector2(1, 0), Vector2())] -var cells={} +var width = 0 +var height = 0 -var piece_active=false -var piece_shape=0 -var piece_pos=Vector2() -var piece_rot=0 +var cells = {} +var piece_active = false +var piece_shape = 0 +var piece_pos = Vector2() +var piece_rot = 0 -func piece_cell_xform(p,er=0): - var r = (4+er+piece_rot)%4 - return piece_pos+block_rotations[r].xform(p) -func _draw(): +func piece_cell_xform(p, er = 0): + var r = (4 + er + piece_rot) % 4 + return piece_pos + block_rotations[r].xform(p) + - var sb = get_stylebox("bg","Tree") # use line edit bg - draw_style_box(sb,Rect2(Vector2(),get_size()).grow(3)) +func _draw(): + var sb = get_stylebox("bg", "Tree") # Use line edit bg + draw_style_box(sb, Rect2(Vector2(), get_size()).grow(3)) var bs = block.get_size() for y in range(height): for x in range(width): - if (Vector2(x,y) in cells): - draw_texture_rect(block,Rect2(Vector2(x,y)*bs,bs),false,block_colors[cells[Vector2(x,y)]]) - + if (Vector2(x, y) in cells): + draw_texture_rect(block, Rect2(Vector2(x, y)*bs, bs), false, block_colors[cells[Vector2(x, y)]]) + if (piece_active): - for c in block_shapes[piece_shape]: - draw_texture_rect(block,Rect2(piece_cell_xform(c)*bs,bs),false,block_colors[piece_shape]) - + draw_texture_rect(block, Rect2(piece_cell_xform(c)*bs, bs), false, block_colors[piece_shape]) -func piece_check_fit(ofs,er=0): +func piece_check_fit(ofs, er = 0): for c in block_shapes[piece_shape]: - var pos = piece_cell_xform(c,er)+ofs + var pos = piece_cell_xform(c, er) + ofs if (pos.x < 0): return false if (pos.y < 0): @@ -88,130 +82,114 @@ func piece_check_fit(ofs,er=0): if (pos in cells): return false - return true + return true -func new_piece(): - piece_shape = randi() % MAX_SHAPES - piece_pos = Vector2(width/2,0) - piece_active=true - piece_rot=0 - if (piece_shape==0): - piece_pos.y+=1 - +func new_piece(): + piece_shape = randi() % MAX_SHAPES + piece_pos = Vector2(width/2, 0) + piece_active = true + piece_rot = 0 + if (piece_shape == 0): + piece_pos.y += 1 + if (not piece_check_fit(Vector2())): - #game over - #print("GAME OVER!") + # Game over game_over() - - update() - + update() + + func test_collapse_rows(): - var accum_down=0 + var accum_down = 0 for i in range(height): var y = height - i - 1 var collapse = true for x in range(width): - if (Vector2(x,y) in cells): + if (Vector2(x, y) in cells): if (accum_down): - cells[ Vector2(x,y+accum_down) ] = cells[Vector2(x,y)] + cells[Vector2(x, y + accum_down)] = cells[Vector2(x, y)] else: - collapse=false + collapse = false if (accum_down): - cells.erase( Vector2(x,y+accum_down) ) - - if (collapse): - accum_down+=1 + cells.erase(Vector2(x, y + accum_down)) - - score+=accum_down*100 + if (collapse): + accum_down += 1 + + score += accum_down*100 score_label.set_text(str(score)) - - + + func game_over(): + piece_active = false + get_node("gameover").set_text("Game over!") + update() + - piece_active=false - get_node("gameover").set_text("Game Over") - update() - - func restart_pressed(): + score = 0 + score_label.set_text("0") + cells.clear() + get_node("gameover").set_text("") + piece_active = true + get_node("../restart").release_focus() + update() - score=0 - score_label.set_text("0") - cells.clear() - get_node("gameover").set_text("") - piece_active=true - get_node("../restart").release_focus() - update() - - func piece_move_down(): - if (!piece_active): return - if (piece_check_fit(Vector2(0,1))): - piece_pos.y+=1 - update() + if (piece_check_fit(Vector2(0, 1))): + piece_pos.y += 1 + update() else: - for c in block_shapes[piece_shape]: var pos = piece_cell_xform(c) - cells[pos]=piece_shape + cells[pos] = piece_shape test_collapse_rows() new_piece() - -func piece_rotate(): +func piece_rotate(): var adv = 1 - if (not piece_check_fit(Vector2(),1)): + if (not piece_check_fit(Vector2(), 1)): return piece_rot = (piece_rot + adv) % 4 update() - - - -func _input(ie): +func _input(ie): if (not piece_active): return if (!ie.is_pressed()): return if (ie.is_action("move_left")): - if (piece_check_fit(Vector2(-1,0))): - piece_pos.x-=1 + if (piece_check_fit(Vector2(-1, 0))): + piece_pos.x -= 1 update() elif (ie.is_action("move_right")): - if (piece_check_fit(Vector2(1,0))): - piece_pos.x+=1 + if (piece_check_fit(Vector2(1, 0))): + piece_pos.x += 1 update() elif (ie.is_action("move_down")): piece_move_down() elif (ie.is_action("rotate")): piece_rotate() - - -func setup(w,h): - width=w - height=h - set_size( Vector2(w,h)*block.get_size() ) + + +func setup(w, h): + width = w + height = h + set_size(Vector2(w, h)*block.get_size()) new_piece() get_node("timer").start() - + func _ready(): # Initalization here - - setup(10,20) + setup(10, 20) score_label = get_node("../score") - + set_process_input(true) - - - - diff --git a/demos/2d/texscreen/bubbles.gd b/demos/2d/texscreen/bubbles.gd index 2ee227a928..38877679d8 100644 --- a/demos/2d/texscreen/bubbles.gd +++ b/demos/2d/texscreen/bubbles.gd @@ -1,17 +1,12 @@ extends Control -# member variables here, example: -# var a=2 -# var b="textvar" +# Member variables +const MAX_BUBBLES = 10 -const MAX_BUBBLES=10 func _ready(): # Initialization here for i in range(MAX_BUBBLES): var bubble = preload("res://lens.scn").instance() add_child(bubble) - pass - - diff --git a/demos/2d/texscreen/lens.gd b/demos/2d/texscreen/lens.gd index 2ccbfba497..d1007553b0 100644 --- a/demos/2d/texscreen/lens.gd +++ b/demos/2d/texscreen/lens.gd @@ -1,37 +1,32 @@ extends BackBufferCopy -# member variables here, example: -# var a=2 -# var b="textvar" -const MOTION_SPEED=150 +# Member variables +const MOTION_SPEED = 150 + +var vsize +var dir -var vsize; -var dir; func _process(delta): - var pos = get_pos() + dir * delta * MOTION_SPEED + var pos = get_pos() + dir*delta*MOTION_SPEED + + if (pos.x < 0): + dir.x = abs(dir.x) + elif (pos.x > vsize.x): + dir.x = -abs(dir.x) + + if (pos.y < 0): + dir.y = abs(dir.y) + elif (pos.y > vsize.y): + dir.y = -abs(dir.y) - if (pos.x<0): - dir.x=abs(dir.x) - elif (pos.x>vsize.x): - dir.x=-abs(dir.x) - - if (pos.y<0): - dir.y=abs(dir.y) - elif (pos.y>vsize.y): - dir.y=-abs(dir.y) - set_pos(pos) + func _ready(): vsize = get_viewport_rect().size - var pos = vsize * Vector2(randf(),randf()); - set_pos(pos); - dir = Vector2(randf()*2.0-1,randf()*2.0-1).normalized() + var pos = vsize*Vector2(randf(), randf()) + set_pos(pos) + dir = Vector2(randf()*2.0 - 1, randf()*2.0 - 1).normalized() set_process(true) - - # Initialization here - pass - - |