Skip to content

Commit

Permalink
finish the saving system
Browse files Browse the repository at this point in the history
  • Loading branch information
LupaDevStudio committed Dec 13, 2023
1 parent 4929193 commit 5803746
Show file tree
Hide file tree
Showing 6 changed files with 76 additions and 21 deletions.
1 change: 1 addition & 0 deletions screens/game.kv
Original file line number Diff line number Diff line change
Expand Up @@ -419,6 +419,7 @@
release_function: root.display_resources_help

ImageWithTextButton:
id: back_button
source: PATH_IMAGES + "back_arrow.png"
size_hint: BACK_ARROW_SIZE, BACK_ARROW_SIZE*0.6
pos_hint: {"x":0.025, "center_y": 0.08}
Expand Down
69 changes: 55 additions & 14 deletions screens/game.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,9 +75,6 @@ class GameScreen(ImprovedScreen):
weapons_value = StringProperty("0")
tools_value = StringProperty("0")

# Day counter
counter_day = 0

# Boolean indicating if the current moment is an answer or not
is_answer = False

Expand Down Expand Up @@ -127,6 +124,8 @@ def __init__(self, **kw):
self.credit: int
self.help_mode = False

self.game_mode = "new"

def preload(self, *_):

if not self.is_loaded:
Expand Down Expand Up @@ -194,26 +193,30 @@ def enable_cards(self, cards_list):
def on_pre_enter(self, *args):

super().on_pre_enter(*args)
self.disable_widget("back_button")

if not self.help_mode:

# Load the gameplay json
game.load_resources()
game.reset_variables()
if self.game_mode == "new":
game.reset_variables()
# Hide all cards
self.hide_cards()
else:
self.load_game()
self.update_display_resources()

# Hide all cards
self.hide_cards()

def on_enter(self, *args):

if not self.help_mode:

# Start the game music
music_mixer.play("game_music", loop=True)

# Launch the start day function
Clock.schedule_once(self.start_day)
if self.game_mode == "new":
# Launch the start day function
Clock.schedule_once(self.start_day)

# Load an add
if platform == "android":
Expand All @@ -227,8 +230,6 @@ def on_enter(self, *args):
# Allocate the number of credits
self.credit = 1

# Reset the number of days
self.counter_day = 0
else:
self.help_mode = False

Expand All @@ -238,6 +239,8 @@ def display_card(self, *_):
"""
Load and display the cards depending on the game phase.
"""
# Increase the counter of days
self.ids.day_indicator.text = TEXT.game["day"] + str(game.day)
if game.phase == "decision":
self.ids["decision_center"].text = game.text_dict["card"]
self.ids["decision_no"].text = game.text_dict["left"]
Expand Down Expand Up @@ -275,6 +278,7 @@ def choose_answer(self, choice: Literal["left", "right", "down"], *_):
game.make_choice(choice=choice)
game.end_day()
self.display_answer()
self.disable_widget("back_button")
else:
if choice == "right":
self.play_ads()
Expand Down Expand Up @@ -338,9 +342,7 @@ def start_day(self, *_):
"""
Start a new day with a new batch of cards.
"""
# Increase the counter of days
self.counter_day += 1
self.ids.day_indicator.text = TEXT.game["day"] + str(self.counter_day)
self.enable_widget("back_button")

if not game.game_over:
# Hide the cards
Expand All @@ -354,7 +356,10 @@ def start_day(self, *_):

# Set the day background
self.set_back_image_texture(self.day_camp_background.image.texture)

self.save_game()
else:
self.disable_widget("back_button")
self.update_display_resources()
if self.credit > 0:
self.show_pre_game_over()
Expand Down Expand Up @@ -414,3 +419,39 @@ def display_resources_help(self):

def go_to_menu(self):
self.manager.current = "menu"

def save_game(self):
save_dict = {}
save_dict["card_id"] = game.card_id
save_dict["phase"] = game.phase
save_dict["day"] = game.day
save_dict["food"] = game.food
save_dict["weapons"] = game.weapons
save_dict["tools"] = game.tools
save_dict["military"] = game.military
save_dict["civilian"] = game.civilian
save_dict["paleo"] = game.paleo
save_dict["order"] = game.order
USER_DATA.saved_data = save_dict
USER_DATA.save_changes()

def load_game(self):
save_dict = USER_DATA.saved_data
game.card_id = save_dict["card_id"]
game.phase = save_dict["phase"]
game.day = save_dict["day"]
game.food = save_dict["food"]
game.weapons = save_dict["weapons"]
game.tools = save_dict["tools"]
game.military = save_dict["military"]
game.civilian = save_dict["civilian"]
game.paleo = save_dict["paleo"]
game.order = save_dict["order"]
game.extract_texts()
self.hide_cards()
self.enable_widget("back_button")
self.display_card()

def reset_variables(self):
game.reset_variables()
self.start_day()
4 changes: 4 additions & 0 deletions screens/game_over.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,10 @@ def on_enter(self, *args):
# Play the game over music
music_mixer.play("time_of_the_apocalypse")

# Clear the saved game data
USER_DATA.saved_data = None
USER_DATA.save_changes()

# Display something when getting a new highscore
return super().on_enter(*args)

Expand Down
11 changes: 6 additions & 5 deletions screens/intermediate_menu.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,13 +46,14 @@ def __init__(self, **kw):
**kw)
self.other_screens_loaded = False

def on_enter(self, *args):
def on_pre_enter(self, *args):

# Set the labels text
self.start_label_text = TEXT.menu["new_game"]
self.continue_label_text = TEXT.menu["continue_game"]
if USER_DATA.saved_data is not None:
self.continue_label_text = TEXT.menu["continue_game"]

return super().on_enter(*args)
return super().on_pre_enter(*args)

def go_to_menu(self):
"""
Expand All @@ -74,8 +75,8 @@ def start_game(self, mode):
-------
None
"""
# PAUL
print(mode)
self.manager.get_screen("game").game_mode = mode

if USER_DATA.tutorial:
self.manager.current = "tutorial"
else:
Expand Down
5 changes: 5 additions & 0 deletions tools/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,10 @@ def __init__(self) -> None:
self.tutorial = data["tutorial"]
self.music_volume = data["music_volume"]
self.sound_effects_volume = data["sound_effects_volume"]
if "saved_data" not in data:
self.saved_data = None
else:
self.saved_data = data["saved_data"]

def save_changes(self) -> None:
"""
Expand All @@ -128,6 +132,7 @@ def save_changes(self) -> None:
data["tutorial"] = self.tutorial
data["music_volume"] = self.music_volume
data["sound_effects_volume"] = self.sound_effects_volume
data["saved_data"] = self.saved_data

# Save this dictionary
save_json_file(
Expand Down
7 changes: 5 additions & 2 deletions tools/postrias.py
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,9 @@ def start_day(self):

# Draw the card corresponding to the phase
self.card_id = self.draw(self.phase)
self.extract_texts()

def extract_texts(self):

# Extract the texts to display
if self.phase == "decree":
Expand Down Expand Up @@ -300,9 +303,9 @@ def make_choice(self, choice: Literal["left", "down", "right"]):
elif self.phase == "decision":
if choice in ["yes", "no"]:
list_text = TEXT.answer[choice][self.gameplay["decision"]
[self.card_id]["complainant"]]
[self.card_id]["complainant"]]
self.text_dict["answer"] = random.choice(list_text)

else:
self.text_dict["answer"] = TEXT.answer[choice]

Expand Down

0 comments on commit 5803746

Please sign in to comment.