def create_object(self, obj_info, x, y): from game.components import Tile, TileObject from game.loaders import SpriteLoader from game.scripts import Npc, NpcType obj_type = obj_info['type'] if obj_type == 'none': return None elif obj_type == 'npc': npc_info = obj_info['info'] loader = SpriteLoader(npc_info['sprite']) spr, anim, groups = loader.load() return self.world.create_entity( ScriptComponent(Npc(NpcType[npc_info['type']], npc_info)), Tile({ 'pos': [x, y] }), TileObject({'is_solid': True}), Transform({ 'scale': Vector2(2, 2) }), spr, anim, groups )
def on_event(self, event): if event.type == pygame.KEYDOWN: if event.key == pygame.K_SPACE: if self.state == TextboxState.OPENING: self.set_reading() elif self.state == TextboxState.READING: self.text_index += 1 if self.text_index < self.text_max: self.set_text(self.text[self.text_index][1:4]) if self.owner_entity != None: self.world.delete_entity(self.owner_entity) self.owner_entity = None owner = self.text[self.text_index][0] if owner != None: self.owner_entity = self.world.create_entity( Element({ 'name': self.element.name + "_owner", 'pos': Vector2(self.element.pos.x + 8, self.element.pos.y) }), Sprite( self.font.render(owner, False, (0, 0, 0))), Transform({'layer': 25})) self.anim.current = 'named' else: self.anim.current = 'noname' else: self.set_closing()
def load_chunk(self, chunk_x, chunk_y): self.loaded_chunks.append((chunk_x, chunk_y)) try: chunk_name = next(filter(lambda ch: ch['pos']==[chunk_x, chunk_y], self.world_info.info['chunks']))['name'] except StopIteration: print("No chunk defined for chunk position ({}, {})".format(chunk_x, chunk_y)) raise chunk_path = paths.get_chunk(self.world_info.name, chunk_name) chunk_json = open(chunk_path) chunk = json.load(chunk_json) chunk_json.close() self.loaded_entities[(chunk_x, chunk_y)] = [] chunk_surface = pygame.Surface((int(constants.CHUNK_SIZE_PIXELS), int(constants.CHUNK_SIZE_PIXELS))) for layout in chunk['layouts']: for y in range(0, constants.CHUNK_SIZE): for x in range(0, constants.CHUNK_SIZE): tile_id = layout[y][x] tile_info = self.world_info.mappings[str(tile_id)] if isinstance(tile_info[0], list): weights = list(map(lambda x: x[2], tile_info)) tile_info = random.choices(tile_info, weights)[0] tileset_name = tile_info[0] tile_name = tile_info[1] tileset = self.tilesets[tileset_name] tile_img_pos = tileset['info'][tile_name] tile_size = tileset['info']['tile_size'] chunk_surface.blit(tileset['img'], (x * tile_size, y * tile_size), pygame.Rect(tile_img_pos[0], tile_img_pos[1], tile_size, tile_size)) spr = Sprite(chunk_surface, pygame.Rect(0, 0, constants.CHUNK_SIZE_PIXELS * self.scale, constants.CHUNK_SIZE_PIXELS * self.scale)) transform = Transform({ 'pos': Vector2(chunk_x * constants.CHUNK_SIZE_PIXELS * self.scale, chunk_y * constants.CHUNK_SIZE_PIXELS * self.scale), 'scale': Vector2(self.scale, self.scale) }) self.loaded_entities[(chunk_x, chunk_y)].append(self.world.create_entity(spr, transform)) objects = chunk['objects'] object_mappings = chunk['object_mappings'] for y in range(0, constants.CHUNK_SIZE): for x in range(0, constants.CHUNK_SIZE): obj_id = objects[y][x] obj_info = object_mappings[str(obj_id)] obj_x = int(chunk_x * constants.CHUNK_SIZE + x) obj_y = int(chunk_y * constants.CHUNK_SIZE + y) self.loaded_objects[(obj_x, obj_y)] = self.create_object(obj_info, obj_x, obj_y)
def set_text(self, lines): for i in range(0, 3): if self.text_entity[i] != None: self.world.delete_entity(self.text_entity[i]) self.text_entity[i] = self.world.create_entity( Element({ 'name': self.element.name + "_text" + str(i), 'pos': Vector2(self.element.pos.x + 8, self.element.pos.y + TEXT_SIZE * i + 48) }), Sprite(self.font.render(lines[i], False, (0, 0, 0))), Transform({'layer': 25}))
def update_text(self): from game.components import Sprite, Transform if self.text_entity != None: self.world.delete_entity(self.text_entity) self.text_entity = None self.last_text = self.text spr = self.font.render(self.text, False, (0, 0, 0)) self.text_entity = self.world.create_entity( Element({ 'name': self.element.name + "_text", 'pos': Vector2(self.element.pos.x, self.element.pos.y) }), Sprite(spr), Transform({'layer': 25}))
def set_text(self, text): from game.components import Sprite, Transform if self.text_entity != None: self.world.delete_entity(self.text_entity) self.text_entity = None spr = self.font.render(text, False, (255, 255, 255)) self.text_entity = self.world.create_entity( Element({ 'name': self.element.name + '_text', 'pos': Vector2( self.element.pos.x + (self.element.size.x / 2) - (spr.get_width() / 2), self.element.pos.y + (self.element.size.y / 2) - (spr.get_height() / 2)) }), Sprite(spr), Transform({'layer': 25}))
def create(self, name, entities, value, world): x = value['x'] y = value['y'] scale_x = value['scale_x'] scale_y = value['scale_y'] width = value['width'] height = value['height'] element = Element({ 'name': name, 'size': Vector2(width, height), 'pos': Vector2(x, y) }) script_comp = None transform = Transform({ 'pos': Vector2(x, y), 'scale': Vector2(scale_x, scale_y), 'layer': 20 }) sprite_path = value['sprite'] sprite_loader = SpriteLoader(sprite_path) spr, anim, anim_groups = sprite_loader.load() element_type = value['element'] if element_type == 'button': script_comp = ScriptComponent(Button(value.get('text', ""))) entities.append( world.create_entity(element, script_comp, transform, spr, anim, anim_groups)) elif element_type == 'textbox': #TODO script_comp = ScriptComponent(Textbox()) entites.append( world.create_entity( element, EventListener({ 'events': [ "KEYDOWN", "KEYUP", "MOUSEBUTTONDOWN", "MOUSEBUTTONUP", "MOUSEMOTION" ] }), script_comp, transform, spr, anim, anim_groups))
def set_reading(self): self.transform.scale.x = 1.0 self.transform.scale.y = 1.0 self.state = TextboxState.READING owner = self.text[self.text_index][0] if owner != None: self.owner_entity = self.world.create_entity( Element({ 'name': self.element.name + "_owner", 'pos': Vector2(self.element.pos.x + 8, self.element.pos.y) }), Sprite(self.font.render(owner, False, (0, 0, 0))), Transform({'layer': 25})) self.anim.current = 'named' else: self.anim.current = 'noname' if self.text != None: self.set_text(self.text[self.text_index][1:4])
from pygame.locals import * from pygame.time import Clock from game.Renderer import Renderer from game import Shadow from game.Geometry import Vec2 from game.components.Transform import * from game.Mesh import * renderer = Renderer() triangle_vertices = [Vec2(0, 50), Vec2(-50, -30), Vec2(50, -30)] quad_vertices = [Vec2(-20, -20), Vec2(20, -20), Vec2(20, 20), Vec2(-20, 20)] Triangle1 = Mesh(triangle_vertices) Triangle1.transform = Transform(translation=(400, 300)) meshes = [Triangle1] mousePosition = (0, 0) def initialize(): global renderer pygame.init() pygame.display.set_caption("Shadow") pygame.mouse.set_visible(False) renderer = Renderer() renderer.init((800, 600))