From e981de8d9da4edea3b0d6d8f23476acefa1665fc Mon Sep 17 00:00:00 2001 From: Zahkc Date: Tue, 11 Jul 2023 02:22:21 +1000 Subject: [PATCH] 0.9-Alpha --- Data/Profiles/BOTORU.json | 1 + Data/Profiles/MUSH.json | 1 + Save/current.json | 2 +- Save/settings.json | 1 + Sprites/Characters/MUSH/ADULT/BODY.bmp | Bin 0 -> 5358 bytes code.py | 91 ++++++++++++++++++------- lib/animate.py | 19 ++++++ lib/entity.py | 53 ++++++++++++++ lib/menu.py | 2 +- lib/selector.py | 73 +++++++++----------- 10 files changed, 177 insertions(+), 66 deletions(-) create mode 100644 Data/Profiles/BOTORU.json create mode 100644 Data/Profiles/MUSH.json create mode 100644 Save/settings.json create mode 100644 Sprites/Characters/MUSH/ADULT/BODY.bmp create mode 100644 lib/animate.py create mode 100644 lib/entity.py diff --git a/Data/Profiles/BOTORU.json b/Data/Profiles/BOTORU.json new file mode 100644 index 0000000..123d4b4 --- /dev/null +++ b/Data/Profiles/BOTORU.json @@ -0,0 +1 @@ +{"spritePath": "/Sprites/Characters/BOTORU/ADULT/BODY.BMP", "joyMult": 1.0, "name": "BOTORU", "respectMult": 1.0, "baseJoy": 1, "evolutionInterval": 100, "baseRespect": 1, "lifespan": 1000} diff --git a/Data/Profiles/MUSH.json b/Data/Profiles/MUSH.json new file mode 100644 index 0000000..78f77eb --- /dev/null +++ b/Data/Profiles/MUSH.json @@ -0,0 +1 @@ +{"spritePath": "/Sprites/Characters/MUSH/ADULT/BODY.BMP", "joyMult": 1.0, "name": "MUSH", "respectMult": 1.0, "baseJoy": 1, "evolutionInterval": 100, "baseRespect": 1, "lifespan": 1000} diff --git a/Save/current.json b/Save/current.json index b204901..19cfd46 100644 --- a/Save/current.json +++ b/Save/current.json @@ -1 +1 @@ -TestTestTake 2Take 2Take 2Take 2 \ No newline at end of file +{"profile" : "BOTORU", "age" : 205, "joy" : 2, "respect" : 3, "care" : 2, "money" : 5, "nextEventTime" : 1000, "nextEvloutionTime" : 1500, "death" : 2000} diff --git a/Save/settings.json b/Save/settings.json new file mode 100644 index 0000000..97a880d --- /dev/null +++ b/Save/settings.json @@ -0,0 +1 @@ +{"timeout": 1500} diff --git a/Sprites/Characters/MUSH/ADULT/BODY.bmp b/Sprites/Characters/MUSH/ADULT/BODY.bmp new file mode 100644 index 0000000000000000000000000000000000000000..22dd0ef4c8009c6f56d7160b065ad0a63bb9a323 GIT binary patch literal 5358 zcmeH~OKuZE5QfX6VIM0TU}W}4mM<`hMq-78khlRiV9NoJSa1W@Lr=Krhu{TPo? z2%D7Re%9Aj^>^D+9^U_cEowi}e#Pgr`|M=Y^$*y8^VUAEu)>eX#sBH-;oZkCI=G=3 z8oob1eV1>)9_4bm$oYJhzyMV`-VyRYu2D&{H!m-c51-HM5us zW6+{duB8E7R5@h|e@?(?QXJw#R+?%O>j3}5m}oE2v1XicPyGounblKSQF48iw&2yZClX1CdTIb`H- z7+k?r<)#|i_qnbJrI4Y}u$Y_OO{X3*!8Ht&$Hq`cvDN4qM}TH|QYHvc)Pp!Ztp{8k zyv?%Q2(|0MVWUSf-SoG;#{?qR8p}8jI#kkOwa?7T43v-0PkYn?+rj`ym@z@BdiZ*2 zINUQj?=zy$YCy+7&+*-)fwDv?0Ud8*a6wM%i9K+CLO)Jqa=EkA3Jp9n`EU;1z)UF1 zyipl#0|O}xff_ox&QQI`5Zc5z1m4PTVFCiJ8S@I`D~TBIDZL)mdPV~H?r z83dxU$9iup!e|w%#!BW3;%u{B? nT8+hEyN!wdEym?n1iHfH+s#>-Thpmnd|~Q4EhK7ApHH_xd$VZR literal 0 HcmV?d00001 diff --git a/code.py b/code.py index 377f64a..a9510fd 100644 --- a/code.py +++ b/code.py @@ -1,12 +1,17 @@ import time +import json import alarm import board import displayio -import digitalio import bitmaptools import adafruit_imageload +import storage +import supervisor +import microcontroller import menu +import entity +import animate import selector import buttonBus @@ -15,27 +20,59 @@ import buttonBus #setup board.DISPLAY.root_group.hidden = False board.DISPLAY.rotation = 90 -splash = displayio.Group() -board.DISPLAY.show(splash) + +try: + storage.remount("/", False) +except RuntimeError as E: + print("Failed Mount") + +root = displayio.Group() + +bgGroup = displayio.Group() +bgItemGroup = displayio.Group() +haioGroup = displayio.Group() +fgItemGroup = displayio.Group() +menuGroup = displayio.Group() +selectorGroup = displayio.Group() + +board.DISPLAY.show(root) + +entity = entity.entity() +entity.importSave() +entity.show(haioGroup) + bg, bgPallette = adafruit_imageload.load("/Sprites/Backgrounds/bg1.bmp", bitmap=displayio.Bitmap, palette=displayio.Palette) bgTile = displayio.TileGrid(bg, pixel_shader=bgPallette) +bgGroup.append(bgTile) -tama, tamaPallette = adafruit_imageload.load("/Sprites/Characters/MUSH/ADULT/BODY.BMP", bitmap=displayio.Bitmap, palette=displayio.Palette) -tamaPallette.make_transparent(3) -tamaTile = displayio.TileGrid(tama, pixel_shader=tamaPallette, x=20, y=100) +root.append(bgGroup) +root.append(bgItemGroup) +root.append(haioGroup) +root.append(fgItemGroup) +root.append(menuGroup) +root.append(selectorGroup) + +select = selector.selector([[5,0],[40,0],[70,0],[90,0],[0,220],[110,220]]) +select.showSelector(selectorGroup) +wakeTime = time.monotonic() -splash.append(bgTile) -splash.append(tamaTile) +#Load Settings +with open("/Save/settings.json", "r") as reader: + settings = json.loads(reader.read()) +timeoutAmount = settings["timeout"] +timeout = round(time.monotonic() + timeoutAmount, 1) -bounce = False -bounceAmount = -10 -selector.showSelector(splash) +nextWake = max(entity.character["nextEventTime"], entity.character["nextEvloutionTime"], entity.character["death"]) - entity.character["age"] #Menu Activites def do(x): + global wakeTime if x == 0: + entity.character["age"] += int(time.monotonic() - wakeTime) + wakeTime = time.monotonic() + print(entity.character["age"]) pass elif x == 1: pass @@ -44,33 +81,37 @@ def do(x): elif x == 3: pass elif x == 4: - menu.toggleMenu(splash) + menu.toggleMenu(root) elif x == 5: buttonBus.button1.deinit() pin_alarm = alarm.pin.PinAlarm(pin=board.D1, value=True, pull=True) time.sleep(0.5) alarm.exit_and_deep_sleep_until_alarms(pin_alarm) +def checkTimeout(): + # print("Time: " + str(round(time.monotonic(), 1)) + " Timeout: " + str(timeout)) + if round(time.monotonic(), 1) == timeout: + entity.character["age"] += int(time.monotonic() - wakeTime) + # entity.update() + buttonBus.button1.deinit() + pin_alarm = alarm.pin.PinAlarm(pin=board.D1, value=True, pull=True) + time.sleep(0.5) + alarm.exit_and_deep_sleep_until_alarms(pin_alarm) #System Loop while True: - - if(round(time.monotonic()%0.5, 1) == 0.5): - if bounce == False: - bounce = True - bounceAmount = bounceAmount * -1 - tamaTile.y += bounceAmount - else: - if bounce == True: - bounce = False - + checkTimeout() + animate.doAnimate(haioGroup, 0.5, 10) if buttonBus.getSelectedButton() == 0: - selector.lastPos() + select.lastPos() time.sleep(0.2) + timeout = round(time.monotonic() + timeoutAmount, 1) elif buttonBus.getSelectedButton() == 1: - do(selector.spos) + do(select.getSPos()) time.sleep(0.2) + timeout = round(time.monotonic() + timeoutAmount, 1) elif buttonBus.getSelectedButton() == 2: - selector.nextPos() + select.nextPos() time.sleep(0.2) + timeout = round(time.monotonic() + timeoutAmount, 1) diff --git a/lib/animate.py b/lib/animate.py new file mode 100644 index 0000000..c9cd1c0 --- /dev/null +++ b/lib/animate.py @@ -0,0 +1,19 @@ +import time +import displayio + +bounce = False +bounceAmount = 0 + +def doAnimate(haioGroup: displayio.Group, duration: float, newBounceAmount: int): + global bounce + global bounceAmount + if bounceAmount == 0: + bounceAmount = newBounceAmount + if(round(time.monotonic()%duration, 2) == round(duration, 2)): + if bounce == False: + bounce = True + bounceAmount = bounceAmount * -1 + haioGroup.y += bounceAmount + else: + if bounce == True: + bounce = False diff --git a/lib/entity.py b/lib/entity.py new file mode 100644 index 0000000..200d80f --- /dev/null +++ b/lib/entity.py @@ -0,0 +1,53 @@ +import json +import time +import storage +import displayio +import supervisor +import microcontroller +import adafruit_imageload + +class entity: + def __init__(self): + pass + + profile = { + "spritePath": str(), + "joyMult": float(), + "name": str(), + "respectMult": float(), + "baseJoy": int(), + "evolutionInterval": int(), + "baseRespect": int(), + "lifespan": int() + } + + + character = { + "profile": str(), #Profile to load + "age": int(), #Age in seconds + "joy": float(), #happy or sad + "respect": float(), #Good or Evil + "mistakes": int(), #How many mistakes made + "money": int(), #MONEY + "nextEventTime": int(), # seconds till event + "nextEvloutionTime": int(), # seconds till + "death": int() # seconds till death + } + + def importSave(self): + with open("/Save/current.json", "r") as save: + self.character = json.loads(save.read()) + with open("/Data/Profiles/" + self.character["profile"] + ".json", "r") as importedProfile: + self.profile = json.loads(importedProfile.read()) + + + def exportSave(self): + with open("/Save/current.json", "w") as save: + save.write(json.dumps(character)) + save.flush() + + def show(self, haioGroup: displayio.Group): + haio, haioPalette = adafruit_imageload.load(self.profile["spritePath"], bitmap=displayio.Bitmap, palette=displayio.Palette) + haioPalette.make_transparent(len(haioPalette)-1) + haioTile = displayio.TileGrid(haio, pixel_shader=haioPalette, x=20, y=100) + haioGroup.append(haioTile) diff --git a/lib/menu.py b/lib/menu.py index 4fd5f2e..342380f 100644 --- a/lib/menu.py +++ b/lib/menu.py @@ -15,7 +15,7 @@ def toggleMenu(splash): if menuToggle: battery.reset() time.sleep(0.05) - timeText = bitmap_label.Label(terminalio.FONT, text="Time: " + str(time.time() - 946684800), scale=1, color=0x000000, x=25, y=60) + timeText = bitmap_label.Label(terminalio.FONT, text="Time: " + str(time.monotonic()), scale=1, color=0x000000, x=25, y=60) batText = bitmap_label.Label(terminalio.FONT, text="Battery: " + str(round(battery.cell_percent)) + "%", scale=1, color=0x000000, x=25, y=80) menuGroup = displayio.Group() menuGroup.append(Rect(10, 10, 115, 220, fill=0xFFFFFF, outline=0x000000, stroke=2)) diff --git a/lib/selector.py b/lib/selector.py index dfd4c7d..0f63515 100644 --- a/lib/selector.py +++ b/lib/selector.py @@ -1,47 +1,42 @@ from adafruit_display_shapes.circle import Circle -spos = -1 select = Circle(15, 10, 10, outline=0x000000, stroke=2) +spos = -1 -def showSelector(splash): - global spos - if spos == -1: - splash.append(select) - spos += 1 - print("added") +class selector: -def hideSelector(splash): - global spos - if spos != -1: - splash.pop() + def __init__(self, positions): + self.size = len(positions) + self.positions = positions -def nextPos(): - global spos - spos = (spos + 1) % 6 - updatePos() + def showSelector(self, selectorGroup): + global spos + if spos == -1: + selectorGroup.insert(0,select) + spos += 1 -def lastPos(): - global spos - spos = (spos - 1) % 6 - updatePos() + def hideSelector(self, selectorGroup): + global spos + if spos != -1: + selectorGroup.pop() -def updatePos(): - global select - if spos == 0: - select.x = 5 - select.y = 0 - elif spos == 1: - select.x = 40 - select.y = 0 - elif spos == 2: - select.x = 70 - select.y = 0 - elif spos == 3: - select.x = 90 - select.y = 0 - elif spos == 4: - select.x = 0 - select.y = 220 - elif spos == 5: - select.x = 110 - select.y = 220 + def getSPos(self): + return spos + + def getSize(self): + return self.size + + def nextPos(self): + global spos + spos = (spos + 1) % self.size + self.updatePos() + + def lastPos(self): + global spos + spos = (spos - 1) % self.size + self.updatePos() + + def updatePos(self): + global select + select.x = self.positions[spos][0] + select.y = self.positions[spos][1]