Skip to content

Commit

Permalink
refactor(examples): tidy drag_and_drop_inventory.rpy
Browse files Browse the repository at this point in the history
  • Loading branch information
remarkablemark committed Aug 31, 2024
1 parent 5a20bc1 commit 7ace782
Showing 1 changed file with 25 additions and 35 deletions.
60 changes: 25 additions & 35 deletions game/examples/drag_and_drop_inventory.rpy
Original file line number Diff line number Diff line change
@@ -1,28 +1,26 @@
# https://discover-with-mia.itch.io/renpy-tutorial-drag-and-drop-inventory
init python:

# update the expression of a given character based on preference for a particular item.
def character_expression(character, item) -> None:
def get_character_expression(character, item) -> str:
global characters
if item == characters[character]["preferences"]:
characters[character]["expression"] = "happy"
return "happy"
else:
characters[character]["expression"] = "normal"
return "normal"

# check if there are items in the inventory currently available for use.
def items_are_available() -> bool:
return any(item["available"] for item in inventory.values())

# select the first available item from the inventory as a default choice.
def select_default_item() -> tuple[any, any]:
for item_id, item_info in inventory.items():
if item_info["available"]:
return item_id, item_info
return None, None

# handle the drag and drop interaction. Update global state when an item is dropped onto a character.
# handle the drag and drop interaction
# update global state when an item is dropped onto a character
def character_dragged(drags, drop) -> bool:
global currentCharacter, lastGivenItem
global current_character, last_given_item

if not drop:
return False
Expand All @@ -31,10 +29,7 @@ init python:
character = drop.drag_name

if character in characters and item in inventory and inventory[item]["available"]:
lastGivenItem = item

character_expression(character, item)

last_given_item = item
inventory[item]["available"] = False
return True

Expand Down Expand Up @@ -64,18 +59,16 @@ default characters = {
"normal": "sylvie/sylvie blue normal.png",
"happy": "sylvie/sylvie blue smile.png",
"preferences": "gem",
"expression": "normal",
},
"Eileen": {
"normal": "eileen/eileen happy.png",
"happy": "eileen/eileen vhappy.png",
"preferences": "book",
"expression": "normal",
},
}

default currentCharacter = "Sylvie"
default lastGivenItem = None
default current_character = "Sylvie"
default last_given_item = None

default selected_info = ""
default selected_item = ""
Expand Down Expand Up @@ -109,7 +102,12 @@ screen interact_with_characters():
vbox:
for item_id, item_info in inventory.items():
if item_info["available"]:
textbutton f"{item_info['name']} - {item_info['description']}" action [SetVariable('selected_item', item_id), SetVariable('selected_info', item_info)]
textbutton f"{item_info['name']} - {item_info['description']}":
action [
SetVariable('selected_item', item_id),
SetVariable('selected_info', item_info),
]

else:
text "??? - ???"

Expand All @@ -124,37 +122,29 @@ screen interact_with_characters():
image selected_info["image"]

drag:
drag_name currentCharacter
drag_name current_character
draggable False
xalign 0.5
yalign 1.0
image characters[currentCharacter][characters[currentCharacter]["expression"]]
image characters[current_character]["normal"]

label drag_and_drop_inventory:

$ selected_item, selected_info = select_default_item()
$ item_given = False

call screen interact_with_characters

if lastGivenItem:
$ character_expression(currentCharacter, lastGivenItem)
$ expression = characters[currentCharacter]["expression"]
if last_given_item:
$ character_expression = get_character_expression(current_character, last_given_item)
$ character_image = characters[current_character][character_expression]

$ character_image = characters[currentCharacter][expression]
if character_expression == "happy":
show expression character_image
"[current_character] is happy with her favorite gift."

if expression == "happy":
show expression character_image at center
"[currentCharacter] is happy with her favorite gift."
else:
show expression character_image at center
"[currentCharacter]: 'Thank you, but this isn't what I prefer.'"

$ item_given = True

jump suite_story

label suite_story:
show expression character_image
"[current_character]: 'Thank you, but this isn't what I prefer.'"

while items_are_available():
jump drag_and_drop_inventory
Expand Down

0 comments on commit 7ace782

Please sign in to comment.