49
demos/2d/space_shooter/asteroid.gd
Normal file
@ -0,0 +1,49 @@
|
||||
|
||||
extends Area2D
|
||||
|
||||
# member variables here, example:
|
||||
# var a=2
|
||||
# var b="textvar"
|
||||
|
||||
const SPEED=-200
|
||||
const Y_RANDOM=10
|
||||
|
||||
var points=1
|
||||
|
||||
|
||||
var speed_y=0.0
|
||||
|
||||
func _process(delta):
|
||||
|
||||
translate( Vector2(SPEED,speed_y) * delta )
|
||||
|
||||
func _ready():
|
||||
# Initialization here
|
||||
speed_y=rand_range(-Y_RANDOM,Y_RANDOM)
|
||||
pass
|
||||
|
||||
var destroyed=false
|
||||
|
||||
func destroy():
|
||||
if (destroyed):
|
||||
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
|
||||
|
||||
func is_enemy():
|
||||
return not destroyed
|
||||
|
||||
|
||||
func _on_visibility_enter_screen():
|
||||
set_process(true)
|
||||
#make it spin!
|
||||
get_node("anim").play("spin")
|
||||
|
||||
|
||||
func _on_visibility_exit_screen():
|
||||
queue_free()
|
||||
pass # replace with function body
|
||||
BIN
demos/2d/space_shooter/asteroid.scn
Normal file
BIN
demos/2d/space_shooter/bg_gradient.png
Normal file
|
After Width: | Height: | Size: 1.5 KiB |
BIN
demos/2d/space_shooter/big_star.png
Normal file
|
After Width: | Height: | Size: 275 B |
37
demos/2d/space_shooter/enemy1.gd
Normal file
@ -0,0 +1,37 @@
|
||||
|
||||
extends Area2D
|
||||
|
||||
# member variables here, example:
|
||||
# var a=2
|
||||
# var b="textvar"
|
||||
|
||||
const SPEED=-200
|
||||
|
||||
func _process(delta):
|
||||
get_parent().translate(Vector2(SPEED*delta,0))
|
||||
|
||||
|
||||
var destroyed=false
|
||||
|
||||
func is_enemy():
|
||||
return not destroyed
|
||||
|
||||
|
||||
func destroy():
|
||||
if (destroyed):
|
||||
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+=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
|
||||
|
||||
func _on_visibility_exit_screen():
|
||||
queue_free()
|
||||
|
||||
BIN
demos/2d/space_shooter/enemy1.png
Normal file
|
After Width: | Height: | Size: 6.9 KiB |
BIN
demos/2d/space_shooter/enemy1.scn
Normal file
56
demos/2d/space_shooter/enemy2.gd
Normal file
@ -0,0 +1,56 @@
|
||||
|
||||
extends Area2D
|
||||
|
||||
# member variables here, example:
|
||||
# var a=2
|
||||
# var b="textvar"
|
||||
const SPEED=-220
|
||||
const SHOOT_INTERVAL=1
|
||||
var shoot_timeout=0
|
||||
|
||||
func _process(delta):
|
||||
translate( Vector2(SPEED*delta,0) )
|
||||
shoot_timeout-=delta
|
||||
|
||||
if (shoot_timeout<0):
|
||||
|
||||
shoot_timeout=SHOOT_INTERVAL
|
||||
|
||||
#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
|
||||
get_parent().add_child(shot)
|
||||
|
||||
var destroyed=false
|
||||
|
||||
func is_enemy():
|
||||
return not destroyed
|
||||
|
||||
func destroy():
|
||||
if (destroyed):
|
||||
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+=10
|
||||
|
||||
func _ready():
|
||||
set_fixed_process(true)
|
||||
# Initialization here
|
||||
pass
|
||||
|
||||
|
||||
|
||||
|
||||
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
|
||||
BIN
demos/2d/space_shooter/enemy2.png
Normal file
|
After Width: | Height: | Size: 5.5 KiB |
BIN
demos/2d/space_shooter/enemy2.scn
Normal file
32
demos/2d/space_shooter/enemy_shot.gd
Normal file
@ -0,0 +1,32 @@
|
||||
|
||||
extends Area2D
|
||||
|
||||
# member variables here, example:
|
||||
# var a=2
|
||||
# var b="textvar"
|
||||
|
||||
const SPEED = -800
|
||||
|
||||
func _process(delta):
|
||||
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
|
||||
set_process(false)
|
||||
get_node("anim").play("splash")
|
||||
|
||||
func _on_visibility_exit_screen():
|
||||
queue_free()
|
||||
|
||||
BIN
demos/2d/space_shooter/enemy_shot.png
Normal file
|
After Width: | Height: | Size: 330 B |
BIN
demos/2d/space_shooter/enemy_shot.scn
Normal file
21
demos/2d/space_shooter/engine.cfg
Normal file
@ -0,0 +1,21 @@
|
||||
[application]
|
||||
|
||||
name="Simple Shooter"
|
||||
main_scene="res://main_menu.scn"
|
||||
|
||||
[autoload]
|
||||
|
||||
game_state="res://game_state.gd"
|
||||
|
||||
[display]
|
||||
|
||||
width=1024
|
||||
height=600
|
||||
|
||||
[input]
|
||||
|
||||
move_up=[key(Up)]
|
||||
move_down=[key(Down)]
|
||||
move_left=[key(Left)]
|
||||
move_right=[key(Right)]
|
||||
shoot=[key(Space)]
|
||||
BIN
demos/2d/space_shooter/explosion.scn
Normal file
BIN
demos/2d/space_shooter/fire.png
Normal file
|
After Width: | Height: | Size: 357 B |
24
demos/2d/space_shooter/game_state.gd
Normal file
@ -0,0 +1,24 @@
|
||||
extends Node
|
||||
|
||||
|
||||
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()
|
||||
|
||||
|
||||
|
||||
func game_over():
|
||||
if (points>max_points):
|
||||
max_points=points
|
||||
#save high score
|
||||
var f = File.new()
|
||||
f.open("user://highscore",File.WRITE)
|
||||
f.store_var(max_points)
|
||||
|
||||
BIN
demos/2d/space_shooter/level.scn
Normal file
BIN
demos/2d/space_shooter/level_tiles.res
Normal file
BIN
demos/2d/space_shooter/level_tiles.scn
Normal file
20
demos/2d/space_shooter/main_menu.gd
Normal file
@ -0,0 +1,20 @@
|
||||
|
||||
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
|
||||
|
||||
|
||||
|
||||
|
||||
func _on_play_pressed():
|
||||
get_node("/root/game_state").points=0
|
||||
get_tree().change_scene("res://level.scn")
|
||||
pass # replace with function body
|
||||
BIN
demos/2d/space_shooter/main_menu.scn
Normal file
BIN
demos/2d/space_shooter/meteorite.png
Normal file
|
After Width: | Height: | Size: 5.9 KiB |
BIN
demos/2d/space_shooter/parallax.scn
Normal file
26
demos/2d/space_shooter/rail.gd
Normal file
@ -0,0 +1,26 @@
|
||||
|
||||
extends Node2D
|
||||
|
||||
|
||||
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))
|
||||
|
||||
func _ready():
|
||||
set_process(true)
|
||||
# Initialization here
|
||||
|
||||
|
||||
|
||||
88
demos/2d/space_shooter/ship.gd
Normal file
@ -0,0 +1,88 @@
|
||||
|
||||
extends Area2D
|
||||
|
||||
# member variables here, example:
|
||||
# var a=2
|
||||
# var b="textvar"
|
||||
|
||||
const SPEED = 200
|
||||
|
||||
var screen_size
|
||||
|
||||
var prev_shooting=false
|
||||
|
||||
func _process(delta):
|
||||
|
||||
var motion = Vector2()
|
||||
if Input.is_action_pressed("move_up"):
|
||||
motion+=Vector2(0,-1)
|
||||
if Input.is_action_pressed("move_down"):
|
||||
motion+=Vector2(0,1)
|
||||
if Input.is_action_pressed("move_left"):
|
||||
motion+=Vector2(-1,0)
|
||||
if Input.is_action_pressed("move_right"):
|
||||
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
|
||||
|
||||
set_pos(pos)
|
||||
|
||||
if (shooting and not prev_shooting):
|
||||
# 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
|
||||
get_node("../..").add_child(shot)
|
||||
#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) )
|
||||
|
||||
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
|
||||
get_node("anim").play("explode")
|
||||
get_node("sfx").play("sound_explode")
|
||||
get_node("../hud/game_over").show()
|
||||
get_node("/root/game_state").game_over()
|
||||
get_parent().stop()
|
||||
set_process(false)
|
||||
|
||||
|
||||
func _on_ship_body_enter( body ):
|
||||
_hit_something()
|
||||
|
||||
|
||||
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
|
||||
BIN
demos/2d/space_shooter/ship.png
Normal file
|
After Width: | Height: | Size: 2.7 KiB |
BIN
demos/2d/space_shooter/ship.scn
Normal file
BIN
demos/2d/space_shooter/shoot.png
Normal file
|
After Width: | Height: | Size: 222 B |
47
demos/2d/space_shooter/shot.gd
Normal file
@ -0,0 +1,47 @@
|
||||
|
||||
extends Area2D
|
||||
|
||||
# member variables here, example:
|
||||
# var a=2
|
||||
# var b="textvar"
|
||||
|
||||
const SPEED = 800
|
||||
|
||||
func _process(delta):
|
||||
translate(Vector2(delta*SPEED,0))
|
||||
|
||||
func _ready():
|
||||
# Initialization here
|
||||
set_process(true)
|
||||
pass
|
||||
|
||||
var hit=false
|
||||
|
||||
func _hit_something():
|
||||
if (hit):
|
||||
return
|
||||
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
|
||||
if (area.has_method("destroy")):
|
||||
#duck typing at it's best
|
||||
area.destroy()
|
||||
_hit_something()
|
||||
|
||||
|
||||
pass
|
||||
|
||||
|
||||
func _on_shot_body_enter( body ):
|
||||
#hit the tilemap
|
||||
_hit_something()
|
||||
pass # replace with function body
|
||||
BIN
demos/2d/space_shooter/shot.scn
Normal file
BIN
demos/2d/space_shooter/small_star.png
Normal file
|
After Width: | Height: | Size: 90 B |
BIN
demos/2d/space_shooter/sound_explode.wav
Normal file
BIN
demos/2d/space_shooter/sound_shoot.wav
Normal file
BIN
demos/2d/space_shooter/tile.png
Normal file
|
After Width: | Height: | Size: 810 B |