Example #1
0
    def check_spawning(self, res):
        # Check that there are enough entities fulfilling the critieria in vicinity.
        if self.criteria and self.get_prop_float("__spawner_radius", 0) > 0:
            domain = self.get_parent_domain()
            if domain:
                missing_amount = self.get_prop_int("__spawner_amount", 0)
                if missing_amount > 0:
                    sphere = physics.Ball(
                        self.location.pos,
                        self.get_prop_float("__spawner_radius", 0))
                    collisions = domain.query_collisions(sphere)
                    for collision in collisions:
                        entity = collision.entity
                        if server.world.match_entity(self.criteria, entity):
                            missing_amount = missing_amount - entity.get_prop_int(
                                "amount", 1)

                        if missing_amount == 0:
                            break

                    if missing_amount > 0:
                        # We need to spawn an entity.
                        create_op = self.spawn_new_entity(res)
                        if create_op:
                            res.append(create_op)
Example #2
0
    def setup_operation(self, op):
        domain = self.get_parent_domain()
        if domain:

            # If there's an "entity_ref" prop it's the reference to the actor which caused the explosion.
            actor_id = self.id
            entity_ref_prop = self.props.entity_ref
            if entity_ref_prop is not None:
                actor_id = entity_ref_prop["$eid"]

            blast_radius = self.location.radius
            if blast_radius > 0:
                sphere = physics.Ball(self.location.pos, blast_radius)
                collisions = domain.query_collisions(sphere)
                base_damage = self.get_prop_float("damage", 0)
                for collision in collisions:
                    entity = collision.entity
                    damage = base_damage * (
                        (blast_radius - collision.distance) / blast_radius)
                    if entity != self:
                        self.send_world(
                            Operation('hit',
                                      Entity(hit_type="explosion",
                                             id=actor_id,
                                             damage=damage),
                                      to=entity))
Example #3
0
def initialise():
    'Build balls and prepare GUI.'
    global balls, x, y, screen, lock, start, frame
    balls = []
    for ball in xrange(BALLS):
        x = -START_SPACE if random.randint(0, 1) else START_SPACE + SCREEN_WIDTH
        y = random.randint(BALL_RADIUS, SCREEN_HEIGHT - FLOOR_SPACE - BALL_RADIUS)
        balls.append(physics.Ball(x, y, BALL_RADIUS))
    root = Tkinter.Tk()
    root.resizable(False, False)
    root.title('Bouncy Balls')
    x = (root.winfo_screenwidth() - SCREEN_WIDTH) / 2
    y = (root.winfo_screenheight() - SCREEN_HEIGHT) / 2
    root.geometry('%dx%d+%d+%d' % (SCREEN_WIDTH, SCREEN_HEIGHT, x, y))
    root.bind_all('<Escape>', lambda event: event.widget.quit())
    root.bind('<Configure>', move)
    screen = Tkinter.Canvas(root, width=SCREEN_WIDTH, height=SCREEN_HEIGHT, background=BACKGROUND)
    screen.after(1000 / FPS, update)
    screen.after(10000 / FPS, unlock)
    screen.pack()
    floor_height = SCREEN_HEIGHT - FLOOR_SPACE + 2
    screen.create_rectangle(0, 0, WALL_SPACE - 1, floor_height, fill=FORCE_COLOR)
    screen.create_rectangle(SCREEN_WIDTH - WALL_SPACE + 1, 0, SCREEN_WIDTH, floor_height, fill=FORCE_COLOR)
    screen.create_line(0, floor_height, SCREEN_WIDTH, floor_height, width=3, fill=FLOOR_COLOR)
    lock = True
    start = time.clock()
    frame = 1.0
Example #4
0
def build_balls():
    "Build some non-overlapping balls."
    global balls
    balls = []
    sides = set()
    for ball in xrange(GAM.B_ALL):
        x = -GAM.B_OFF if random.randint(0, 1) else GAM.B_OFF + GAM.SCR_W
        y = random.randint(GAM.B_RAD, GAM.SCR_H - GAM.F_OFF - GAM.B_RAD) / GAM.B_RAD * GAM.B_RAD
        while (x, y) in sides:
            x = -GAM.B_OFF if random.randint(0, 1) else GAM.B_OFF + GAM.SCR_W
            y = random.randint(GAM.B_RAD, GAM.SCR_H - GAM.F_OFF - GAM.B_RAD) / GAM.B_RAD * GAM.B_RAD
        sides.add((x, y))
        balls.append(physics.Ball(x, y, GAM.B_RAD))
        balls[-1].type = 0
    balls = tuple(balls)
Example #5
0
def read_input(fname, energylog=0):
    with open(fname, "r") as f:

        # First line of file is the dimension of the frame in pixels
        w, h = [int(x) for x in f.readline().strip().split(",")]
        world = physics.World(width=w, height=h)

        current_shape = ""
        for line in f.readlines():
            line = line.strip()

            # Ignore commented out lines and empty lines
            if not line or line[0] == "#":
                continue

            words = line.split()
            if words[0] == "PARAM":
                setattr(physics, words[1], float(words[2]))
                continue

            if current_shape == "circle":
                data = line.split(",")
                if data[0] == "density":
                    density = float(data[1])
                else:
                    center_x = float(data[0])
                    center_y = float(data[1])
                    rad = float(data[2])
                    obj = physics.Ball(world,
                                       pos=[center_x, center_y],
                                       radius=rad)
                    current_shape = ""

            elif current_shape == "polygon" or current_shape == "fixedpolygon":
                data = line.split(",")
                if data[0] == "density":
                    density = float(data[1])
                elif data[0] == "sides":
                    sides = int(data[1])
                    npoints = sides
                    points = []
                elif data[0] == "velocity":
                    vel = physics.np.array([float(data[1]), float(data[2])])
                elif data[0] == "point":
                    npoints -= 1
                    pp = [float(_) for _ in data[1:]]
                    point = physics.Point(world, pos=pp, speed=vel)
                    points.append(point)
                    if npoints == 0:
                        if current_shape == "polygon":
                            if density == physics.np.inf:
                                print(
                                    "Warning: density never set for polygon, set to 1"
                                )
                                density = 1
                            obj = physics.Polygon(world,
                                                  points=points,
                                                  density=density,
                                                  speed=vel)
                        else:
                            obj = physics.FixedPolygon(world, points=points)
                        current_shape = ""
            else:
                vel = physics.np.array([0.0, 0.0])
                current_shape = line
                density = physics.np.inf
    return world