Mudanças entre as edições de "Godot Engine: SkyFire"

De Aulas
Linha 60: Linha 60:
 
if $B.position.y - half_h > screensize.y:
 
if $B.position.y - half_h > screensize.y:
 
$B.position.y -= h * 2
 
$B.position.y -= h * 2
 +
</syntaxhighlight>
 +
 +
== Ship ==
 +
 +
<syntaxhighlight lang=gdscript>
 +
extends KinematicBody2D
 +
 +
var speed = 500
 +
var screensize
 +
var half_x
 +
var half_h
 +
 +
func _ready():
 +
screensize = get_viewport_rect().size
 +
half_x = $Sprite.texture.get_width() / 2
 +
half_h = $Sprite.texture.get_height() / 2
 +
 +
 +
func _process(delta):
 +
var vec = Vector2()
 +
if Input.is_action_pressed("ui_right"):
 +
vec.x += speed
 +
elif Input.is_action_pressed("ui_left"):
 +
vec.x -= speed
 +
if Input.is_action_just_pressed("ui_select"):
 +
get_parent().new_bomb(Vector2(position.x, position.y - half_h - 20))
 +
var info = move_and_collide(vec * delta)
 +
if info:
 +
var obj = info.get_collider()
 +
if 'Enemy' in obj.name:
 +
get_parent().game_over()
 +
obj.queue_free()
 +
queue_free()
 +
get_parent().new_explosion(obj.position)
 +
get_parent().new_explosion(position)
 +
position.x = clamp(position.x, half_x, screensize.x - half_x)
 +
</syntaxhighlight>
 +
 +
== Enemy ==
 +
 +
<syntaxhighlight lang=gdscript>
 +
extends KinematicBody2D
 +
 +
const LEFT = 0
 +
const RIGHT = 1
 +
 +
var dead = false
 +
var speed_x = 300
 +
var speed_y = 100
 +
var direction = RIGHT
 +
var w
 +
var h
 +
var half_w
 +
var half_h
 +
var screensize
 +
 +
func _ready():
 +
randomize()
 +
screensize = get_viewport_rect().size
 +
w = $Sprite.texture.get_width()
 +
h = $Sprite.texture.get_height()
 +
half_w = w / 2
 +
half_h = h / 2
 +
position.x = (randi() % int(screensize.x - w)) + half_w
 +
position.y = -100
 +
 +
 +
func _process(delta):
 +
var velocity = Vector2()
 +
if direction == RIGHT:
 +
velocity = Vector2(speed_x, speed_y)
 +
if position.x + half_w > screensize.x:
 +
direction = LEFT
 +
elif direction == LEFT:
 +
velocity = Vector2(-speed_x, speed_y)
 +
if position.x - half_w < 0:
 +
direction = RIGHT
 +
if velocity.length() != 0:
 +
var info = move_and_collide(velocity * delta)
 +
if info:
 +
var obj = info.get_collider()
 +
if 'Ship' in obj.name:
 +
get_parent().game_over()
 +
obj.queue_free()
 +
queue_free()
 +
get_parent().new_explosion(obj.position)
 +
get_parent().new_explosion(position)
 +
if position.y - half_h > screensize.y:
 +
queue_free()
 +
</syntaxhighlight>
 +
 +
== Bomb ==
 +
 +
<syntaxhighlight lang=gdscript>
 +
extends KinematicBody2D
 +
 +
var speed = 200
 +
 +
 +
func _ready():
 +
$SoundBomb.play()
 +
 +
 +
func _process(delta):
 +
var info = move_and_collide(Vector2(0, -speed) * delta)
 +
if info:
 +
var obj = info.get_collider()
 +
if 'Enemy' in obj.name:
 +
obj.queue_free()
 +
queue_free()
 +
get_parent().new_explosion(obj.position)
 +
get_parent().add_score()
 +
if $Sprite.position.y < 0:
 +
queue_free()
 +
</syntaxhighlight>
 +
 +
== Explosion ==
 +
 +
<syntaxhighlight lang=gdscript>
 +
extends StaticBody2D
 +
 +
func _ready():
 +
$AnimatedSprite.play()
 +
$SoundExplosion.play()
 +
 +
func _on_AnimatedSprite_animation_finished():
 +
queue_free()
 +
</syntaxhighlight>
 +
 +
== World ==
 +
 +
<syntaxhighlight lang=gdscript>
 +
extends Node2D
 +
 +
var Bomb = preload("res://Bomb.tscn")
 +
var Explosion = preload("res://Explosion.tscn")
 +
var Enemy = preload("res://Enemy.tscn")
 +
 +
#export (PackedScene) var Enemy
 +
var score = 0
 +
 +
func _ready():
 +
$Score.text = str(score)
 +
 +
func add_score():
 +
score += 1
 +
$Score.text = str(score)
 +
 +
func game_over():
 +
$GameOver.visible = true
 +
 +
func new_bomb(pos):
 +
var bomb = Bomb.instance()
 +
bomb.position = pos
 +
add_child(bomb)
 +
 +
func new_explosion(pos):
 +
var explosion = Explosion.instance()
 +
explosion.position = pos
 +
add_child(explosion)
 +
 +
func _on_EnemyTimer_timeout():
 +
var enemy = Enemy.instance()
 +
add_child(enemy)
 +
$EnemyTimer.wait_time = (randi() % 3) + 2
 +
$EnemyTimer.start()
 
</syntaxhighlight>
 
</syntaxhighlight>

Edição das 16h19min de 18 de novembro de 2022

Afluentes : Jogos Digitais, Usabilidade, desenvolvimento web, mobile e jogos

Assets

Estrutura

  • BackGround (Node2D)
    • A (Sprite)
    • B (Sprite)
  • Ship (KinematicBody2D)
    • Sprite
    • CollisionShape2D
  • Enemy (KinematicBody2D)
    • Sprite
    • CollisionShape2D
  • Bomb (KinematicBody2D)
    • Sprite
    • CollisionShape2D
    • SoundBomb (AudioStreamPlayer)
  • Explosion (StaticBody2D)
    • AnimatedSprite
    • CollisionShape2D
    • SoundExplosion (AudioStreamPlayer)
    • Nó (sinal) - _on_AnimatedSprite_finished()
  • World (Node2D)
    • Background (instancia)
    • Ship (instancia)
    • EnemiTimer (Timer)
    • Music (AudioStreamPlayer)
    • GameOver (TextureRect)
    • Label (Label)
    • Score (Label)

Scripts

Background

extends Node2D

var speed = 100
var h
var half_h
var screensize

func _ready():
	screensize = get_viewport_rect().size
	h = $A.texture.get_height()
	half_h = h / 2


func _process(delta):
	$A.position.y += speed * delta
	$B.position.y += speed * delta
	if $A.position.y - half_h > screensize.y:
		$A.position.y -= h * 2
	if $B.position.y - half_h > screensize.y:
		$B.position.y -= h * 2

Ship

extends KinematicBody2D

var speed = 500
var screensize
var half_x
var half_h

func _ready():
	screensize = get_viewport_rect().size
	half_x = $Sprite.texture.get_width() / 2
	half_h = $Sprite.texture.get_height() / 2


func _process(delta):
	var vec = Vector2()
	if Input.is_action_pressed("ui_right"):
		vec.x += speed
	elif Input.is_action_pressed("ui_left"):
		vec.x -= speed
	if Input.is_action_just_pressed("ui_select"):
		get_parent().new_bomb(Vector2(position.x, position.y - half_h - 20))
	var info = move_and_collide(vec * delta)
	if info:
		var obj = info.get_collider()
		if 'Enemy' in obj.name:
			get_parent().game_over()
			obj.queue_free()
			queue_free()
			get_parent().new_explosion(obj.position)
			get_parent().new_explosion(position)
	position.x = clamp(position.x, half_x, screensize.x - half_x)

Enemy

extends KinematicBody2D

const LEFT = 0
const RIGHT = 1

var dead = false
var speed_x = 300
var speed_y = 100
var direction = RIGHT
var w
var h
var half_w
var half_h
var screensize

func _ready():
	randomize()
	screensize = get_viewport_rect().size
	w = $Sprite.texture.get_width()
	h = $Sprite.texture.get_height()
	half_w = w / 2
	half_h = h / 2
	position.x = (randi() % int(screensize.x - w)) + half_w
	position.y = -100


func _process(delta):
	var velocity = Vector2()
	if direction == RIGHT:
		velocity = Vector2(speed_x, speed_y)
		if position.x + half_w > screensize.x:
			direction = LEFT
	elif direction == LEFT:
		velocity = Vector2(-speed_x, speed_y)
		if position.x - half_w < 0:
			direction = RIGHT
	if velocity.length() != 0:
		var info = move_and_collide(velocity * delta)
		if info:
			var obj = info.get_collider()
			if 'Ship' in obj.name:
				get_parent().game_over()
				obj.queue_free()
				queue_free()
				get_parent().new_explosion(obj.position)
				get_parent().new_explosion(position)
	if position.y - half_h > screensize.y:
		queue_free()

Bomb

extends KinematicBody2D

var speed = 200


func _ready():
	$SoundBomb.play()


func _process(delta):
	var info = move_and_collide(Vector2(0, -speed) * delta)
	if info:
		var obj = info.get_collider()
		if 'Enemy' in obj.name:
			obj.queue_free()
			queue_free()
			get_parent().new_explosion(obj.position)
			get_parent().add_score()
	if $Sprite.position.y < 0:
		queue_free()

Explosion

extends StaticBody2D

func _ready():
	$AnimatedSprite.play()
	$SoundExplosion.play()

func _on_AnimatedSprite_animation_finished():
	queue_free()

World

extends Node2D

var Bomb = preload("res://Bomb.tscn")
var Explosion = preload("res://Explosion.tscn")
var Enemy = preload("res://Enemy.tscn")

#export (PackedScene) var Enemy
var score = 0

func _ready():
	$Score.text = str(score)
	
func add_score():
	score += 1
	$Score.text = str(score)
	
func game_over():
	$GameOver.visible = true

func new_bomb(pos):
	var bomb = Bomb.instance()
	bomb.position = pos
	add_child(bomb)

func new_explosion(pos):
	var explosion = Explosion.instance()
	explosion.position = pos
	add_child(explosion)

func _on_EnemyTimer_timeout():
	var enemy = Enemy.instance()
	add_child(enemy)
	$EnemyTimer.wait_time = (randi() % 3) + 2
	$EnemyTimer.start()