Skip to content

Commit

Permalink
well, it's a video game
Browse files Browse the repository at this point in the history
  • Loading branch information
nolenroyalty committed Apr 29, 2023
1 parent 9b47346 commit 3e7fea0
Show file tree
Hide file tree
Showing 9 changed files with 170 additions and 42 deletions.
8 changes: 8 additions & 0 deletions Fonts/DefaultFont.tres
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
[gd_resource type="DynamicFont" load_steps=2 format=2]

[ext_resource path="res://Fonts/dogicapixel.ttf" type="DynamicFontData" id=1]

[resource]
resource_local_to_scene = true
size = 8
font_data = ExtResource( 1 )
5 changes: 1 addition & 4 deletions Hazards/Torch.gd
Original file line number Diff line number Diff line change
@@ -1,25 +1,22 @@
extends Sprite

export var PENALTY = 1
export var PENALTY = 35
var MAX_HAZARD_COUNTER = 60
var hazard_counter = 0
var is_hazarding = false

signal hazard_contact(penalty)

func handle_body_entered(_body):
print("entered")
is_hazarding = true

func handle_body_exited(_body):
print("left")
is_hazarding = false
hazard_counter = 0

func _physics_process(_delta):
if is_hazarding:
if hazard_counter == 0:
print('emitting')
emit_signal("hazard_contact", PENALTY)

hazard_counter += 1
Expand Down
11 changes: 6 additions & 5 deletions Levels/Level2.tscn

Large diffs are not rendered by default.

79 changes: 58 additions & 21 deletions Main.gd
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,12 @@ onready var static_line = $StaticLine
onready var line_points = $StaticLine/LinePoints

var bucketLoader = preload("res://Bucket/Bucket.tscn")
var endScreen = preload("res://UI/EndScreen.tscn")
var levels = [preload("res://Levels/Level1.tscn"),
preload("res://Levels/Level2.tscn"),
preload("res://Levels/Level3.tscn")]

enum S { STATIC, DRAGGING }
enum S { STATIC, DRAGGING, WON, LOST, LOADING }
const STRETCH_FACTOR = 1.2
const MAXIMUM_BUCKET_WEIGHT = 80
const MINIMUM_BUCKET_WEIGHT = 0
Expand All @@ -21,7 +22,7 @@ const MAX_EXPECTED_DISTANCE = 400
const STICK_TO_MOUSE_WITHIN = 30
const MAXIMUM_SINGLE_FRAME_Y_MOVE = 1

var loaded_level_idx = 2
var loaded_level_idx = 0
var loaded_level = null
var state = S.STATIC
var mouse_within_line = false
Expand All @@ -32,52 +33,76 @@ var bucket = null

func reset_level_state():
if loaded_level != null:
print("freeing level")
loaded_level.queue_free()
loaded_level = null

if bucket != null:
bucket.queue_free()
bucket = null

state = S.STATIC
bucket_velocity = Vector2.ZERO
mouse_within_line = false

func level_completed():
print("level completed")
State.should_tick = false

func handle_hazard_contact(penalty, _hazard):
print("hazard contact penalty: %d" % [penalty])
State.deduct_ice(penalty)

func load_level():
func won_game():
var end_screen = endScreen.instance()
state = S.WON
# It'd be nice if we had a retry button here
add_child(end_screen)
State.should_tick = false

func lost_game():
var end_screen = endScreen.instance()
state = S.LOST
add_child(end_screen)
State.should_tick = false

func load_level(index):
state = S.LOADING
reset_level_state()

if loaded_level_idx >= levels.size():
if index >= levels.size():
print("level index exceeds number of levels!")
return

var level = levels[loaded_level_idx].instance()
var level = levels[index].instance()
loaded_level = level
fixed_points = level.get_fixed_points()
original_length = line_length(fixed_points)
bucket = bucketLoader.instance()
bucket.position = level.get_and_hide_bucket_position()
var _ignore = level.connect("level_completed", self, "level_completed")
bucket.set_deferred("position", level.get_and_hide_bucket_position())
# bucket.position = level.get_and_hide_bucket_position()
var _ignore = level.connect("level_completed", self, "handle_level_completed")

for node in level.get_hazards():
node.connect("hazard_contact", self, "handle_hazard_contact", [node])

add_child(bucket)
add_child(level)
self.call_deferred("add_child", bucket)
# add_child(bucket)
self.call_deferred("add_child", level)
state = S.STATIC
# Eventually we want to connect a "points updated" function here.
# loaded_level.connect("level_complete", self, "load_level2")

func handle_level_completed():
print("level %d completed" % loaded_level_idx)
if loaded_level_idx + 1 == levels.size():
won_game()
else:
loaded_level_idx += 1
load_level(loaded_level_idx)

func _ready():
load_level()
load_level(loaded_level_idx)
static_line.connect("mouse_entered", self, "handle_mouse_entered")
static_line.connect("mouse_exited", self, "handle_mouse_exited")
VisualServer.set_default_clear_color(Color("6b6ab3"))

var _ignore = State.connect("out_of_ice", self, "lost_game")

func add_bucket_to_points(points_without_bucket, mouse_position) -> Array:
var points_before_and_after = partition_points_before_and_after(points_without_bucket, bucket.position)
Expand Down Expand Up @@ -120,18 +145,25 @@ func add_bucket_to_points(points_without_bucket, mouse_position) -> Array:
# var actual_delta_y = min(desired_delta_y, -MAXIMUM_SINGLE_FRAME_Y_MOVE)
bucket.move_and_collide(Vector2(0, desired_delta_y * 3))
# bucket_velocity.y += desired_delta_y
dy = min(desired_delta_y, -MAXIMUM_SINGLE_FRAME_Y_MOVE)
dy = max(desired_delta_y, -MAXIMUM_SINGLE_FRAME_Y_MOVE)

# bucket.set_deferred("position.y", bucket.position.y + dy)
bucket.position.y += dy

var bucket_point = Vector2(bucket.position.x, bucket.position.y)
return points_before + [bucket_point] + points_after

func _physics_process(_delta):
if Input.is_action_just_pressed("click") and mouse_within_line:
state = S.DRAGGING
match state:
S.WON, S.LOST, S.LOADING: return

if Input.is_action_just_pressed("click") and mouse_within_line and state == S.STATIC:
state = S.DRAGGING
State.should_tick = true

bucket_velocity.y = min(bucket_velocity.y + GRAVITY_DELTA, GRAVITY_MAX)
bucket_velocity = bucket.move_and_slide(bucket_velocity)
if bucket_velocity != Vector2.ZERO:
bucket_velocity = bucket.move_and_slide(bucket_velocity)

match state:
S.STATIC:
Expand All @@ -146,7 +178,8 @@ func _physics_process(_delta):
var proposed_length = line_length(proposed_points)
var allowed_length = original_length * STRETCH_FACTOR
if proposed_length <= allowed_length:
render_line(add_bucket_to_points(proposed_points, mouse_pos))
self.call_deferred("render_line", add_bucket_to_points(proposed_points, mouse_pos))
# render_line(add_bucket_to_points(proposed_points, mouse_pos))
else:
# If we have time it'd be great if we could make dragging here work
# isntead of ignoring it.
Expand All @@ -156,7 +189,8 @@ func render_line(points):
# Maybe this could be smart about keeping most of the polygons?
for child in static_line.get_children():
if child is CollisionPolygon2D:
child.queue_free()
child.call_deferred("queue_free")
# child.queue_free()

line_points.points = points
# Store the line's global position so we can reset its position after moving its parent
Expand All @@ -166,14 +200,17 @@ func render_line(points):
# Move the rigidbody to the center of the line, taking into account
# any offset the Polygon2D node may have relative to the rigidbody
var line_center = get_line_center()
# static_line.set_deferred("global_position", static_line.global_position + line_center + line_points.position)
static_line.global_position += line_center + line_points.position
# Move the line node to its original position
# line_points.set_deferred("global_position", line_global_position)
line_points.global_position = line_global_position

# line_poly may contain multiple polygons, so iterate over it
for poly in line_poly:
var collision_shape = CollisionPolygon2D.new()
collision_shape.polygon = offset_line_points(line_center, poly)
# static_line.call_deferred("add_child", collision_shape)
static_line.add_child(collision_shape)

func partition_points_before_and_after(points, pos) -> Array:
Expand Down
21 changes: 19 additions & 2 deletions State.gd
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,33 @@ signal ice_updated(ice_remaining)
signal out_of_ice()

const STARTING_ICE = 100
const MELT_RATE = 1
const MELT_EVERY_N_SECONDS = 1
var ice_remaining = STARTING_ICE setget set_ice_remaining
var start_time = null
var should_tick = false

func set_ice_remaining(value):
var was_already_zero = ice_remaining == 0
ice_remaining = value
if ice_remaining < 0:
ice_remaining = 0

emit_signal("ice_updated", ice_remaining)
if ice_remaining == 0:
if ice_remaining == 0 and not was_already_zero:
emit_signal("out_of_ice")

func deduct_ice(amount):
set_ice_remaining(ice_remaining - amount)
set_ice_remaining(ice_remaining - amount)

func _ready():
start_time = OS.get_unix_time()
tick_forever()

func tick_forever():
while true:
yield(get_tree().create_timer(MELT_EVERY_N_SECONDS), "timeout")
if should_tick: deduct_ice(MELT_RATE)

func time_elapsed():
return OS.get_unix_time() - start_time
57 changes: 57 additions & 0 deletions UI/EndScreen.tscn
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
[gd_scene load_steps=6 format=2]

[ext_resource path="res://Fonts/dogicapixel.ttf" type="DynamicFontData" id=1]
[ext_resource path="res://UI/WinScreen.gd" type="Script" id=2]

[sub_resource type="DynamicFont" id=1]
size = 50
font_data = ExtResource( 1 )

[sub_resource type="DynamicFontData" id=3]
font_path = "res://Fonts/dogicapixel.ttf"

[sub_resource type="DynamicFont" id=2]
size = 10
font_data = SubResource( 3 )

[node name="EndScreen" type="CanvasLayer"]
script = ExtResource( 2 )

[node name="Title" type="Label" parent="."]
anchor_left = 0.5
anchor_top = 0.5
anchor_right = 0.5
anchor_bottom = 0.5
margin_left = -300.0
margin_top = -90.0
margin_right = 299.0
margin_bottom = -52.0
custom_fonts/font = SubResource( 1 )
text = "You Win!"
align = 1

[node name="IceRemaining" type="Label" parent="."]
anchor_left = 0.5
anchor_top = 0.5
anchor_right = 0.5
anchor_bottom = 0.5
margin_left = -300.0
margin_top = -7.0
margin_right = 300.0
margin_bottom = 7.0
custom_fonts/font = SubResource( 2 )
text = "Ice Remaining: 10%"
align = 1

[node name="TotalTime" type="Label" parent="."]
anchor_left = 0.5
anchor_top = 0.5
anchor_right = 0.5
anchor_bottom = 0.5
margin_left = -300.0
margin_top = 24.0
margin_right = 300.0
margin_bottom = 38.0
custom_fonts/font = SubResource( 2 )
text = "Total Time: 30 seconds"
align = 1
11 changes: 1 addition & 10 deletions UI/IceBarHud.tscn
Original file line number Diff line number Diff line change
@@ -1,23 +1,15 @@
[gd_scene load_steps=5 format=2]
[gd_scene load_steps=3 format=2]

[ext_resource path="res://UI/IceBar.tscn" type="PackedScene" id=1]
[ext_resource path="res://UI/IceBarHud.gd" type="Script" id=2]

[sub_resource type="DynamicFontData" id=1]
font_path = "res://Fonts/dogicapixel.ttf"

[sub_resource type="DynamicFont" id=2]
size = 8
font_data = SubResource( 1 )

[node name="IceBarHud" type="CanvasLayer"]
script = ExtResource( 2 )

[node name="IceBar" parent="." instance=ExtResource( 1 )]
margin_top = 383.0
margin_right = 600.0
margin_bottom = 400.0
rect_min_size = Vector2( 32, 14 )

[node name="Label" type="Label" parent="IceBar"]
anchor_top = 0.5
Expand All @@ -26,5 +18,4 @@ margin_left = 4.0
margin_top = -4.0
margin_right = 83.0
margin_bottom = 3.0
custom_fonts/font = SubResource( 2 )
text = "ice remaining"
15 changes: 15 additions & 0 deletions UI/WinScreen.gd
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
extends CanvasLayer

onready var ice_remaining = $IceRemaining
onready var total_time = $TotalTime
onready var title = $Title

func _ready():
# This should have grades so that folks feel a desire to replay
if State.ice_remaining > 0:
title.text = "You Win!"
else:
title.text = "You Lose :("

ice_remaining.text = "Ice Remaining: %d%%" % int( 100 * float(State.ice_remaining) / State.STARTING_ICE)
total_time.text = "Total Time: %d seconds" % int(State.time_elapsed())
5 changes: 5 additions & 0 deletions project.godot
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,14 @@ window/size/test_height=800
window/stretch/mode="2d"
window/stretch/aspect="keep"

[global]

font=false

[gui]

common/drop_mouse_on_gui_input_disabled=true
theme/custom_font="res://Fonts/DefaultFont.tres"

[importer_defaults]

Expand Down

0 comments on commit 3e7fea0

Please sign in to comment.