Skip to content

Commit

Permalink
Add year constraint support to stories
Browse files Browse the repository at this point in the history
  • Loading branch information
squarepear committed Feb 4, 2024
1 parent 3f7c54d commit d5bb23b
Show file tree
Hide file tree
Showing 8 changed files with 52 additions and 32 deletions.
8 changes: 4 additions & 4 deletions project/common/year.gd
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
class_name Year

enum {
Freshman = 0,
Sophomore = 1,
Junior = 2,
Senior = 3,
Freshman,
Sophomore,
Junior,
Senior,
}


Expand Down
2 changes: 2 additions & 0 deletions project/story/starting_stories/big_vs_small.gd
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ extends SimpleStory

var text := '"I\'ve been thinking about college. Do you think it\'s better for me to go a [u]big[/u] school or [u]small[/u] school?"'

var years := Year.not_in([ Year.Freshman ])

var options := {
"Bigger is better": {
"text": '"Yeah, more people, more clubs, more opportunities. Thanks!"',
Expand Down
2 changes: 2 additions & 0 deletions project/story/starting_stories/liberal_arts.gd
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ var text := [
'"I liked some of the smaller liberal arts schools I visited, but I also liked the engineering program at the big state university."'
]

var years := [ Year.Junior, Year.Senior ]

var options := {
"Study liberal arts!": {
"text": [
Expand Down
2 changes: 1 addition & 1 deletion project/test/integration/simple_story_validity_test.gd
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ func test_all_story_year_constraints_are_valid():
if "years" in story:
for year in story.years:
assert_true(year is int, "Story %s year %s is an integer" % [story_path, year])
assert_true(Year.keys().has(year), "Story %s year %s is a valid year" % [story_path, year])
assert_true(Year.values().has(year), "Story %s year %s is a valid year" % [story_path, year])


func test_locations():
Expand Down
2 changes: 1 addition & 1 deletion project/ui/main_scene.gd
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ func _run_next_story() -> void:


func _draw_random_story() -> String:
var story_path : String = world.available_stories.pick_random()
var story_path : String = world.get_active_stories().pick_random()
world.available_stories.erase(story_path)
return story_path

Expand Down
29 changes: 7 additions & 22 deletions project/ui/year_indicator.gd
Original file line number Diff line number Diff line change
Expand Up @@ -4,31 +4,16 @@ var world: World:
set(value):
if world != null:
if world != value:
world.turns_changed.disconnect(_update_display)
world.years_changed.disconnect(_update_display)

world = value
world.turns_changed.connect(_update_display)
_update_display(world.turns)
world.years_changed.connect(_update_display)
_update_display(world.years)

@onready var _year_name: Label = %YearName
@onready var _year_circles : Array[ProgressCircle] = [%Year1, %Year2, %Year3, %Year4 ]


func _lookup_year_name(percent_complete: float) -> String:
if percent_complete < 0.25:
_year_circles[0].year_passed = true
return Year.keys()[0]
elif percent_complete < 0.5:
_year_circles[1].year_passed = true
return Year.keys()[1]
elif percent_complete < 0.75:
_year_circles[2].year_passed = true
return Year.keys()[2]
else:
_year_circles[3].year_passed = true
return Year.keys()[3]


func _update_display(turns: int) -> void:
var percent_complete: float = turns / (world.turns_per_year * 4.0)
_year_name.text = _lookup_year_name(percent_complete)
func _update_display(years: int) -> void:
_year_circles[years].year_passed = true
_year_name.text = Year.keys()[years]
7 changes: 7 additions & 0 deletions project/world/simple_story.gd
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,13 @@ func run(presenter) -> void:
presenter.world.end_stories.append(new_ending_possibility)


func is_active(world: World) -> bool:
if "years" in self and not get("years").has(world.years):
return false

return true


func _apply_effects(effects:Dictionary, world:World) -> void:
for attribute_name in effects.keys():
world.character[attribute_name].value += effects[attribute_name]
Expand Down
32 changes: 28 additions & 4 deletions project/world/world.gd
Original file line number Diff line number Diff line change
@@ -1,21 +1,22 @@
## The state of the game world
class_name World extends RefCounted

signal turns_changed(new_turn : int)
signal turns_changed(new_turn: int)
signal years_changed(new_year: int)

## The maximum number of turns that can be taken in a year
const MAX_TURNS_PER_YEAR = 3
const MAX_TURNS_PER_YEAR := 3

## The current character whose story is being played
var character := Character.new()

## The stories that are available to draw from when a new one is needed
##
## The values are resource paths.
var available_stories : Array[String] = []
var available_stories: Array[String] = []

## The end stories that have been added to the world
var end_stories : Array[String] = []
var end_stories: Array[String] = []

## The cast of characters as a map from name to npc object
var cast := Cast.new()
Expand All @@ -32,8 +33,20 @@ var turns_per_year := MAX_TURNS_PER_YEAR
var turns := 0:
set(value):
turns = value

if turns >= turns_per_year:
turns = 0
years += 1

turns_changed.emit(turns)

var years := 0:
set(value):
years = value

if years < 4:
years_changed.emit(years)


## Add all the stories in the given directory to the list
## of available stories
Expand Down Expand Up @@ -65,3 +78,14 @@ func remove_stories(dir:DirAccess) -> Array[String]:
else:
push_error("Tried to remove non-existing story path: %s" % file_path)
return results


func get_active_stories() -> Array[String]:
return available_stories.filter(func(story_path: String) -> bool:
var story = (load(story_path) as RefCounted).new()

if not "is_active" in story:
return true

return story.is_active(self)
)

0 comments on commit d5bb23b

Please sign in to comment.