Improve code formatting

The scripts were streamlined using more or less the following conventions:
 - space after a comma in lists of arguments
 - spaces around weak operators (+, -), no spaces around strong operators (*, /)
 - spaces around comparison operators and compound assignment operators
 - space after a comment start (#)
 - removed trailing spaces or tabs, apart from those that delimit the function indentation level (those could be removed too but since they are added automatically by the editor when typing code, keeping them for now)
 - function blocks separate by two newlines
 - comment sentences start with an upper-case letter
This commit is contained in:
Rémi Verschelde
2015-11-21 16:13:43 +01:00
parent 323dde7f31
commit 7589b2bf60
40 changed files with 851 additions and 1111 deletions

View File

@ -1,16 +1,15 @@
extends Area2D extends Area2D
#virtual from CollisionObject2D (also available as signal)
# Virtual from CollisionObject2D (also available as signal)
func _input_event(viewport, event, shape_idx): func _input_event(viewport, event, shape_idx):
#convert event to local coordinates # Convert event to local coordinates
if (event.type==InputEvent.MOUSE_MOTION): if (event.type == InputEvent.MOUSE_MOTION):
event = make_input_local( event ) event = make_input_local(event)
get_node("label").set_text(str(event.pos)) get_node("label").set_text(str(event.pos))
#virtual from CollisionObject2D (also available as signal)
# Virtual from CollisionObject2D (also available as signal)
func _mouse_exit(): func _mouse_exit():
get_node("label").set_text("") get_node("label").set_text("")

View File

@ -1,21 +1,18 @@
extends RigidBody2D extends RigidBody2D
# member variables here, example: # Member variables
# var a=2 var timeout = 5
# var b="textvar"
var timeout=5
func _process(delta): func _process(delta):
timeout-=delta timeout -= delta
if (timeout<1): if (timeout < 1):
set_opacity(timeout) set_opacity(timeout)
if (timeout<0): if (timeout < 0):
queue_free() queue_free()
func _ready(): func _ready():
set_process(true)
# Initialization here # Initialization here
pass set_process(true)

View File

@ -1,23 +1,20 @@
extends Node2D extends Node2D
# member variables here, example: # Member variables
# var a=2 const EMIT_INTERVAL = 0.1
# var b="textvar" var timeout = EMIT_INTERVAL
const EMIT_INTERVAL=0.1
var timeout=EMIT_INTERVAL
func _process(delta): func _process(delta):
timeout-=delta timeout -= delta
if (timeout<0): if (timeout < 0):
timeout=EMIT_INTERVAL timeout = EMIT_INTERVAL
var ball = preload("res://ball.scn").instance() 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) add_child(ball)
func _ready(): func _ready():
set_process(true)
# Initialization here # Initialization here
pass set_process(true)

View File

@ -1,86 +1,79 @@
extends TileMap extends TileMap
# member variables here, example: # Member variables
# var a=2
# var b="textvar"
# boundarys for the fog rectangle # Boundaries for the fog rectangle
var x_min = -20 # left start tile var x_min = -20 # Left start tile
var x_max = 20 # right end tile var x_max = 20 # Right end tile
var y_min = -20 # top start tile var y_min = -20 # Top start tile
var y_max = 20 # bottom end tile var y_max = 20 # Bottom end tile
var position # players position var position # Player's position
# iteration variables # Iteration variables
var x var x
var y var y
# variable to check if player moved # Variables to check if the player moved
var x_old var x_old
var y_old var y_old
# array to build up the visible area like a square # Array to build up the visible area like a square.
# first value determines the width/height of the tip # First value determines the width/height of the tip.
# here it would be 2*2 + 1 = 5 tiles wide/high # Here it would be 2*2 + 1 = 5 tiles wide/high.
# second value determines the total squares size # Second value determines the total squares size.
# here it would be 5*2 + 1 = 10 tiles wide/high # Here it would be 5*2 + 1 = 10 tiles wide/high.
var l = range(2,5) var l = range(2, 5)
# process that runs in realtime
# Process that runs in realtime
func _fixed_process(delta): func _fixed_process(delta):
position = get_node("../troll").get_pos() position = get_node("../troll").get_pos()
# calculate the corresponding tile # Calculate the corresponding tile
# from the players position # from the players position
x = int(position.x/get_cell_size().x) 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 # causes problems because of rounding problems
if position.x < 0: if position.x < 0:
x -= 1 # correct negative values x -= 1 # Correct negative values
y = int(position.y/get_cell_size().y) y = int(position.y/get_cell_size().y)
if position.y < 0: if (position.y < 0):
y -= 1 y -= 1
# check if the player moved one tile further # Check if the player moved one tile further
if (x_old != x) or (y_old != y): if ((x_old != x) or (y_old != y)):
# Create the transparent part (visited area)
# create the transparent part (visited area) var end = l.size() - 1
var end = l.size()-1
var start = 0 var start = 0
for steps in range(l.size()): for steps in range(l.size()):
for m in range(x-l[end]-1,x+l[end]+2): for m in range(x - l[end] - 1, x + l[end] + 2):
for n in range(y-l[start]-1,y+l[start]+2): for n in range(y - l[start] - 1, y + l[start] + 2):
if get_cell(m,n) != 0: if (get_cell(m, n) != 0):
set_cell(m,n,1,0,0) set_cell(m, n, 1, 0, 0)
end -= 1 end -= 1
start += 1 start += 1
# create the actual and active visible part # Create the actual and active visible part
var end = l.size()-1 var end = l.size() - 1
var start = 0 var start = 0
for steps in range(l.size()): for steps in range(l.size()):
for m in range(x-l[end],x+l[end]+1): for m in range(x - l[end], x + l[end] + 1):
for n in range(y-l[start],y+l[start]+1): for n in range(y - l[start], y + l[start] + 1):
set_cell(m,n,-1) set_cell(m, n, -1)
end -= 1 end -= 1
start += 1 start += 1
x_old = x x_old = x
y_old = y y_old = y
pass
func _ready(): func _ready():
# Initalization here # Initalization here
# Create a square filled with the 100% opaque fog
# create a square filled with the 100% opaque fog for x in range(x_min, x_max):
for x in range(x_min,x_max): for y in range(y_min, y_max):
for y in range(y_min,y_max): set_cell(x, y, 0, 0, 0)
set_cell(x,y,0,0,0)
set_fixed_process(true) set_fixed_process(true)
pass

View File

@ -2,42 +2,38 @@
extends KinematicBody2D extends KinematicBody2D
# This is a simple collision demo showing how # 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 # 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. # 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): func _fixed_process(delta):
var motion = Vector2() var motion = Vector2()
if (Input.is_action_pressed("move_up")): if (Input.is_action_pressed("move_up")):
motion+=Vector2(0,-1) motion += Vector2(0, -1)
if (Input.is_action_pressed("move_bottom")): if (Input.is_action_pressed("move_bottom")):
motion+=Vector2(0,1) motion += Vector2(0, 1)
if (Input.is_action_pressed("move_left")): if (Input.is_action_pressed("move_left")):
motion+=Vector2(-1,0) motion += Vector2(-1, 0)
if (Input.is_action_pressed("move_right")): 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) motion = move(motion)
#make character slide nicely through the world # Make character slide nicely through the world
var slide_attempts = 4 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 = get_collision_normal().slide(motion)
motion=move(motion) motion = move(motion)
slide_attempts-=1 slide_attempts -= 1
func _ready(): func _ready():
# Initalization here # Initalization here
set_fixed_process(true) set_fixed_process(true)
pass

View File

@ -1,26 +1,22 @@
extends Node2D extends Node2D
# member variables here, example: # Member variables
# var a=2 const CAVE_LIMIT = 1000
# var b="textvar"
const CAVE_LIMIT=1000
func _input(ev):
if (ev.type==InputEvent.MOUSE_MOTION and ev.button_mask&1): func _input(event):
var rel_x = ev.relative_x if (event.type == InputEvent.MOUSE_MOTION and event.button_mask&1):
var rel_x = event.relative_x
var cavepos = get_node("cave").get_pos() var cavepos = get_node("cave").get_pos()
cavepos.x+=rel_x cavepos.x += rel_x
if (cavepos.x<-CAVE_LIMIT): if (cavepos.x < -CAVE_LIMIT):
cavepos.x=-CAVE_LIMIT cavepos.x = -CAVE_LIMIT
elif (cavepos.x>0): elif (cavepos.x > 0):
cavepos.x=0 cavepos.x = 0
get_node("cave").set_pos(cavepos) get_node("cave").set_pos(cavepos)
func _ready(): func _ready():
set_process_input(true)
# Initialization here # Initialization here
pass set_process_input(true)

View File

@ -2,42 +2,38 @@
extends KinematicBody2D extends KinematicBody2D
# This is a simple collision demo showing how # 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 # 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. # 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): func _fixed_process(delta):
var motion = Vector2() var motion = Vector2()
if (Input.is_action_pressed("move_up")): if (Input.is_action_pressed("move_up")):
motion+=Vector2(0,-1) motion += Vector2(0, -1)
if (Input.is_action_pressed("move_bottom")): if (Input.is_action_pressed("move_bottom")):
motion+=Vector2(0,1) motion += Vector2(0, 1)
if (Input.is_action_pressed("move_left")): if (Input.is_action_pressed("move_left")):
motion+=Vector2(-1,0) motion += Vector2(-1, 0)
if (Input.is_action_pressed("move_right")): 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) motion = move(motion)
#make character slide nicely through the world # Make character slide nicely through the world
var slide_attempts = 4 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 = get_collision_normal().slide(motion)
motion=move(motion) motion = move(motion)
slide_attempts-=1 slide_attempts -= 1
func _ready(): func _ready():
# Initalization here # Initalization here
set_fixed_process(true) set_fixed_process(true)
pass

View File

@ -2,42 +2,38 @@
extends KinematicBody2D extends KinematicBody2D
# This is a simple collision demo showing how # 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 # 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. # 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): func _fixed_process(delta):
var motion = Vector2() var motion = Vector2()
if (Input.is_action_pressed("move_up")): if (Input.is_action_pressed("move_up")):
motion+=Vector2(0,-1) motion += Vector2(0, -1)
if (Input.is_action_pressed("move_bottom")): if (Input.is_action_pressed("move_bottom")):
motion+=Vector2(0,1) motion += Vector2(0, 1)
if (Input.is_action_pressed("move_left")): if (Input.is_action_pressed("move_left")):
motion+=Vector2(-1,0) motion += Vector2(-1, 0)
if (Input.is_action_pressed("move_right")): 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) motion = move(motion)
#make character slide nicely through the world # Make character slide nicely through the world
var slide_attempts = 4 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 = get_collision_normal().slide(motion)
motion=move(motion) motion = move(motion)
slide_attempts-=1 slide_attempts -= 1
func _ready(): func _ready():
# Initalization here # Initalization here
set_fixed_process(true) set_fixed_process(true)
pass

View File

@ -1,96 +1,86 @@
extends KinematicBody2D extends KinematicBody2D
# member variables here, example: # Member variables
# var a=2
# var b="textvar"
const MAX_SPEED = 300.0 const MAX_SPEED = 300.0
const IDLE_SPEED = 10.0 const IDLE_SPEED = 10.0
const ACCEL=5.0 const ACCEL = 5.0
const VSCALE=0.5 const VSCALE = 0.5
const SHOOT_INTERVAL=0.3 const SHOOT_INTERVAL = 0.3
var speed=Vector2() var speed = Vector2()
var current_anim="" var current_anim = ""
var current_mirror=false 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): func _input(event):
var pos = get_canvas_transform().affine_inverse() * ev.pos if (event.type == InputEvent.MOUSE_BUTTON and event.button_index == 1 and event.pressed and shoot_countdown <= 0):
var dir = (pos-get_global_pos()).normalized() var pos = get_canvas_transform().affine_inverse()*event.pos
var dir = (pos - get_global_pos()).normalized()
var bullet = preload("res://shoot.scn").instance() var bullet = preload("res://shoot.scn").instance()
bullet.advance_dir=dir bullet.advance_dir = dir
bullet.set_pos( get_global_pos() + dir * 60 ) bullet.set_pos(get_global_pos() + dir*60)
get_parent().add_child(bullet) get_parent().add_child(bullet)
shoot_countdown=SHOOT_INTERVAL shoot_countdown = SHOOT_INTERVAL
func _fixed_process(delta): func _fixed_process(delta):
shoot_countdown -= delta
shoot_countdown-=delta
var dir = Vector2() var dir = Vector2()
if (Input.is_action_pressed("up")): if (Input.is_action_pressed("up")):
dir+=Vector2(0,-1) dir += Vector2(0, -1)
if (Input.is_action_pressed("down")): if (Input.is_action_pressed("down")):
dir+=Vector2(0,1) dir += Vector2(0, 1)
if (Input.is_action_pressed("left")): if (Input.is_action_pressed("left")):
dir+=Vector2(-1,0) dir += Vector2(-1, 0)
if (Input.is_action_pressed("right")): if (Input.is_action_pressed("right")):
dir+=Vector2(1,0) dir += Vector2(1, 0)
if (dir!=Vector2()): if (dir != Vector2()):
dir=dir.normalized() dir = dir.normalized()
speed = speed.linear_interpolate(dir*MAX_SPEED,delta*ACCEL) speed = speed.linear_interpolate(dir*MAX_SPEED, delta*ACCEL)
var motion = speed * delta var motion = speed*delta
motion.y*=VSCALE motion.y *= VSCALE
motion=move(motion) motion = move(motion)
if (is_colliding()): if (is_colliding()):
var n = get_collision_normal() var n = get_collision_normal()
motion=n.slide(motion) motion = n.slide(motion)
move(motion) move(motion)
var next_anim="" var next_anim = ""
var next_mirror=false var next_mirror = false
if (dir==Vector2() and speed.length()<IDLE_SPEED): if (dir == Vector2() and speed.length() < IDLE_SPEED):
next_anim="idle" next_anim = "idle"
next_mirror=false next_mirror = false
elif (speed.length()>IDLE_SPEED*0.1): elif (speed.length() > IDLE_SPEED*0.1):
var angle = atan2(abs(speed.x),speed.y) var angle = atan2(abs(speed.x), speed.y)
next_mirror = speed.x>0 next_mirror = speed.x > 0
if (angle<PI/8): if (angle < PI/8):
next_anim="bottom" next_anim = "bottom"
next_mirror=false next_mirror = false
elif (angle<PI/4+PI/8): elif (angle < PI/4 + PI/8):
next_anim="bottom_left" next_anim = "bottom_left"
elif (angle<PI*2/4+PI/8): elif (angle < PI*2/4 + PI/8):
next_anim="left" next_anim = "left"
elif (angle<PI*3/4+PI/8): elif (angle < PI*3/4 + PI/8):
next_anim="top_left" next_anim = "top_left"
else: else:
next_anim="top" next_anim = "top"
next_mirror=false next_mirror = false
if (next_anim != current_anim or next_mirror != current_mirror):
if (next_anim!=current_anim or next_mirror!=current_mirror):
get_node("frames").set_flip_h(next_mirror) get_node("frames").set_flip_h(next_mirror)
get_node("anim").play(next_anim) get_node("anim").play(next_anim)
current_anim=next_anim current_anim = next_anim
current_mirror=next_mirror current_mirror = next_mirror
func _ready(): func _ready():
# Initialization here # Initialization here
set_fixed_process(true) set_fixed_process(true)
set_process_input(true) set_process_input(true)
pass

View File

@ -1,18 +1,7 @@
extends Node2D extends Node2D
# member variables here, example:
# var a=2
# var b="textvar"
func _ready(): func _on_prince_area_body_enter(body):
# Initialization here if (body.get_name() == "cubio"):
pass
func _on_prince_area_body_enter( body ):
if (body.get_name()=="cubio"):
get_node("message").show() get_node("message").show()
pass # replace with function body

View File

@ -1,27 +1,22 @@
extends KinematicBody2D extends KinematicBody2D
# member variables here, example: # Member variables
# var a=2
# var b="textvar"
var advance_dir=Vector2(1,0)
const ADVANCE_SPEED = 500.0 const ADVANCE_SPEED = 500.0
var hit=false var advance_dir = Vector2(1, 0)
var hit = false
func _fixed_process(delta): func _fixed_process(delta):
if (hit): if (hit):
return return
move(advance_dir*delta*ADVANCE_SPEED) move(advance_dir*delta*ADVANCE_SPEED)
if (is_colliding()): if (is_colliding()):
get_node("anim").play("explode") get_node("anim").play("explode")
hit=true hit = true
func _ready(): func _ready():
# Initialization here # Initialization here
set_fixed_process(true) set_fixed_process(true)
pass

View File

@ -1,18 +1,8 @@
extends Node2D extends Node2D
#member variables here, example:
#var a=2
#var b="textvar"
func _ready(): func _on_princess_body_enter(body):
#Initalization here # The name of this editor-generated callback is unfortunate
pass 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() get_node("youwin").show()

View File

@ -1,6 +1,6 @@
[application] [application]
name="Kinematic Collision" name="Kinematic Character"
main_scene="res://colworld.scn" main_scene="res://colworld.scn"
icon="res://icon.png" icon="res://icon.png"

View File

@ -1,137 +1,124 @@
extends KinematicBody2D extends KinematicBody2D
#This is a simple collision demo showing how # 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 # 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. # as long as it starts from a non-colliding spot too.
# Member variables
const GRAVITY = 500.0 # Pixels/second
#pixels / second # Angle in degrees towards either side that the player can consider "floor"
const GRAVITY = 500.0
#Angle in degrees towards either side that the player can
#consider "floor".
const FLOOR_ANGLE_TOLERANCE = 40 const FLOOR_ANGLE_TOLERANCE = 40
const WALK_FORCE = 600 const WALK_FORCE = 600
const WALK_MIN_SPEED=10 const WALK_MIN_SPEED = 10
const WALK_MAX_SPEED = 200 const WALK_MAX_SPEED = 200
const STOP_FORCE = 1300 const STOP_FORCE = 1300
const JUMP_SPEED = 200 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_VELOCITY = 1.0 # One pixel per second
const SLIDE_STOP_MIN_TRAVEL=1.0 #one pixel const SLIDE_STOP_MIN_TRAVEL = 1.0 # One pixel
var velocity = Vector2() var velocity = Vector2()
var on_air_time=100 var on_air_time = 100
var jumping=false var jumping = false
var prev_jump_pressed = false
var prev_jump_pressed=false
func _fixed_process(delta): func _fixed_process(delta):
# Create forces
#create forces var force = Vector2(0, GRAVITY)
var force = Vector2(0,GRAVITY)
var walk_left = Input.is_action_pressed("move_left") var walk_left = Input.is_action_pressed("move_left")
var walk_right = Input.is_action_pressed("move_right") var walk_right = Input.is_action_pressed("move_right")
var jump = Input.is_action_pressed("jump") var jump = Input.is_action_pressed("jump")
var stop=true var stop = true
if (walk_left): if (walk_left):
if (velocity.x<=WALK_MIN_SPEED and velocity.x > -WALK_MAX_SPEED): if (velocity.x <= WALK_MIN_SPEED and velocity.x > -WALK_MAX_SPEED):
force.x-=WALK_FORCE force.x -= WALK_FORCE
stop=false stop = false
elif (walk_right): elif (walk_right):
if (velocity.x>=-WALK_MIN_SPEED and velocity.x < WALK_MAX_SPEED): if (velocity.x >= -WALK_MIN_SPEED and velocity.x < WALK_MAX_SPEED):
force.x+=WALK_FORCE force.x += WALK_FORCE
stop=false stop = false
if (stop): if (stop):
var vsign = sign(velocity.x) var vsign = sign(velocity.x)
var vlen = abs(velocity.x) var vlen = abs(velocity.x)
vlen -= STOP_FORCE * delta vlen -= STOP_FORCE*delta
if (vlen<0): if (vlen < 0):
vlen=0 vlen = 0
velocity.x=vlen*vsign velocity.x = vlen*vsign
# Integrate forces to velocity
velocity += force*delta
# Integrate velocity into motion and move
var motion = velocity*delta
#integrate forces to velocity # Move and consume motion
velocity += force * delta
#integrate velocity into motion and move
var motion = velocity * delta
#move and consume motion
motion = move(motion) motion = move(motion)
var floor_velocity = Vector2()
var floor_velocity=Vector2()
if (is_colliding()): 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()) # 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() var n = get_collision_normal()
if ( rad2deg(acos(n.dot( Vector2(0,-1)))) < FLOOR_ANGLE_TOLERANCE ): if (rad2deg(acos(n.dot(Vector2(0, -1)))) < FLOOR_ANGLE_TOLERANCE):
#if angle to the "up" vectors is < angle tolerance # If angle to the "up" vectors is < angle tolerance
#char is on floor # char is on floor
on_air_time=0 on_air_time = 0
floor_velocity=get_collider_velocity() 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()):
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,
#Since this formula will always slide the character around, # a special case must be considered to to stop it from moving
#a special case must be considered to to stop it from moving # if standing on an inclined floor. Conditions are:
#if standing on an inclined floor. Conditions are: # 1) Standing on floor (on_air_time == 0)
# 1) Standing on floor (on_air_time==0)
# 2) Did not move more than one pixel (get_travel().length() < SLIDE_STOP_MIN_TRAVEL) # 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) # 3) Not moving horizontally (abs(velocity.x) < SLIDE_STOP_VELOCITY)
# 4) Collider is not moving # 4) Collider is not moving
revert_motion() revert_motion()
velocity.y=0.0 velocity.y = 0.0
else: else:
#For every other case of motion,our motion was interrupted. # For every other case of motion, our motion was interrupted.
#Try to complete the motion by "sliding" # Try to complete the motion by "sliding" by the normal
#by the normal
motion = n.slide(motion) motion = n.slide(motion)
velocity = n.slide(velocity) velocity = n.slide(velocity)
#then move again # Then move again
move(motion) move(motion)
if (floor_velocity!=Vector2()): if (floor_velocity != Vector2()):
#if floor moves, move with floor # If floor moves, move with floor
move(floor_velocity*delta) move(floor_velocity*delta)
if (jumping and velocity.y>0): if (jumping and velocity.y > 0):
#if falling, no longer jumping # If falling, no longer jumping
jumping=false jumping = false
if (on_air_time<JUMP_MAX_AIRBORNE_TIME and jump and not prev_jump_pressed and not jumping): 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 # Jump must also be allowed to happen if the character left the floor a little bit ago.
# character left the floor a little bit ago.
# Makes controls more snappy. # Makes controls more snappy.
velocity.y=-JUMP_SPEED velocity.y = -JUMP_SPEED
jumping=true jumping = true
on_air_time += delta
prev_jump_pressed = jump
on_air_time+=delta
prev_jump_pressed=jump
func _ready(): func _ready():
#Initalization here #Initalization here
set_fixed_process(true) set_fixed_process(true)
pass

View File

@ -2,35 +2,31 @@
extends KinematicBody2D extends KinematicBody2D
# This is a simple collision demo showing how # 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 # 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. # 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): func _fixed_process(delta):
var motion = Vector2() var motion = Vector2()
if (Input.is_action_pressed("move_up")): if (Input.is_action_pressed("move_up")):
motion+=Vector2(0,-1) motion += Vector2(0, -1)
if (Input.is_action_pressed("move_bottom")): if (Input.is_action_pressed("move_bottom")):
motion+=Vector2(0,1) motion += Vector2(0, 1)
if (Input.is_action_pressed("move_left")): if (Input.is_action_pressed("move_left")):
motion+=Vector2(-1,0) motion += Vector2(-1, 0)
if (Input.is_action_pressed("move_right")): 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) move(motion)
func _ready(): func _ready():
# Initalization here # Initalization here
set_fixed_process(true) set_fixed_process(true)
pass

View File

@ -1,43 +1,34 @@
extends Sprite extends Sprite
# member variables here, example: # Member variables
# var a=2 const MODE_DIRECT = 0
# var b="textvar" const MODE_CONSTANT = 1
const MODE_SMOOTH = 2
const MODE_DIRECT=0
const MODE_CONSTANT=1
const MODE_SMOOTH=2
const ROTATION_SPEED = 1 const ROTATION_SPEED = 1
const SMOOTH_SPEED = 2.0 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): func _process(delta):
var mpos = get_viewport().get_mouse_pos() var mpos = get_viewport().get_mouse_pos()
if (mode==MODE_DIRECT): if (mode == MODE_DIRECT):
look_at(mpos) look_at(mpos)
elif (mode == MODE_CONSTANT):
elif (mode==MODE_CONSTANT):
var ang = get_angle_to(mpos) var ang = get_angle_to(mpos)
var s = sign(ang) var s = sign(ang)
ang=abs(ang) ang = abs(ang)
rotate( min(ang,ROTATION_SPEED*delta)*s )
elif (mode==MODE_SMOOTH):
rotate(min(ang, ROTATION_SPEED*delta)*s)
elif (mode == MODE_SMOOTH):
var ang = get_angle_to(mpos) var ang = get_angle_to(mpos)
rotate( ang*delta*SMOOTH_SPEED ) rotate(ang*delta*SMOOTH_SPEED)
func _ready(): func _ready():
# Initialization here # Initialization here
set_process(true) set_process(true)
pass

View File

@ -1,29 +1,28 @@
extends Sprite extends Sprite
# Member variables
export var use_idle=true
# member variables here, example:
# var a=2
# var b="textvar"
const BEGIN = -113 const BEGIN = -113
const END = 907 const END = 907
const TIME = 5.0 # seconds const TIME = 5.0 # Seconds
const SPEED = (END-BEGIN)/TIME const SPEED = (END - BEGIN)/TIME
export var use_idle = true
func _process(delta): func _process(delta):
var ofs = get_pos() var ofs = get_pos()
ofs.x+=delta*SPEED ofs.x += delta*SPEED
if (ofs.x>END): if (ofs.x > END):
ofs.x=BEGIN ofs.x = BEGIN
set_pos(ofs) set_pos(ofs)
func _fixed_process(delta): func _fixed_process(delta):
var ofs = get_pos() var ofs = get_pos()
ofs.x+=delta*SPEED ofs.x += delta*SPEED
if (ofs.x>END): if (ofs.x > END):
ofs.x=BEGIN ofs.x = BEGIN
set_pos(ofs) set_pos(ofs)
@ -33,6 +32,3 @@ func _ready():
set_process(true) set_process(true)
else: else:
set_fixed_process(true) set_fixed_process(true)
pass

View File

@ -1,63 +1,54 @@
extends Navigation2D extends Navigation2D
# member variables here, example: # Member variables
# var a=2 const SPEED = 200.0
# var b="textvar"
var begin=Vector2() var begin = Vector2()
var end=Vector2() var end = Vector2()
var path=[] var path = []
const SPEED=200.0
func _process(delta): func _process(delta):
if (path.size() > 1):
if (path.size()>1):
var to_walk = delta*SPEED var to_walk = delta*SPEED
while(to_walk>0 and path.size()>=2): while(to_walk > 0 and path.size() >= 2):
var pfrom = path[path.size()-1] var pfrom = path[path.size() - 1]
var pto = path[path.size()-2] var pto = path[path.size() - 2]
var d = pfrom.distance_to(pto) var d = pfrom.distance_to(pto)
if (d<=to_walk): if (d <= to_walk):
path.remove(path.size()-1) path.remove(path.size() - 1)
to_walk-=d to_walk -= d
else: else:
path[path.size()-1] = pfrom.linear_interpolate(pto,to_walk/d) path[path.size() - 1] = pfrom.linear_interpolate(pto, to_walk/d)
to_walk=0 to_walk = 0
var atpos = path[path.size()-1] var atpos = path[path.size() - 1]
get_node("agent").set_pos(atpos) get_node("agent").set_pos(atpos)
if (path.size()<2): if (path.size() < 2):
path=[] path = []
set_process(false) set_process(false)
else: else:
set_process(false) set_process(false)
func _update_path(): func _update_path():
var p = get_simple_path(begin, end, true)
var p = get_simple_path(begin,end,true) path = Array(p) # Vector2array too complex to use, convert to regular array
path=Array(p) # Vector2array to complex to use, convert to regular array
path.invert() path.invert()
set_process(true) set_process(true)
func _input(ev): func _input(event):
if (ev.type==InputEvent.MOUSE_BUTTON and ev.pressed and ev.button_index==1): if (event.type == InputEvent.MOUSE_BUTTON and event.pressed and event.button_index == 1):
begin=get_node("agent").get_pos() begin = get_node("agent").get_pos()
#mouse to local navigatio cooards # Mouse to local navigation coordinates
end=ev.pos - get_pos() end = event.pos - get_pos()
_update_path() _update_path()
func _ready(): func _ready():
# Initialization here # Initialization here
set_process_input(true) set_process_input(true)
pass

View File

@ -1,21 +1,17 @@
extends RigidBody2D extends RigidBody2D
# member variables here, example: # Member variables
# var a=2 var disabled = false
# var b="textvar"
var disabled=false
func disable(): func disable():
if (disabled): if (disabled):
return return
get_node("anim").play("shutdown") get_node("anim").play("shutdown")
disabled=true disabled = true
func _ready(): func _ready():
# Initalization here # Initalization here
get_node("Timer").start() get_node("Timer").start()
pass

View File

@ -1,28 +1,19 @@
extends Area2D extends Area2D
# member variables here, example: # Member variables
# var a=2 var taken = false
# var b="textvar"
var taken=false
func _on_body_enter( body ): func _on_body_enter( body ):
if (not taken and body extends preload("res://player.gd")): if (not taken and body extends preload("res://player.gd")):
get_node("anim").play("taken") get_node("anim").play("taken")
taken=true taken = true
func _ready(): func _on_coin_area_enter(area):
# Initalization here
pass
func _on_coin_area_enter( area ):
pass # replace with function body 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 pass # replace with function body

View File

@ -1,89 +1,78 @@
extends RigidBody2D extends RigidBody2D
# member variables here, example: # Member variables
# var a=2
# var b="textvar"
const STATE_WALKING = 0 const STATE_WALKING = 0
const STATE_DYING = 1 const STATE_DYING = 1
var state = STATE_WALKING var state = STATE_WALKING
var direction = -1 var direction = -1
var anim="" var anim = ""
var rc_left=null var rc_left = null
var rc_right=null var rc_right = null
var WALK_SPEED = 50 var WALK_SPEED = 50
var bullet_class = preload("res://bullet.gd") var bullet_class = preload("res://bullet.gd")
func _die(): func _die():
queue_free() queue_free()
func _pre_explode(): func _pre_explode():
#stay there # Stay there
clear_shapes() clear_shapes()
set_mode(MODE_STATIC) set_mode(MODE_STATIC)
get_node("sound").play("explode") get_node("sound").play("explode")
func _integrate_forces(s): func _integrate_forces(s):
var lv = s.get_linear_velocity() var lv = s.get_linear_velocity()
var new_anim=anim var new_anim = anim
if (state==STATE_DYING): if (state == STATE_DYING):
new_anim="explode" new_anim = "explode"
elif (state==STATE_WALKING): elif (state == STATE_WALKING):
new_anim = "walk"
new_anim="walk" var wall_side = 0.0
var wall_side=0.0
for i in range(s.get_contact_count()): for i in range(s.get_contact_count()):
var cc = s.get_contact_collider_object(i) var cc = s.get_contact_collider_object(i)
var dp = s.get_contact_local_normal(i) var dp = s.get_contact_local_normal(i)
if (cc): if (cc):
if (cc extends bullet_class and not cc.disabled): if (cc extends bullet_class and not cc.disabled):
set_mode(MODE_RIGID) set_mode(MODE_RIGID)
state=STATE_DYING state = STATE_DYING
#lv=s.get_contact_local_normal(i)*400 #lv = s.get_contact_local_normal(i)*400
s.set_angular_velocity(sign(dp.x)*33.0) s.set_angular_velocity(sign(dp.x)*33.0)
set_friction(1) set_friction(1)
cc.disable() cc.disable()
get_node("sound").play("hit") get_node("sound").play("hit")
break break
if (dp.x > 0.9):
wall_side = 1.0
elif (dp.x < -0.9):
wall_side = -1.0
if (dp.x>0.9): if (wall_side != 0 and wall_side != direction):
wall_side=1.0 direction = -direction
elif (dp.x<-0.9): get_node("sprite").set_scale(Vector2(-direction, 1))
wall_side=-1.0 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))
if (wall_side!=0 and wall_side!=direction): lv.x = direction*WALK_SPEED
direction=-direction if(anim != new_anim):
get_node("sprite").set_scale( Vector2(-direction,1) ) anim = new_anim
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) get_node("anim").play(anim)
s.set_linear_velocity(lv) s.set_linear_velocity(lv)
@ -91,8 +80,5 @@ func _integrate_forces(s):
func _ready(): func _ready():
# Initalization here # Initalization here
rc_left=get_node("raycast_left") rc_left = get_node("raycast_left")
rc_right=get_node("raycast_right") rc_right = get_node("raycast_right")

View File

@ -1,27 +1,21 @@
extends Node2D extends Node2D
# member variables here, example: # Member variables
# var a=2
# var b="textvar"
export var motion = Vector2() export var motion = Vector2()
export var cycle = 1.0 export var cycle = 1.0
var accum=0.0 var accum = 0.0
func _fixed_process(delta): func _fixed_process(delta):
accum += delta*(1.0/cycle)*PI*2.0
accum += delta * (1.0/cycle) * PI * 2.0 accum = fmod(accum, PI*2.0)
accum = fmod(accum,PI*2.0)
var d = sin(accum) var d = sin(accum)
var xf = Matrix32() var xf = Matrix32()
xf[2]= motion * d xf[2]= motion*d
get_node("platform").set_transform(xf) get_node("platform").set_transform(xf)
func _ready(): func _ready():
# Initalization here # Initalization here
set_fixed_process(true) set_fixed_process(true)
pass

View File

@ -1,3 +1,4 @@
extends RigidBody2D extends RigidBody2D
# Character Demo, written by Juan Linietsky. # Character Demo, written by Juan Linietsky.
@ -24,42 +25,40 @@ extends RigidBody2D
# -Friction cant be used, so floor velocity must be considered # -Friction cant be used, so floor velocity must be considered
# for moving platforms. # for moving platforms.
var anim="" # Member variables
var siding_left=false var anim = ""
var jumping=false var siding_left = false
var stopping_jump=false var jumping = false
var shooting=false var stopping_jump = false
var shooting = false
var WALK_ACCEL = 800.0 var WALK_ACCEL = 800.0
var WALK_DEACCEL= 800.0 var WALK_DEACCEL = 800.0
var WALK_MAX_VELOCITY= 200.0 var WALK_MAX_VELOCITY = 200.0
var AIR_ACCEL = 200.0 var AIR_ACCEL = 200.0
var AIR_DEACCEL= 200.0 var AIR_DEACCEL = 200.0
var JUMP_VELOCITY=460 var JUMP_VELOCITY = 460
var STOP_JUMP_FORCE=900.0 var STOP_JUMP_FORCE = 900.0
var MAX_FLOOR_AIRBORNE_TIME = 0.15 var MAX_FLOOR_AIRBORNE_TIME = 0.15
var airborne_time=1e20 var airborne_time = 1e20
var shoot_time=1e20 var shoot_time = 1e20
var MAX_SHOOT_POSE_TIME = 0.3 var MAX_SHOOT_POSE_TIME = 0.3
var bullet = preload("res://bullet.xml") var bullet = preload("res://bullet.xml")
var floor_h_velocity=0.0 var floor_h_velocity = 0.0
var enemy var enemy
func _integrate_forces(s): func _integrate_forces(s):
var lv = s.get_linear_velocity() var lv = s.get_linear_velocity()
var step = s.get_step() var step = s.get_step()
var new_anim=anim var new_anim = anim
var new_siding_left=siding_left var new_siding_left = siding_left
# Get the controls # Get the controls
var move_left = Input.is_action_pressed("move_left") var move_left = Input.is_action_pressed("move_left")
@ -75,187 +74,160 @@ func _integrate_forces(s):
e.set_pos(p) e.set_pos(p)
get_parent().add_child(e) get_parent().add_child(e)
# Deapply prev floor velocity
#deapply prev floor velocity lv.x -= floor_h_velocity
lv.x-=floor_h_velocity floor_h_velocity = 0.0
floor_h_velocity=0.0
# Find the floor (a contact with upwards facing collision normal) # Find the floor (a contact with upwards facing collision normal)
var found_floor=false var found_floor = false
var floor_index=-1 var floor_index = -1
for x in range(s.get_contact_count()): for x in range(s.get_contact_count()):
var ci = s.get_contact_local_normal(x) var ci = s.get_contact_local_normal(x)
if (ci.dot(Vector2(0,-1))>0.6): if (ci.dot(Vector2(0, -1)) > 0.6):
found_floor=true found_floor = true
floor_index=x floor_index = x
# A good idea when impementing characters of all kinds, # A good idea when impementing characters of all kinds,
# Compensates for physics imprecission, as well as human # compensates for physics imprecission, as well as human reaction delay.
# reaction delay.
if (shoot and not shooting): if (shoot and not shooting):
shoot_time=0 shoot_time = 0
var bi = bullet.instance() var bi = bullet.instance()
var ss var ss
if (siding_left): if (siding_left):
ss=-1.0 ss = -1.0
else: else:
ss=1.0 ss = 1.0
var pos = get_pos() + get_node("bullet_shoot").get_pos()*Vector2(ss,1.0) var pos = get_pos() + get_node("bullet_shoot").get_pos()*Vector2(ss, 1.0)
bi.set_pos(pos) bi.set_pos(pos)
get_parent().add_child(bi) get_parent().add_child(bi)
bi.set_linear_velocity( Vector2(800.0*ss,-80) ) bi.set_linear_velocity(Vector2(800.0*ss, -80))
get_node("sprite/smoke").set_emitting(true) get_node("sprite/smoke").set_emitting(true)
get_node("sound").play("shoot") 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: else:
shoot_time+=step shoot_time += step
if (found_floor): if (found_floor):
airborne_time=0.0 airborne_time = 0.0
else: 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 var on_floor = airborne_time < MAX_FLOOR_AIRBORNE_TIME
# Process jump # Process jump
if (jumping): if (jumping):
if (lv.y>0): if (lv.y > 0):
#set off the jumping flag if going down # Set off the jumping flag if going down
jumping=false jumping = false
elif (not jump): elif (not jump):
stopping_jump=true stopping_jump = true
if (stopping_jump): if (stopping_jump):
lv.y+=STOP_JUMP_FORCE*step lv.y += STOP_JUMP_FORCE*step
if (on_floor): if (on_floor):
# Process logic when character is on floor # Process logic when character is on floor
if (move_left and not move_right): if (move_left and not move_right):
if (lv.x > -WALK_MAX_VELOCITY): if (lv.x > -WALK_MAX_VELOCITY):
lv.x-=WALK_ACCEL*step lv.x -= WALK_ACCEL*step
elif (move_right and not move_left): elif (move_right and not move_left):
if (lv.x < WALK_MAX_VELOCITY): if (lv.x < WALK_MAX_VELOCITY):
lv.x+=WALK_ACCEL*step lv.x += WALK_ACCEL*step
else: else:
var xv = abs(lv.x) var xv = abs(lv.x)
xv-=WALK_DEACCEL*step xv -= WALK_DEACCEL*step
if (xv<0): if (xv < 0):
xv=0 xv = 0
lv.x=sign(lv.x)*xv lv.x = sign(lv.x)*xv
#Check jump # Check jump
if (not jumping and jump): if (not jumping and jump):
lv.y=-JUMP_VELOCITY lv.y = -JUMP_VELOCITY
jumping=true jumping = true
stopping_jump=false stopping_jump = false
get_node("sound").play("jump") get_node("sound").play("jump")
#check siding # Check siding
if (lv.x < 0 and move_left): if (lv.x < 0 and move_left):
new_siding_left=true new_siding_left = true
elif (lv.x > 0 and move_right): elif (lv.x > 0 and move_right):
new_siding_left=false new_siding_left = false
if (jumping): if (jumping):
new_anim="jumping" new_anim = "jumping"
elif (abs(lv.x)<0.1): elif (abs(lv.x) < 0.1):
if (shoot_time<MAX_SHOOT_POSE_TIME): if (shoot_time < MAX_SHOOT_POSE_TIME):
new_anim="idle_weapon" new_anim = "idle_weapon"
else: else:
new_anim="idle" new_anim = "idle"
else: else:
if (shoot_time<MAX_SHOOT_POSE_TIME): if (shoot_time < MAX_SHOOT_POSE_TIME):
new_anim="run_weapon" new_anim = "run_weapon"
else: else:
new_anim="run" new_anim = "run"
else: else:
# Process logic when the character is in the air # Process logic when the character is in the air
if (move_left and not move_right): if (move_left and not move_right):
if (lv.x > -WALK_MAX_VELOCITY): if (lv.x > -WALK_MAX_VELOCITY):
lv.x-=AIR_ACCEL*step lv.x -= AIR_ACCEL*step
elif (move_right and not move_left): elif (move_right and not move_left):
if (lv.x < WALK_MAX_VELOCITY): if (lv.x < WALK_MAX_VELOCITY):
lv.x+=AIR_ACCEL*step lv.x += AIR_ACCEL*step
else: else:
var xv = abs(lv.x) var xv = abs(lv.x)
xv-=AIR_DEACCEL*step xv -= AIR_DEACCEL*step
if (xv<0): if (xv < 0):
xv=0 xv = 0
lv.x=sign(lv.x)*xv lv.x = sign(lv.x)*xv
if (lv.y<0): if (lv.y < 0):
if (shoot_time<MAX_SHOOT_POSE_TIME): if (shoot_time < MAX_SHOOT_POSE_TIME):
new_anim="jumping_weapon" new_anim = "jumping_weapon"
else: else:
new_anim="jumping" new_anim = "jumping"
else: else:
if (shoot_time<MAX_SHOOT_POSE_TIME): if (shoot_time < MAX_SHOOT_POSE_TIME):
new_anim="falling_weapon" new_anim = "falling_weapon"
else: else:
new_anim="falling" new_anim = "falling"
# Update siding
#Update siding if (new_siding_left != siding_left):
if (new_siding_left!=siding_left):
if (new_siding_left): if (new_siding_left):
get_node("sprite").set_scale( Vector2(-1,1) ) get_node("sprite").set_scale(Vector2(-1, 1))
else: else:
get_node("sprite").set_scale( Vector2(1,1) ) get_node("sprite").set_scale(Vector2(1, 1))
siding_left=new_siding_left siding_left = new_siding_left
#Change animation # Change animation
if (new_anim!=anim): if (new_anim != anim):
anim=new_anim anim = new_anim
get_node("anim").play(anim) get_node("anim").play(anim)
shooting=shoot shooting = shoot
# Apply floor velocity # Apply floor velocity
if (found_floor): if (found_floor):
floor_h_velocity=s.get_contact_collider_velocity_at_pos(floor_index).x floor_h_velocity = s.get_contact_collider_velocity_at_pos(floor_index).x
lv.x+=floor_h_velocity lv.x += floor_h_velocity
# Finally, apply gravity and set back the linear velocity
lv += s.get_total_gravity()*step
#Finally, apply gravity and set back the linear velocity
lv+=s.get_total_gravity()*step
s.set_linear_velocity(lv) s.set_linear_velocity(lv)
func _ready(): func _ready():
# Initalization here # Initalization here
enemy = ResourceLoader.load("res://enemy.xml")
# if !Globals.has_singleton("Facebook"): # if !Globals.has_singleton("Facebook"):
# return # return
# var Facebook = Globals.get_singleton("Facebook") # var Facebook = Globals.get_singleton("Facebook")
# var link = Globals.get("facebook/link") # var link = Globals.get("facebook/link")
# var icon = Globals.get("facebook/icon") # var icon = Globals.get("facebook/icon")
# var msg = "I just sneezed on your wall! Beat my score and Stop the Running nose!" # var msg = "I just sneezed on your wall! Beat my score and Stop the Running nose!"
# var title = "I just sneezed on your wall!" # var title = "I just sneezed on your wall!"
# Facebook.post("feed", msg, title, link, icon) # Facebook.post("feed", msg, title, link, icon)
enemy = ResourceLoader.load("res://enemy.xml")
pass

View File

@ -1,36 +1,37 @@
extends Spatial extends Spatial
func _ready(): func _ready():
var pf = PolygonPathFinder.new() var pf = PolygonPathFinder.new()
var points = Vector2Array() var points = Vector2Array()
var connections = IntArray() var connections = IntArray()
# poly 1 # Poly 1
points.push_back(Vector2(0, 0)) #0 points.push_back(Vector2(0, 0)) # 0
points.push_back(Vector2(10, 0)) #1 points.push_back(Vector2(10, 0)) # 1
points.push_back(Vector2(10, 10)) #2 points.push_back(Vector2(10, 10)) # 2
points.push_back(Vector2(0, 10)) #3 points.push_back(Vector2(0, 10)) # 3
connections.push_back(0) # connect vertex 0 ... connections.push_back(0) # Connect vertex 0...
connections.push_back(1) # ... to 1 connections.push_back(1) # ... to 1
drawLine(points[0], points[1], get_node("/root/Spatial/Polys")) 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 connections.push_back(2) # ... to 2
drawLine(points[1], points[2], get_node("/root/Spatial/Polys")) drawLine(points[1], points[2], get_node("/root/Spatial/Polys"))
connections.push_back(2) # etc. connections.push_back(2) # Etc.
connections.push_back(3) connections.push_back(3)
drawLine(points[2], points[3], get_node("/root/Spatial/Polys")) drawLine(points[2], points[3], get_node("/root/Spatial/Polys"))
connections.push_back(3) # connect vertex 3 ... connections.push_back(3) # Connect vertex 3...
connections.push_back(0) # back to vertex 0, to close the polygon connections.push_back(0) # ... back to vertex 0, to close the polygon
drawLine(points[3], points[0], get_node("/root/Spatial/Polys")) drawLine(points[3], points[0], get_node("/root/Spatial/Polys"))
# poly 2, as obstacle inside poly 1 # Poly 2, as obstacle inside poly 1
points.push_back(Vector2(2, 0.5)) #4 points.push_back(Vector2(2, 0.5)) # 4
points.push_back(Vector2(4, 0.5)) #5 points.push_back(Vector2(4, 0.5)) # 5
points.push_back(Vector2(4, 9.5)) #6 points.push_back(Vector2(4, 9.5)) # 6
points.push_back(Vector2(2, 9.5)) #7 points.push_back(Vector2(2, 9.5)) # 7
connections.push_back(4) connections.push_back(4)
connections.push_back(5) connections.push_back(5)
@ -45,25 +46,23 @@ func _ready():
connections.push_back(4) connections.push_back(4)
drawLine(points[7], points[4], get_node("/root/Spatial/Polys")) drawLine(points[7], points[4], get_node("/root/Spatial/Polys"))
print("points: ", points)
print("points: ",points) print("connections: ", connections)
print("connections: ",connections)
pf.setup(points, connections) pf.setup(points, connections)
var path = pf.find_path(Vector2(1, 5), Vector2(8, 5)) var path = pf.find_path(Vector2(1, 5), Vector2(8, 5))
var lastStep = null var lastStep = null
print("path: ",path) print("path: ", path)
for step in path: for step in path:
print("step: ",step) print("step: ", step)
if (lastStep != null): if (lastStep != null):
var currPathSegment = Vector2Array() var currPathSegment = Vector2Array()
drawLine(lastStep, step, get_node("/root/Spatial/Path")) drawLine(lastStep, step, get_node("/root/Spatial/Path"))
lastStep = step lastStep = step
func drawLine(pointA, pointB, immediateGeo): func drawLine(pointA, pointB, immediateGeo):
var drawPosY = 0.1 var drawPosY = 0.1
var im = immediateGeo var im = immediateGeo
@ -76,5 +75,3 @@ func drawLine(pointA, pointB, immediateGeo):
im.add_vertex(Vector3(pointA.x, drawPosY, pointA.y)) im.add_vertex(Vector3(pointA.x, drawPosY, pointA.y))
im.add_vertex(Vector3(pointB.x, drawPosY, pointB.y)) im.add_vertex(Vector3(pointB.x, drawPosY, pointB.y))
im.end() im.end()

View File

@ -1,73 +1,68 @@
extends Node2D extends Node2D
#member variables here, example: # Member variables
#var a=2
#var b="textvar"
const INITIAL_BALL_SPEED = 80 const INITIAL_BALL_SPEED = 80
var ball_speed = INITIAL_BALL_SPEED var ball_speed = INITIAL_BALL_SPEED
var screen_size = Vector2(640,400) var screen_size = Vector2(640, 400)
#default ball direction
var direction = Vector2(-1,0) # Default ball direction
var pad_size = Vector2(8,32) var direction = Vector2(-1, 0)
var pad_size = Vector2(8, 32)
const PAD_SPEED = 150 const PAD_SPEED = 150
func _process(delta): func _process(delta):
# Get ball position and pad rectangles
#get ball position and pad rectangles
var ball_pos = get_node("ball").get_pos() var ball_pos = get_node("ball").get_pos()
var left_rect = Rect2( get_node("left").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 ) var right_rect = Rect2(get_node("right").get_pos() - pad_size*0.5, pad_size)
#integrate new ball postion # Integrate new ball postion
ball_pos+=direction*ball_speed*delta ball_pos += direction*ball_speed*delta
#flip when touching roof or floor # 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)): if ((ball_pos.y < 0 and direction.y < 0) or (ball_pos.y > screen_size.y and direction.y > 0)):
direction.y = -direction.y direction.y = -direction.y
#flip, change direction and increase speed when touching pads # 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)): 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 direction.x = -direction.x
ball_speed*=1.1 ball_speed *= 1.1
direction.y=randf()*2.0-1 direction.y = randf()*2.0 - 1
direction = direction.normalized() direction = direction.normalized()
#check gameover # Check gameover
if (ball_pos.x<0 or ball_pos.x>screen_size.x): if (ball_pos.x < 0 or ball_pos.x > screen_size.x):
ball_pos=screen_size*0.5 ball_pos = screen_size*0.5
ball_speed=INITIAL_BALL_SPEED ball_speed = INITIAL_BALL_SPEED
direction=Vector2(-1,0) direction = Vector2(-1, 0)
get_node("ball").set_pos(ball_pos) get_node("ball").set_pos(ball_pos)
#move left pad # Move left pad
var left_pos = get_node("left").get_pos() var left_pos = get_node("left").get_pos()
if (left_pos.y > 0 and Input.is_action_pressed("left_move_up")): 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")): 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) get_node("left").set_pos(left_pos)
#move right pad # Move right pad
var right_pos = get_node("right").get_pos() var right_pos = get_node("right").get_pos()
if (right_pos.y > 0 and Input.is_action_pressed("right_move_up")): 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")): if (right_pos.y < screen_size.y and Input.is_action_pressed("right_move_down")):
right_pos.y+=PAD_SPEED*delta right_pos.y += PAD_SPEED*delta
get_node("right").set_pos(right_pos) get_node("right").set_pos(right_pos)
func _ready(): 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() pad_size = get_node("left").get_texture().get_size()
set_process(true) set_process(true)

View File

@ -1,32 +1,26 @@
extends Control extends Control
# member variables here, example:
# var a=2
# var b="textvar"
func _ready(): func _ready():
# Initialization here # Initialization here
for c in get_node("pictures").get_children(): 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(): for c in get_node("effects").get_children():
get_node("effect").add_item("FX: "+c.get_name()) get_node("effect").add_item("FX: " + c.get_name())
pass
func _on_picture_item_selected(ID):
func _on_picture_item_selected( ID ):
for c in range(get_node("pictures").get_child_count()): for c in range(get_node("pictures").get_child_count()):
if (ID==c): if (ID == c):
get_node("pictures").get_child(c).show() get_node("pictures").get_child(c).show()
else: else:
get_node("pictures").get_child(c).hide() 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()): for c in range(get_node("effects").get_child_count()):
if (ID==c): if (ID == c):
get_node("effects").get_child(c).show() get_node("effects").get_child(c).show()
else: else:
get_node("effects").get_child(c).hide() get_node("effects").get_child(c).hide()

View File

@ -4,13 +4,16 @@ extends Node2D
# This demo is an example of controling a high number of 2D objects with logic and collision without using scene nodes. # 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 # 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 BULLET_COUNT = 500
const SPEED_MIN = 20 const SPEED_MIN = 20
const SPEED_MAX = 50 const SPEED_MAX = 50
var bullets=[] var bullets = []
var shape var shape
# Inner classes
class Bullet: class Bullet:
var pos = Vector2() var pos = Vector2()
var speed = 1.0 var speed = 1.0
@ -18,48 +21,46 @@ class Bullet:
func _draw(): func _draw():
var t = preload("res://bullet.png") var t = preload("res://bullet.png")
var tofs = -t.get_size()*0.5 var tofs = -t.get_size()*0.5
for b in bullets: for b in bullets:
draw_texture(t,b.pos+tofs) draw_texture(t, b.pos + tofs)
func _process(delta): func _process(delta):
var width = get_viewport_rect().size.x*2.0 var width = get_viewport_rect().size.x*2.0
var mat = Matrix32() var mat = Matrix32()
for b in bullets: for b in bullets:
b.pos.x-=b.speed*delta b.pos.x -= b.speed*delta
if (b.pos.x < -30): if (b.pos.x < -30):
b.pos.x+=width b.pos.x += width
mat.o=b.pos mat.o = b.pos
Physics2DServer.body_set_state(b.body,Physics2DServer.BODY_STATE_TRANSFORM,mat) Physics2DServer.body_set_state(b.body, Physics2DServer.BODY_STATE_TRANSFORM, mat)
update() update()
func _ready(): func _ready():
# Initialization here
shape = Physics2DServer.shape_create(Physics2DServer.SHAPE_CIRCLE) shape = Physics2DServer.shape_create(Physics2DServer.SHAPE_CIRCLE)
Physics2DServer.shape_set_data(shape,8) #radius Physics2DServer.shape_set_data(shape, 8) # Radius
for i in range(BULLET_COUNT): for i in range(BULLET_COUNT):
var b = Bullet.new() 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) b.body = Physics2DServer.body_create(Physics2DServer.BODY_MODE_KINEMATIC)
Physics2DServer.body_set_space(b.body,get_world_2d().get_space()) Physics2DServer.body_set_space(b.body, get_world_2d().get_space())
Physics2DServer.body_add_shape(b.body,shape) Physics2DServer.body_add_shape(b.body, shape)
b.pos = Vector2( get_viewport_rect().size * Vector2(randf()*2.0,randf()) ) #twice as long 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.x += get_viewport_rect().size.x # Start outside
var mat = Matrix32() var mat = Matrix32()
mat.o=b.pos mat.o = b.pos
Physics2DServer.body_set_state(b.body,Physics2DServer.BODY_STATE_TRANSFORM,mat) Physics2DServer.body_set_state(b.body, Physics2DServer.BODY_STATE_TRANSFORM, mat)
bullets.append(b) bullets.append(b)
set_process(true) set_process(true)
@ -68,9 +69,4 @@ func _exit_tree():
Physics2DServer.free_rid(b.body) Physics2DServer.free_rid(b.body)
Physics2DServer.free_rid(shape) Physics2DServer.free_rid(shape)
# Initalization here
bullets.clear() bullets.clear()
pass

View File

@ -1,32 +1,26 @@
extends Node2D extends Node2D
# member variables here, example: # Member variables
# var a=2 var touching = 0
# var b="textvar"
var touching=0 func _input(event):
if (event.type == InputEvent.MOUSE_MOTION):
func _input(ev): get_node("player").set_pos(event.pos - Vector2(0, 16))
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 ): func _on_player_body_enter_shape(body_id, body, body_shape, area_shape):
touching += 1
touching+=1 if (touching == 1):
if (touching==1):
get_node("player/sprite").set_frame(1) get_node("player/sprite").set_frame(1)
func _on_player_body_exit_shape( body_id, body, body_shape, area_shape ): func _on_player_body_exit_shape(body_id, body, body_shape, area_shape):
touching -= 1
touching-=1 if (touching == 0):
if (touching==0):
get_node("player/sprite").set_frame(0) get_node("player/sprite").set_frame(0)
func _ready(): func _ready():
# Initialization here
set_process_input(true) set_process_input(true)
pass

View File

@ -1,38 +1,34 @@
extends Area2D extends Area2D
# member variables here, example: # Member variables
# var a=2 const SPEED = -200
# var b="textvar" const Y_RANDOM = 10
const SPEED=-200 var points = 1
const Y_RANDOM=10 var speed_y = 0.0
var destroyed = false
var points=1
var speed_y=0.0
func _process(delta): func _process(delta):
translate(Vector2(SPEED, speed_y)*delta)
translate( Vector2(SPEED,speed_y) * delta )
func _ready(): func _ready():
# Initialization here # Initialization here
speed_y=rand_range(-Y_RANDOM,Y_RANDOM) speed_y = rand_range(-Y_RANDOM, Y_RANDOM)
pass
var destroyed=false
func destroy(): func destroy():
if (destroyed): if (destroyed):
return return
destroyed=true destroyed = true
get_node("anim").play("explode") get_node("anim").play("explode")
set_process(false) set_process(false)
get_node("sfx").play("sound_explode") get_node("sfx").play("sound_explode")
#accum points # Accumulate points
get_node("/root/game_state").points+=1 get_node("/root/game_state").points += 1
func is_enemy(): func is_enemy():
return not destroyed return not destroyed
@ -40,10 +36,9 @@ func is_enemy():
func _on_visibility_enter_screen(): func _on_visibility_enter_screen():
set_process(true) set_process(true)
#make it spin! # Make it spin!
get_node("anim").play("spin") get_node("anim").play("spin")
func _on_visibility_exit_screen(): func _on_visibility_exit_screen():
queue_free() queue_free()
pass # replace with function body

View File

@ -1,18 +1,16 @@
extends Area2D extends Area2D
# member variables here, example: # Member variables
# var a=2 const SPEED = -200
# var b="textvar"
const SPEED=-200
func _process(delta):
get_parent().translate(Vector2(SPEED*delta,0))
var destroyed=false var destroyed=false
func _process(delta):
get_parent().translate(Vector2(SPEED*delta, 0))
func is_enemy(): func is_enemy():
return not destroyed return not destroyed
@ -20,18 +18,19 @@ func is_enemy():
func destroy(): func destroy():
if (destroyed): if (destroyed):
return return
destroyed=true destroyed = true
get_node("anim").play("explode") get_node("anim").play("explode")
set_process(false) set_process(false)
get_node("sfx").play("sound_explode") get_node("sfx").play("sound_explode")
#accum points # Accumulate points
get_node("/root/game_state").points+=5 get_node("/root/game_state").points += 5
func _on_visibility_enter_screen(): func _on_visibility_enter_screen():
set_process(true) set_process(true)
get_node("anim").play("zigzag") get_node("anim").play("zigzag")
get_node("anim").seek(randf()*2.0) #make it start from any pos get_node("anim").seek(randf()*2.0) # Make it start from any pos
func _on_visibility_exit_screen(): func _on_visibility_exit_screen():
queue_free() queue_free()

View File

@ -1,56 +1,52 @@
extends Area2D extends Area2D
# member variables here, example: # Member variables
# var a=2 const SPEED = -220
# var b="textvar" const SHOOT_INTERVAL = 1
const SPEED=-220
const SHOOT_INTERVAL=1 var shoot_timeout = 0
var shoot_timeout=0 var destroyed=false
func _process(delta): func _process(delta):
translate( Vector2(SPEED*delta,0) ) translate(Vector2(SPEED*delta, 0))
shoot_timeout-=delta shoot_timeout -= delta
if (shoot_timeout<0): if (shoot_timeout < 0):
shoot_timeout = SHOOT_INTERVAL
shoot_timeout=SHOOT_INTERVAL # Instance a shot
#instance a shot
var shot = preload("res://enemy_shot.scn").instance() var shot = preload("res://enemy_shot.scn").instance()
#set pos as "shoot_from" Position2D node # Set pos as "shoot_from" Position2D node
shot.set_pos( get_node("shoot_from").get_global_pos() ) shot.set_pos(get_node("shoot_from").get_global_pos())
#add it to parent, so it has world coordinates # Add it to parent, so it has world coordinates
get_parent().add_child(shot) get_parent().add_child(shot)
var destroyed=false
func is_enemy(): func is_enemy():
return not destroyed return not destroyed
func destroy(): func destroy():
if (destroyed): if (destroyed):
return return
destroyed=true destroyed = true
get_node("anim").play("explode") get_node("anim").play("explode")
set_process(false) set_process(false)
get_node("sfx").play("sound_explode") get_node("sfx").play("sound_explode")
#accum points # Accumulate points
get_node("/root/game_state").points+=10 get_node("/root/game_state").points += 10
func _ready(): func _ready():
set_fixed_process(true)
# Initialization here # Initialization here
pass set_fixed_process(true)
func _on_visibility_enter_screen(): func _on_visibility_enter_screen():
set_process(true) set_process(true)
pass # replace with function body
func _on_visibility_exit_screen(): func _on_visibility_exit_screen():
queue_free() queue_free()
pass # replace with function body

View File

@ -1,32 +1,32 @@
extends Area2D extends Area2D
# member variables here, example: # Member variables
# var a=2
# var b="textvar"
const SPEED = -800 const SPEED = -800
var hit = false
func _process(delta): func _process(delta):
translate(Vector2(delta*SPEED,0)) translate(Vector2(delta*SPEED, 0))
func _ready(): func _ready():
# Initialization here # Initialization here
set_process(true) set_process(true)
var hit=false
func is_enemy(): func is_enemy():
return true return true
func _hit_something(): func _hit_something():
if (hit): if (hit):
return return
hit=true hit = true
set_process(false) set_process(false)
get_node("anim").play("splash") get_node("anim").play("splash")
func _on_visibility_exit_screen(): func _on_visibility_exit_screen():
queue_free() queue_free()

View File

@ -1,24 +1,22 @@
extends Node extends Node
# Member variables
var points = 0 var points = 0
var max_points = 0 var max_points = 0
func _ready(): func _ready():
var f = File.new() var f = File.new()
#load high score # Load high score
if (f.open("user://highscore", File.READ) == OK):
if (f.open("user://highscore",File.READ)==OK): max_points = f.get_var()
max_points=f.get_var()
func game_over(): func game_over():
if (points>max_points): if (points > max_points):
max_points=points max_points = points
#save high score # Save high score
var f = File.new() var f = File.new()
f.open("user://highscore",File.WRITE) f.open("user://highscore", File.WRITE)
f.store_var(max_points) f.store_var(max_points)

View File

@ -1,20 +1,12 @@
extends Control extends Control
# member variables here, example:
# var a=2
# var b="textvar"
func _ready(): func _ready():
get_node("score").set_text( "HIGH SCORE: "+str( get_node("/root/game_state").max_points ) )
# Initialization here # Initialization here
pass get_node("score").set_text("HIGH SCORE: " + str(get_node("/root/game_state").max_points))
func _on_play_pressed(): 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") get_tree().change_scene("res://level.scn")
pass # replace with function body

View File

@ -1,25 +1,20 @@
extends Node2D 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(): func stop():
set_process(false) set_process(false)
var offset=0
func _process(delta): func _process(delta):
offset+=delta*SPEED offset += delta*SPEED
set_pos(Vector2(offset,0)) set_pos(Vector2(offset, 0))
func _ready(): func _ready():
set_process(true)
# Initialization here # Initialization here
set_process(true)

View File

@ -1,71 +1,66 @@
extends Area2D extends Area2D
# member variables here, example: # Member variables
# var a=2
# var b="textvar"
const SPEED = 200 const SPEED = 200
var screen_size var screen_size
var prev_shooting = false
var killed = false
var prev_shooting=false
func _process(delta): func _process(delta):
var motion = Vector2() var motion = Vector2()
if Input.is_action_pressed("move_up"): if Input.is_action_pressed("move_up"):
motion+=Vector2(0,-1) motion += Vector2(0, -1)
if Input.is_action_pressed("move_down"): if Input.is_action_pressed("move_down"):
motion+=Vector2(0,1) motion += Vector2(0, 1)
if Input.is_action_pressed("move_left"): if Input.is_action_pressed("move_left"):
motion+=Vector2(-1,0) motion += Vector2(-1, 0)
if Input.is_action_pressed("move_right"): if Input.is_action_pressed("move_right"):
motion+=Vector2(1,0) motion += Vector2(1, 0)
var shooting = Input.is_action_pressed("shoot") var shooting = Input.is_action_pressed("shoot")
var pos = get_pos() var pos = get_pos()
pos+=motion*delta*SPEED pos += motion*delta*SPEED
if (pos.x<0): if (pos.x < 0):
pos.x=0 pos.x = 0
if (pos.x>screen_size.x): if (pos.x > screen_size.x):
pos.x=screen_size.x pos.x = screen_size.x
if (pos.y<0): if (pos.y < 0):
pos.y=0 pos.y = 0
if (pos.y>screen_size.y): if (pos.y > screen_size.y):
pos.y=screen_size.y pos.y = screen_size.y
set_pos(pos) set_pos(pos)
if (shooting and not prev_shooting): if (shooting and not prev_shooting):
# just pressed # Just pressed
var shot = preload("res://shot.scn").instance() var shot = preload("res://shot.scn").instance()
#use the position3d as reference # Use the Position2D as reference
shot.set_pos( get_node("shootfrom").get_global_pos() ) shot.set_pos(get_node("shootfrom").get_global_pos())
#put it two parents above, so it is not moved by us # Put it two parents above, so it is not moved by us
get_node("../..").add_child(shot) get_node("../..").add_child(shot)
#play sound # Play sound
get_node("sfx").play("shoot") get_node("sfx").play("shoot")
prev_shooting = shooting prev_shooting = shooting
#update points counter # Update points counter
get_node("../hud/score_points").set_text( str(get_node("/root/game_state").points) ) get_node("../hud/score_points").set_text(str(get_node("/root/game_state").points))
func _ready(): func _ready():
# Initialization here # Initialization here
screen_size = get_viewport().get_rect().size screen_size = get_viewport().get_rect().size
set_process(true) set_process(true)
pass
var killed=false
func _hit_something(): func _hit_something():
if (killed): if (killed):
return return
killed=true killed = true
get_node("anim").play("explode") get_node("anim").play("explode")
get_node("sfx").play("sound_explode") get_node("sfx").play("sound_explode")
get_node("../hud/game_over").show() get_node("../hud/game_over").show()
@ -74,15 +69,14 @@ func _hit_something():
set_process(false) set_process(false)
func _on_ship_body_enter( body ): func _on_ship_body_enter(body):
_hit_something() _hit_something()
func _on_ship_area_enter( area ): func _on_ship_area_enter(area):
if (area.has_method("is_enemy") and area.is_enemy()): if (area.has_method("is_enemy") and area.is_enemy()):
_hit_something() _hit_something()
func _on_back_to_menu_pressed(): func _on_back_to_menu_pressed():
get_tree().change_scene("res://main_menu.scn") get_tree().change_scene("res://main_menu.scn")
pass # replace with function body

View File

@ -1,48 +1,41 @@
extends Area2D extends Area2D
# member variables here, example: # Member variables
# var a=2
# var b="textvar"
const SPEED = 800 const SPEED = 800
var hit = false
func _process(delta): func _process(delta):
translate(Vector2(delta*SPEED,0)) translate(Vector2(delta*SPEED, 0))
func _ready(): func _ready():
# Initialization here # Initialization here
set_process(true) set_process(true)
pass
var hit=false
func _hit_something(): func _hit_something():
if (hit): if (hit):
return return
hit=true hit = true
set_process(false) set_process(false)
get_node("anim").play("splash") get_node("anim").play("splash")
func _on_visibility_exit_screen(): func _on_visibility_exit_screen():
queue_free() queue_free()
pass # replace with function body
func _on_shot_area_enter(area):
func _on_shot_area_enter( area ): # Hit an enemy or asteroid
#hit an enemy or asteroid
if (area.has_method("destroy")): if (area.has_method("destroy")):
#duck typing at it's best # Duck typing at it's best
area.destroy() area.destroy()
_hit_something() _hit_something()
pass func _on_shot_body_enter(body):
# Hit the tilemap
func _on_shot_body_enter( body ):
#hit the tilemap
_hit_something() _hit_something()
pass # replace with function body

View File

@ -1,82 +1,76 @@
extends Control extends Control
# Simple Tetris-like demo, (c) 2012 Juan Linietsky # Simple Tetris-like demo, (c) 2012 Juan Linietsky
# Implemented by using a regular Control and drawing on it during the _draw() callback. # 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()) # The drawing surface is updated only when changes happen (by calling update())
# Member variables
var score = 0 var score = 0
var score_label=null var score_label = null
const MAX_SHAPES = 7 const MAX_SHAPES = 7
var block = preload("block.png") var block = preload("block.png")
var block_colors=[ var block_colors = [
Color(1,0.5,0.5), Color(1, 0.5, 0.5),
Color(0.5,1,0.5), Color(0.5, 1, 0.5),
Color(0.5,0.5,1), Color(0.5, 0.5, 1),
Color(0.8,0.4,0.8), Color(0.8, 0.4, 0.8),
Color(0.8,0.8,0.4), Color(0.8, 0.8, 0.4),
Color(0.4,0.8,0.8), Color(0.4, 0.8, 0.8),
Color(0.7,0.7,0.7)] Color(0.7, 0.7, 0.7)]
var block_shapes=[ var block_shapes = [
[ Vector2(0,-1),Vector2(0,0),Vector2(0,1),Vector2(0,2) ], # I [ 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(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) ], # S
[ Vector2(1,1),Vector2(0,1),Vector2(0,0),Vector2(-1,0) ], # Z [ 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) ], # L
[ Vector2(1,1),Vector2(1,0),Vector2(0,0),Vector2(-1,0) ], # J [ 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 [ Vector2(0, 1), Vector2(1, 0), Vector2(0, 0), Vector2(-1, 0) ]] # T
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 width = 0
var height = 0
var cells = {}
var piece_active = false
var piece_shape = 0
var piece_pos = Vector2()
var piece_rot = 0
var block_rotations=[ func piece_cell_xform(p, er = 0):
Matrix32( Vector2(1,0),Vector2(0,1), Vector2() ), var r = (4 + er + piece_rot) % 4
Matrix32( Vector2(0,1),Vector2(-1,0), Vector2() ), return piece_pos + block_rotations[r].xform(p)
Matrix32( Vector2(-1,0),Vector2(0,-1), Vector2() ),
Matrix32( Vector2(0,-1),Vector2(1,0), Vector2() )
]
var width=0
var height=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 _draw():
var sb = get_stylebox("bg", "Tree") # Use line edit bg
var sb = get_stylebox("bg","Tree") # use line edit bg draw_style_box(sb, Rect2(Vector2(), get_size()).grow(3))
draw_style_box(sb,Rect2(Vector2(),get_size()).grow(3))
var bs = block.get_size() var bs = block.get_size()
for y in range(height): for y in range(height):
for x in range(width): for x in range(width):
if (Vector2(x,y) in cells): if (Vector2(x, y) in cells):
draw_texture_rect(block,Rect2(Vector2(x,y)*bs,bs),false,block_colors[cells[Vector2(x,y)]]) draw_texture_rect(block, Rect2(Vector2(x, y)*bs, bs), false, block_colors[cells[Vector2(x, y)]])
if (piece_active): if (piece_active):
for c in block_shapes[piece_shape]: 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]: 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): if (pos.x < 0):
return false return false
if (pos.y < 0): if (pos.y < 0):
@ -90,105 +84,94 @@ func piece_check_fit(ofs,er=0):
return true return true
func new_piece():
func new_piece():
piece_shape = randi() % MAX_SHAPES piece_shape = randi() % MAX_SHAPES
piece_pos = Vector2(width/2,0) piece_pos = Vector2(width/2, 0)
piece_active=true piece_active = true
piece_rot=0 piece_rot = 0
if (piece_shape==0): if (piece_shape == 0):
piece_pos.y+=1 piece_pos.y += 1
if (not piece_check_fit(Vector2())): if (not piece_check_fit(Vector2())):
#game over # Game over
#print("GAME OVER!")
game_over() game_over()
update() update()
func test_collapse_rows(): func test_collapse_rows():
var accum_down=0 var accum_down = 0
for i in range(height): for i in range(height):
var y = height - i - 1 var y = height - i - 1
var collapse = true var collapse = true
for x in range(width): for x in range(width):
if (Vector2(x,y) in cells): if (Vector2(x, y) in cells):
if (accum_down): if (accum_down):
cells[ Vector2(x,y+accum_down) ] = cells[Vector2(x,y)] cells[Vector2(x, y + accum_down)] = cells[Vector2(x, y)]
else: else:
collapse=false collapse = false
if (accum_down): if (accum_down):
cells.erase( Vector2(x,y+accum_down) ) cells.erase(Vector2(x, y + accum_down))
if (collapse): if (collapse):
accum_down+=1 accum_down += 1
score += accum_down*100
score+=accum_down*100
score_label.set_text(str(score)) score_label.set_text(str(score))
func game_over(): func game_over():
piece_active = false
piece_active=false get_node("gameover").set_text("Game over!")
get_node("gameover").set_text("Game Over") update()
update()
func restart_pressed(): func restart_pressed():
score = 0
score=0 score_label.set_text("0")
score_label.set_text("0") cells.clear()
cells.clear() get_node("gameover").set_text("")
get_node("gameover").set_text("") piece_active = true
piece_active=true get_node("../restart").release_focus()
get_node("../restart").release_focus() update()
update()
func piece_move_down(): func piece_move_down():
if (!piece_active): if (!piece_active):
return return
if (piece_check_fit(Vector2(0,1))): if (piece_check_fit(Vector2(0, 1))):
piece_pos.y+=1 piece_pos.y += 1
update() update()
else: else:
for c in block_shapes[piece_shape]: for c in block_shapes[piece_shape]:
var pos = piece_cell_xform(c) var pos = piece_cell_xform(c)
cells[pos]=piece_shape cells[pos] = piece_shape
test_collapse_rows() test_collapse_rows()
new_piece() new_piece()
func piece_rotate(): func piece_rotate():
var adv = 1 var adv = 1
if (not piece_check_fit(Vector2(),1)): if (not piece_check_fit(Vector2(), 1)):
return return
piece_rot = (piece_rot + adv) % 4 piece_rot = (piece_rot + adv) % 4
update() update()
func _input(ie): func _input(ie):
if (not piece_active): if (not piece_active):
return return
if (!ie.is_pressed()): if (!ie.is_pressed()):
return return
if (ie.is_action("move_left")): if (ie.is_action("move_left")):
if (piece_check_fit(Vector2(-1,0))): if (piece_check_fit(Vector2(-1, 0))):
piece_pos.x-=1 piece_pos.x -= 1
update() update()
elif (ie.is_action("move_right")): elif (ie.is_action("move_right")):
if (piece_check_fit(Vector2(1,0))): if (piece_check_fit(Vector2(1, 0))):
piece_pos.x+=1 piece_pos.x += 1
update() update()
elif (ie.is_action("move_down")): elif (ie.is_action("move_down")):
piece_move_down() piece_move_down()
@ -196,22 +179,17 @@ func _input(ie):
piece_rotate() piece_rotate()
func setup(w,h): func setup(w, h):
width=w width = w
height=h height = h
set_size( Vector2(w,h)*block.get_size() ) set_size(Vector2(w, h)*block.get_size())
new_piece() new_piece()
get_node("timer").start() get_node("timer").start()
func _ready(): func _ready():
# Initalization here # Initalization here
setup(10, 20)
setup(10,20)
score_label = get_node("../score") score_label = get_node("../score")
set_process_input(true) set_process_input(true)

View File

@ -1,17 +1,12 @@
extends Control extends Control
# member variables here, example: # Member variables
# var a=2 const MAX_BUBBLES = 10
# var b="textvar"
const MAX_BUBBLES=10
func _ready(): func _ready():
# Initialization here # Initialization here
for i in range(MAX_BUBBLES): for i in range(MAX_BUBBLES):
var bubble = preload("res://lens.scn").instance() var bubble = preload("res://lens.scn").instance()
add_child(bubble) add_child(bubble)
pass

View File

@ -1,37 +1,32 @@
extends BackBufferCopy extends BackBufferCopy
# member variables here, example: # Member variables
# var a=2 const MOTION_SPEED = 150
# var b="textvar"
const MOTION_SPEED=150 var vsize
var dir
var vsize;
var dir;
func _process(delta): func _process(delta):
var pos = get_pos() + dir * delta * MOTION_SPEED var pos = get_pos() + dir*delta*MOTION_SPEED
if (pos.x<0): if (pos.x < 0):
dir.x=abs(dir.x) dir.x = abs(dir.x)
elif (pos.x>vsize.x): elif (pos.x > vsize.x):
dir.x=-abs(dir.x) dir.x = -abs(dir.x)
if (pos.y<0): if (pos.y < 0):
dir.y=abs(dir.y) dir.y = abs(dir.y)
elif (pos.y>vsize.y): elif (pos.y > vsize.y):
dir.y=-abs(dir.y) dir.y = -abs(dir.y)
set_pos(pos) set_pos(pos)
func _ready(): func _ready():
vsize = get_viewport_rect().size vsize = get_viewport_rect().size
var pos = vsize * Vector2(randf(),randf()); var pos = vsize*Vector2(randf(), randf())
set_pos(pos); set_pos(pos)
dir = Vector2(randf()*2.0-1,randf()*2.0-1).normalized() dir = Vector2(randf()*2.0 - 1, randf()*2.0 - 1).normalized()
set_process(true) set_process(true)
# Initialization here
pass