import StateMachine if __name__ == "__main__": sm = StateMachine.StateMachine() sm.stateMachine()
def __init__(self): self.__stateName = "DEFAULT" self.__transitions = list() self.__stateMachine = StateMachine.StateMachine()
SCREEN = pg.display.set_mode((800, 608)) # set screen size SCREEN_RECT = SCREEN.get_rect() # new a GameRenderData render_data = GameRenderData() pg.display.set_caption(render_data.GAME_CAPTION) # set game caption clock = pg.time.Clock() # some about fps fps = 60 show_fps = False done = False # new a render state machine render_state_machine = StateMachine() render_state_machine.add("START", start_game) render_state_machine.add("LOADMAP", load_map) render_state_machine.add("GAMEOVER", None, end_state=1) render_state_machine.setStart("START") while not done: events = pg.event.get() for event in events: if event.type == pg.QUIT: done = True elif event.type == pg.KEYDOWN: keys = pg.key.get_pressed() if event.key == pg.K_F5: show_fps = not show_fps if not show_fps:
def load(self, load_info): logger.info("Load") # Rebuild gameStateObj self.allunits = [ UnitObject.UnitObject(info) for info in load_info['allunits'] ] self.factions = load_info['factions'] if 'factions' in load_info else ( load_info['groups'] if 'groups' in load_info else {}) self.allreinforcements = load_info['allreinforcements'] self.prefabs = load_info['prefabs'] self.triggers = load_info.get('triggers', dict()) map_info = load_info['map'] self.playtime = load_info['playtime'] self.convoy = [ ItemMethods.deserialize(item_dict) for item_dict in load_info['convoy'] ] self.convoy = [item for item in self.convoy if item] self.turncount = load_info['turncount'] self.game_constants = load_info['game_constants'] self.level_constants = load_info['level_constants'] self.objective = CustomObjects.Objective.deserialize( load_info['objective']) if load_info['objective'] else None self.phase_music = CustomObjects.PhaseMusic.deserialize( load_info['phase_music']) if load_info['phase_music'] else None support_dict = load_info['support'] self.talk_options = load_info['talk_options'] self.base_conversations = load_info['base_conversations'] self.stateMachine = StateMachine.StateMachine( load_info['state_list'][0], load_info['state_list'][1]) self.statistics = load_info['statistics'] self.message = [ Dialogue.Dialogue_Scene(scene) for scene in load_info.get('message', []) ] # self.message = [] self.unlocked_lore = load_info['unlocked_lore'] self.market_items = load_info.get('market_items', set()) self.mode = load_info.get('mode', self.default_mode()) # Map self.map = SaveLoad.create_map('Data/Level' + str(self.game_constants['level'])) if map_info: self.map.replay_commands(map_info['command_list'], self.game_constants['level']) self.map.command_list = map_info['command_list'] for position, current_hp in map_info['HP']: self.map.tiles[position].set_hp(current_hp) # Statuses for index, info in enumerate(load_info['allunits']): for s_dict in info['status_effects']: if isinstance(s_dict, dict): StatusObject.deserialize(s_dict, self.allunits[index], self) else: self.allunits[index].status_effects.append(s_dict) # Support if cf.CONSTANTS['support']: self.support = Support.Support_Graph('Data/support_nodes.txt', 'Data/support_edges.txt') self.support.deserialize(support_dict) else: self.support = None # Set up blitting surface if self.map: mapSurfWidth = self.map.width * GC.TILEWIDTH mapSurfHeight = self.map.height * GC.TILEHEIGHT self.mapSurf = Engine.create_surface((mapSurfWidth, mapSurfHeight)) self.grid_manager = AStar.Grid_Manager(self.map) self.boundary_manager = CustomObjects.BoundaryManager(self.map) for unit in self.allunits: if unit.position: self.grid_manager.set_unit_node(unit.position, unit) self.generic() if 'phase_info' in load_info: self.phase.current, self.phase.previous = load_info['phase_info']
from State import * from StateMachine import * from GameEntity import * Person = GaneEntity1(0) Person.currentState = State2() stateMachine = StateMachine(Person) stateMachine._CurrentState = State2() stateMachine.Update() stateMachine._CurrentState = State1() stateMachine.Update()
def main(args): testMachine = StateMachine() testMachine.runStates()
def build_state_machine(phases, vocab): id_to_word = {vocab[word]: word for word in vocab.keys()} vocab_idx = [int(idx) for idx in id_to_word.keys() ] # the list of word ids, e.g. [1, 2, 3, 4, ..., vocab_size] state_machine = StateMachine(events={'input': InputEvent()}) state_machine.add_state('init') state_machine.add_state('final') cond_table = { ('init', 'final'): TransitCondition(), ('init', 'init'): TransitCondition(vocab_idx[:]), ('final', 'final'): TransitCondition(vocab_idx[:]) } cond_word = { ('init', 'final'): [], ('init', 'init'): list(vocab.keys()), ('final', 'final'): list(vocab.keys()) } num_mid_states = 0 state_idx_mapping = {0: 'init'} for phase in phases: words = phase.split() prev_s = 'init' for i, word in enumerate(words): if word not in vocab: break word_id = vocab[word] if i == len(words) - 1: current_s = 'final' else: num_mid_states += 1 current_s = 'mid_{}'.format(num_mid_states) state_idx_mapping[num_mid_states] = current_s state_machine.add_state(current_s) if (prev_s, current_s) in cond_table: cond_table[(prev_s, current_s)].update(word_id) cond_word[(prev_s, current_s)].append(word) else: cond_table[(prev_s, current_s)] = TransitCondition([word_id]) cond_word[(prev_s, current_s)] = [word] if prev_s == 'init': cond_table[('init', 'init')].update(word_id, is_remove=True) if word in cond_word[('init', 'init')]: cond_word[('init', 'init')].remove(word) else: if (prev_s, 'init') not in cond_table: cond_table[(prev_s, 'init')] = TransitCondition(vocab_idx[:]) cond_word[(prev_s, 'init')] = list(vocab.keys()) cond_table[(prev_s, 'init')].update(word_id, is_remove=True) if word in cond_word[(prev_s, 'init')]: cond_word[(prev_s, 'init')].remove(word) prev_s = current_s state_idx_mapping[num_mid_states + 1] = 'final' for source_s, dest_s in cond_table: state_machine.add_transition(source_s, dest_s, 'input', cond_table[(source_s, dest_s)]) return state_machine, state_idx_mapping