Add package loading so not everything is defined in __main__ anymore
Signed-off-by: Alek Ratzloff <alekratz@gmail.com>
This commit is contained in:
@@ -1,107 +1,26 @@
|
|||||||
from agame.action import *
|
import argparse
|
||||||
from agame.game import *
|
import importlib.util
|
||||||
from agame.item import *
|
from pathlib import Path
|
||||||
from agame.room import *
|
import sys
|
||||||
from agame.trigger import *
|
|
||||||
|
|
||||||
|
|
||||||
# TODO - take a custom module name that has:
|
# Parse args
|
||||||
# .database
|
parser = argparse.ArgumentParser(
|
||||||
# .game
|
description="Run a game",
|
||||||
# so that you can just RUN it
|
|
||||||
# Something similar to wsgi using "app" in the passed module
|
|
||||||
|
|
||||||
|
|
||||||
# This is the *game* here
|
|
||||||
database = Database()
|
|
||||||
database.add_items(
|
|
||||||
Item(
|
|
||||||
id="glowing_rock",
|
|
||||||
name="glowing rock",
|
|
||||||
desc="This rock is glowing.",
|
|
||||||
synonyms=("rock",),
|
|
||||||
room_desc="You see a ((glowing rock)). You have **got** to have it.",
|
|
||||||
triggers={
|
|
||||||
GET: [
|
|
||||||
PrintAction(
|
|
||||||
"You try to pick up the rock, but it slips out of your greasy hands.",
|
|
||||||
"Maybe you should wash your hands, you disgusting little man.",
|
|
||||||
)
|
|
||||||
],
|
|
||||||
LOOK: [PrintAction("Man, that rock looks awesome.")],
|
|
||||||
},
|
|
||||||
),
|
|
||||||
Item(
|
|
||||||
id="cell_door",
|
|
||||||
name="door",
|
|
||||||
room_desc="A ((door)) sits on the far wall.",
|
|
||||||
triggers={
|
|
||||||
GET: [PrintAction("The door is pretty attached to its wall.")],
|
|
||||||
OPEN: [
|
|
||||||
CheckVarAction(
|
|
||||||
"cell_door_open",
|
|
||||||
Compare.EQUALS,
|
|
||||||
True,
|
|
||||||
yes=[
|
|
||||||
PrintAction(
|
|
||||||
"It's already open. You push on the door even //more//, just in case."
|
|
||||||
),
|
|
||||||
SleepAction(1.0),
|
|
||||||
PrintAction("..."),
|
|
||||||
SleepAction(1.0),
|
|
||||||
PrintAction("Yup, still open."),
|
|
||||||
],
|
|
||||||
no=[
|
|
||||||
SetVarAction("cell_door_open", True),
|
|
||||||
PrintAction("The door swings open, thanks to you."),
|
|
||||||
],
|
|
||||||
)
|
|
||||||
],
|
|
||||||
CLOSE: [
|
|
||||||
CheckVarAction(
|
|
||||||
"cell_door_open",
|
|
||||||
Compare.EQUALS,
|
|
||||||
True,
|
|
||||||
yes=[
|
|
||||||
PrintAction("You close that door. Nice job."),
|
|
||||||
SetVarAction("cell_door_open", False),
|
|
||||||
],
|
|
||||||
no=[PrintAction("The door is already closed.")],
|
|
||||||
)
|
|
||||||
],
|
|
||||||
LOOK: [
|
|
||||||
CheckVarAction(
|
|
||||||
"cell_door_open",
|
|
||||||
Compare.EQUALS,
|
|
||||||
True,
|
|
||||||
yes=[PrintAction("It's a door, wide open, because you opened it.")],
|
|
||||||
no=[PrintAction("A closed door. You can change this.")],
|
|
||||||
)
|
|
||||||
],
|
|
||||||
},
|
|
||||||
),
|
|
||||||
)
|
)
|
||||||
database.add_rooms(
|
parser.add_argument(
|
||||||
Room(
|
"PACKAGE", type=str, help="The Python package that contains the game to run."
|
||||||
id="start",
|
|
||||||
name="Test room",
|
|
||||||
desc="You're in ((Todd's Test Cell)).",
|
|
||||||
items=[
|
|
||||||
database.items["glowing_rock"].create_inst(),
|
|
||||||
database.items["cell_door"].create_inst(),
|
|
||||||
],
|
|
||||||
),
|
|
||||||
)
|
)
|
||||||
|
args = parser.parse_args()
|
||||||
|
|
||||||
# Build the game state
|
# Get package to run
|
||||||
game = Game(
|
package = args.PACKAGE
|
||||||
database=database,
|
|
||||||
room=database.rooms["start"],
|
|
||||||
vars={
|
|
||||||
"cell_door_open": False,
|
|
||||||
},
|
|
||||||
)
|
|
||||||
|
|
||||||
|
# Load game module
|
||||||
|
game_module = importlib.import_module(package)
|
||||||
|
game = game_module.game # type: ignore
|
||||||
|
|
||||||
|
# Run
|
||||||
game.print_room()
|
game.print_room()
|
||||||
while True:
|
while True:
|
||||||
try:
|
try:
|
||||||
|
|||||||
95
examplegame/__init__.py
Normal file
95
examplegame/__init__.py
Normal file
@@ -0,0 +1,95 @@
|
|||||||
|
from agame.game import Game, Database
|
||||||
|
from agame.item import Item
|
||||||
|
from agame.room import Room
|
||||||
|
from agame.action import *
|
||||||
|
from agame.trigger import *
|
||||||
|
|
||||||
|
# This is the *game* here
|
||||||
|
database = Database()
|
||||||
|
database.add_items(
|
||||||
|
Item(
|
||||||
|
id="glowing_rock",
|
||||||
|
name="glowing rock",
|
||||||
|
desc="This rock is glowing.",
|
||||||
|
synonyms=("rock",),
|
||||||
|
room_desc="You see a ((glowing rock)). You have **got** to have it.",
|
||||||
|
triggers={
|
||||||
|
GET: [
|
||||||
|
PrintAction(
|
||||||
|
"You try to pick up the rock, but it slips out of your greasy hands.",
|
||||||
|
"Maybe you should wash your hands, you disgusting little man.",
|
||||||
|
)
|
||||||
|
],
|
||||||
|
LOOK: [PrintAction("Man, that rock looks awesome.")],
|
||||||
|
},
|
||||||
|
),
|
||||||
|
Item(
|
||||||
|
id="cell_door",
|
||||||
|
name="door",
|
||||||
|
room_desc="A ((door)) sits on the far wall.",
|
||||||
|
triggers={
|
||||||
|
GET: [PrintAction("The door is pretty attached to its wall.")],
|
||||||
|
OPEN: [
|
||||||
|
CheckVarAction(
|
||||||
|
"cell_door_open",
|
||||||
|
Compare.EQUALS,
|
||||||
|
True,
|
||||||
|
yes=[
|
||||||
|
PrintAction(
|
||||||
|
"It's already open. You push on the door even //more//, just in case."
|
||||||
|
),
|
||||||
|
SleepAction(1.0),
|
||||||
|
PrintAction("..."),
|
||||||
|
SleepAction(1.0),
|
||||||
|
PrintAction("Yup, still open."),
|
||||||
|
],
|
||||||
|
no=[
|
||||||
|
SetVarAction("cell_door_open", True),
|
||||||
|
PrintAction("The door swings open, thanks to you."),
|
||||||
|
],
|
||||||
|
)
|
||||||
|
],
|
||||||
|
CLOSE: [
|
||||||
|
CheckVarAction(
|
||||||
|
"cell_door_open",
|
||||||
|
Compare.EQUALS,
|
||||||
|
True,
|
||||||
|
yes=[
|
||||||
|
PrintAction("You close that door. Nice job."),
|
||||||
|
SetVarAction("cell_door_open", False),
|
||||||
|
],
|
||||||
|
no=[PrintAction("The door is already closed.")],
|
||||||
|
)
|
||||||
|
],
|
||||||
|
LOOK: [
|
||||||
|
CheckVarAction(
|
||||||
|
"cell_door_open",
|
||||||
|
Compare.EQUALS,
|
||||||
|
True,
|
||||||
|
yes=[PrintAction("It's a door, wide open, because you opened it.")],
|
||||||
|
no=[PrintAction("A closed door. You can change this.")],
|
||||||
|
)
|
||||||
|
],
|
||||||
|
},
|
||||||
|
),
|
||||||
|
)
|
||||||
|
database.add_rooms(
|
||||||
|
Room(
|
||||||
|
id="start",
|
||||||
|
name="Test room",
|
||||||
|
desc="You're in ((Todd's Test Cell)).",
|
||||||
|
items=[
|
||||||
|
database.items["glowing_rock"].create_inst(),
|
||||||
|
database.items["cell_door"].create_inst(),
|
||||||
|
],
|
||||||
|
),
|
||||||
|
)
|
||||||
|
|
||||||
|
# Build the game state
|
||||||
|
game = Game(
|
||||||
|
database=database,
|
||||||
|
room=database.rooms["start"],
|
||||||
|
vars={
|
||||||
|
"cell_door_open": False,
|
||||||
|
},
|
||||||
|
)
|
||||||
Reference in New Issue
Block a user