def on_mouse_press (self, x, y, buttons, modifiers): if buttons == pyglet.window.mouse.LEFT: grid_pos = get_cell_from_point(self.draging[0], self.draging[1]) is_out = self.menu.level.world.grid.is_out_at(self.tower_class, grid_pos) can_fit = self.menu.level.world.grid.can_fit_at(self.tower_class, grid_pos) if not is_out and can_fit: self.menu.level.add_tower(self.tower_class, grid_pos) self.menu.end_drag()
def on_mouse_press(self, x, y, buttons, modifiers): vx, vy = director.get_virtual_coordinates(x,y) grid_cell = get_cell_from_point(vx, vy) if buttons == mouse.RIGHT: world_obj = self.level.world.grid.get_at(grid_cell) # remove tower: if world_obj is not None and isinstance(world_obj, Tower): self.level.remove_tower(world_obj) elif buttons == mouse.LEFT: world_obj = self.level.world.grid.get_at(grid_cell) # activate or deactivate tower: if world_obj is not None and isinstance(world_obj, Tower): self.level.world.activate_tower(world_obj) else: self.level.world.deactivate_tower()
def on_mouse_motion(self, x, y, dx, dy): self.draging = director.get_virtual_coordinates(x,y) grid_pos = get_cell_from_point(self.draging[0], self.draging[1]) # TODO this call is ugly, so loong: is_out = self.menu.level.world.grid.is_out_at(self.tower_class, grid_pos) if is_out: self.rect_layer.visible = False else: self.rect_layer.visible = True # TODO this call is ugly, so loong: can_fit = self.menu.level.world.grid.can_fit_at(self.tower_class, grid_pos) self.rect_layer.position = (grid_pos[0]*GRID_CELL, grid_pos[1]*GRID_CELL) if can_fit: self.rect_layer.color = 0, 255, 0 else: self.rect_layer.color = 255, 0, 0 self.sprite.position = self.draging
def __init__(self, tower_class, menu, x=0, y=0): super(TowerCreationLayer, self).__init__() self.tower_class = tower_class self.menu = menu self.draging = (x,y) # a rect to indicate the object's possible position: self.rect_size_w = self.tower_class.size[0] * GRID_CELL self.rect_size_h = self.tower_class.size[1] * GRID_CELL self.rect_layer = ColorLayer(0,250,0,55, self.rect_size_w, self.rect_size_h) grid_pos = get_cell_from_point(self.draging[0], self.draging[1]) self.rect_layer.position = (grid_pos[0]*GRID_CELL, grid_pos[1]*GRID_CELL) self.add(self.rect_layer) # dragging sprite of the map object: self.sprite = Sprite(images_for_sprites[tower_class]) self.sprite.scale = 0.5 self.sprite.position = self.draging self.add(self.sprite)