game1: add entity system, dynamic player spawn, multi-level preparation

This commit is contained in:
Alice Gaudon 2020-08-18 16:55:40 +02:00
parent c42e060a2b
commit d58119d498
10 changed files with 145 additions and 19 deletions

View File

@ -0,0 +1,7 @@
[gd_resource type="AtlasTexture" load_steps=2 format=2]
[ext_resource path="res://images/game1/game1_tileset.png" type="Texture" id=1]
[resource]
atlas = ExtResource( 1 )
region = Rect2( 32, 0, 16, 16 )

View File

@ -0,0 +1,7 @@
[gd_resource type="AtlasTexture" load_steps=2 format=2]
[ext_resource path="res://images/game1/game1_tileset.png" type="Texture" id=1]
[resource]
atlas = ExtResource( 1 )
region = Rect2( 64, 0, 16, 16 )

View File

@ -0,0 +1,7 @@
[gd_resource type="AtlasTexture" load_steps=2 format=2]
[ext_resource path="res://images/game1/game1_tileset.png" type="Texture" id=1]
[resource]
atlas = ExtResource( 1 )
region = Rect2( 48, 0, 16, 16 )

View File

@ -0,0 +1,7 @@
[gd_resource type="AtlasTexture" load_steps=2 format=2]
[ext_resource path="res://images/game1/game1_tileset.png" type="Texture" id=1]
[resource]
atlas = ExtResource( 1 )
region = Rect2( 80, 0, 16, 16 )

View File

@ -19,6 +19,11 @@ _global_script_classes=[ {
"language": "GDScript",
"path": "res://scripts/DialogueBox.gd"
}, {
"base": "Sprite",
"class": "Flatfish",
"language": "GDScript",
"path": "res://scripts/game1/Flatfish.gd"
}, {
"base": "Node",
"class": "IntroScript",
"language": "GDScript",
@ -27,6 +32,7 @@ _global_script_classes=[ {
_global_script_class_icons={
"DialogueAction": "",
"DialogueBox": "",
"Flatfish": "",
"IntroScript": ""
}

View File

@ -1,4 +1,4 @@
[gd_scene load_steps=39 format=2]
[gd_scene load_steps=38 format=2]
[ext_resource path="res://images/game1/opening.tres" type="SpriteFrames" id=1]
[ext_resource path="res://images/game1/opening2bg.tres" type="SpriteFrames" id=2]
@ -6,7 +6,6 @@
[ext_resource path="res://scenes/game1/opening2_player.tscn" type="PackedScene" id=4]
[ext_resource path="res://scripts/game1/opening2_next_step_trigger.gd" type="Script" id=5]
[ext_resource path="res://scenes/game1/Level2.tscn" type="PackedScene" id=6]
[ext_resource path="res://images/game1/game1_player.tres" type="SpriteFrames" id=7]
[ext_resource path="res://images/goo/BigGoo1.png" type="Texture" id=8]
[ext_resource path="res://images/goo/BigGoo1_Outlines.png" type="Texture" id=9]
[ext_resource path="res://images/goo/BigGoo2_Outlines.png" type="Texture" id=10]
@ -86,13 +85,10 @@ script = ExtResource( 3 )
[node name="Level1" parent="Game" instance=ExtResource( 29 )]
[node name="Level2" parent="Game" instance=ExtResource( 6 )]
[node name="Player" type="AnimatedSprite" parent="Game"]
frames = ExtResource( 7 )
[node name="Camera2D" type="Camera2D" parent="Game/Player"]
visible = false
[node name="Opening2" type="Node2D" parent="."]
visible = false
[node name="Opening2Bg" type="AnimatedSprite" parent="Opening2"]
frames = ExtResource( 2 )
@ -233,5 +229,6 @@ texture = ExtResource( 27 )
normal_map = ExtResource( 28 )
[node name="Opening1" type="AnimatedSprite" parent="."]
visible = false
frames = ExtResource( 1 )
centered = false

View File

@ -3,7 +3,6 @@
[ext_resource path="res://images/game1/tileset.tres" type="TileSet" id=1]
[node name="Level1" type="Node2D"]
visible = false
[node name="Background" type="TileMap" parent="."]
tile_set = ExtResource( 1 )

9
scenes/game1/Player.tscn Normal file
View File

@ -0,0 +1,9 @@
[gd_scene load_steps=2 format=2]
[ext_resource path="res://images/game1/game1_player.tres" type="SpriteFrames" id=1]
[node name="Player" type="AnimatedSprite"]
frames = ExtResource( 1 )
offset = Vector2( 8, 0 )
[node name="Camera2D" type="Camera2D" parent="."]

20
scripts/game1/Flatfish.gd Normal file
View File

@ -0,0 +1,20 @@
extends Sprite
class_name Flatfish
const s = preload('res://images/game1/flatfish_s.tres')
const s_h = preload('res://images/game1/flatfish_s_h.tres')
const t = preload('res://images/game1/flatfish_t.tres')
const t_h = preload('res://images/game1/flatfish_t_h.tres')
var type: int
func _init(_type : int):
type = _type
if type == FlatfishType.S:
texture = s_h
else:
texture = t_h
enum FlatfishType {S, T}

View File

@ -3,25 +3,92 @@ extends Node
var playing = false
onready var player = $Game/Player
onready var player_camera = $Game/Player/Camera2D
onready var player = preload('res://scenes/game1/Player.tscn').instance()
onready var player_camera = player.get_node("Camera2D")
onready var levels = [
$Game/Level1,
$Game/Level2,
]
var current_level = 0
func _ready():
launch_game()
$Opening1.play('default')
yield($Opening1, 'animation_finished')
$Opening1.hide()
func launch_game():
# Prepare levels
spawn_entities()
# Hide opening
$Opening2.hide()
# Prepare player
var level = levels[current_level]
level.add_child_below_node(level.get_node("Props"), player)
playing = true
player_camera.make_current()
player.connect('frame_changed', self, 'update_camera')
# Spawn player
var spawn_pos = Vector2()
#todo: get spawn pos from map
player.position = spawn_pos + Vector2(8, 8)
func spawn_entities():
for node in levels:
for layer in node.get_children():
var tm = layer as TileMap
var tileset = tm.tile_set
# Flatfish cells
var cells = []
for tile in ['flatfish_s', 'flatfish_s_hidden', 'flatfish_t', 'flatfish_t_hidden']:
cells += tm.get_used_cells_by_id(tileset.find_tile_by_name(tile))
var entities = Node.new()
node.add_child_below_node(node, entities)
for cell in cells:
var fish = Flatfish.new(Flatfish.FlatfishType.S)
fish.position = cell * 16 + Vector2(8, 8)
var t = tm.is_cell_transposed(cell.x, cell.y)
var x = tm.is_cell_x_flipped(cell.x, cell.y)
var y = tm.is_cell_y_flipped(cell.x, cell.y)
if t && x:
fish.rotation_degrees = 90
elif x && y:
fish.rotation_degrees = 180
elif t && y:
fish.rotation_degrees = 270
entities.add_child(fish)
tm.set_cell(cell.x, cell.y, -1)
var go_right = false
var go_left = false
func _input(event):
if playing && !is_camera_moving:
if playing:
if event.is_action_pressed('right'):
move_right()
go_right = true
if event.is_action_pressed('left'):
go_left = true
move()
if event.is_action_released('right'):
go_right = false
if event.is_action_released('left'):
go_left = false
func move():
if playing && !is_camera_moving:
if go_right && !go_left:
move_right()
elif go_left && !go_right:
move_left()
func move_right():
@ -31,26 +98,26 @@ func move_right():
yield(player, 'animation_finished')
is_camera_moving = false
player.play('default')
player.frame = 0
player.position.x += 16
is_camera_moving = false
move()
func move_left():
player.position.x -= 16
is_camera_moving = true
player.position.x -= 16
player.play('moving', true)
update_camera()
yield(player, 'animation_finished')
is_camera_moving = false
player.play('default')
player.frame = 0
is_camera_moving = false
move()
const animation_steps = [3, 6, 10, 13]
var is_camera_moving = false