BASIC-RoBots / src / main.py

##
## main.py for BASIC-RoBots
## 
## Copyright (C) 2012 Pierre Surply
## <pierre.surply@gmail.com>
##
## This file is part of BASIC-RoBots.
##
##    BASIC-RoBots is free software: you can redistribute it and/or modify
##    it under the terms of the GNU General Public License as published by
##    the Free Software Foundation, either version 3 of the License, or
##    (at your option) any later version.
##
##    BASIC-RoBots 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 General Public License for more details.
##
##    You should have received a copy of the GNU General Public License
##    along with BASIC-RoBots.  If not, see <http://www.gnu.org/licenses/>.
## 
## Started on  Wed May  9 17:39:28 2012 Pierre Surply
## Last update Sun Aug 19 14:31:50 2012 Pierre Surply
##

import os
import shutil

import display
import events
import game
import dashboard
import worldmap
import menu

class Main:
    def __init__(self):
        self.display = display.Display()
        self.input = events.Input()
        self.dashboard = None
        self.menu = menu.Menu(self.input, self.display)
        self.state = 0
        self.quit = False

    def run(self):
        while not self.input.quit and not self.quit:
            self.input.update(self.display)
            if self.state == 0:
                cmd = self.menu.update(self.display, self.input)
                if cmd != None:
                    self.menu_command(cmd)
            elif self.state == 1:
                if self.dashboard.update(self.display, self.input):
                    self.dashboard.worldmap.build_env((self.dashboard.select_x,\
                                                          self.dashboard.select_y), \
                                                          self.input)
                    self.game = game.Game(self.input, \
                                              self.dashboard.worldmap.env[self.dashboard.select_x]\
                                              [self.dashboard.select_y])
                    self.state = 2
            elif self.state == 2:
                if self.game.update(self.display, self.input):
                    self.state = 1
        if self.dashboard != None:
            self.dashboard.worldmap.save()

    def menu_command(self, cmd):
        if cmd[0] == 0:
            self.dashboard = dashboard.Dashboard(self.new_game(cmd[1]), self.input)
            self.state = 1
        elif cmd[0] == 1:
            self.dashboard = dashboard.Dashboard(self.load_game(cmd[1]), self.input)
            self.state = 1
        elif cmd[0] == 2:
            self.quit = True


    def new_game(self, path):
        os.makedirs("saves/" + path)
        os.mkdir("saves/" + path + "/env")
        os.mkdir("saves/" + path + "/robots")
        shutil.copytree("res/lib", "saves/" + path + "/lib", \
                             ignore=shutil.ignore_patterns("*~"))
        wm = worldmap.WorldMap(path)
        wm.save()
        return wm


    def load_game(self, path):
        wm = worldmap.WorldMap(path)
        wm.load(self.input)
        return wm


if __name__ == "__main__":
    Main().run()
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.