marmots/world.py

82 lines
3 KiB
Python

# This file is part of MarMOTS.
#
# MarMOTS is free software: you can redistribute it and/or modify it under the terms of the GNU Affero
# General Public License as published by the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# MarMOTS is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the
# implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General
# Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License along with MarMOTS. If not,
# see <https://www.gnu.org/licenses/>.
#
# Copyright 2020 Jeremy Penner
import ansi_cython as ansi
from scripting import Vm
from basetoken import AutoJoiner, AutoKiller, MsgScroller
from engine import Game, TokenClient
from datetime import datetime
import traceback
class Player(TokenClient):
def InitPersistent(self, owner, client):
TokenClient.InitPersistent(self, owner, client, "drawable", "player")
self.rbot = self.game.vm.AddPlayer()
self.vm = self.game.vm
def die(self):
self.vm.RemovePlayer(self.rbot)
TokenClient.die(self)
def run(self):
while True:
key = self.EvKey().receive(self)
posNew = self.rbot.Pos().Clone()
if key == ansi.K_UP:
posNew.Up()
elif key == ansi.K_DOWN:
posNew.Down()
elif key == ansi.K_LEFT:
posNew.Left()
elif key == ansi.K_RIGHT:
posNew.Right()
if not posNew.Equals(self.rbot.Pos()):
# is there an rbot here?
rgrbotCollide = [rbot for rbot in self.vm.Rgrbot() if rbot != self.rbot and rbot.Pos().Equals(posNew)]
if len(rgrbotCollide) > 0:
try:
rgrbotCollide[0].botdef.syntOnTouch.Eval(self)
except:
traceback.print_exc()
elif self.game.board.AchAtPos(posNew) == ansi.achBlank:
self.rbot.Move(posNew)
def draw(self, ascr, client):
if client == self.client:
self.game.board.draw(ascr)
for rbot in self.vm.Rgrbot():
ascr.PutAch(rbot.Ach(), rbot.Pos().X(), rbot.Pos().Y())
def GlobalMsg(self, stMsg):
self.game.rgtoken("msgscroller")[0].evPost.fire(stMsg)
# idea: synchronous event pair to implement "show message" w/ blocking
class GameWorld(Game):
def InitPersistent(self, board):
Game.InitPersistent(self)
self.board = board
self.vm = Vm(board.defs)
self.dtStart = datetime.now()
def StName(self):
return self.dtStart.strftime("%b %d, %Y at %I:%M%p") + " [" + str(len(self.RgclientConnected())) + "]"
def GetRgclsTokTrans(self):
return [[AutoJoiner, Player], MsgScroller, AutoKiller]
def GetRgtagDraw(self):
return ["player", "overlay"]