Skip to content

Commit

Permalink
Added drawing layers
Browse files Browse the repository at this point in the history
  • Loading branch information
DeflatedPickle committed Sep 16, 2018
1 parent b70bdc1 commit 9d17e2c
Show file tree
Hide file tree
Showing 8 changed files with 64 additions and 17 deletions.
11 changes: 8 additions & 3 deletions example/test.py
Original file line number Diff line number Diff line change
Expand Up @@ -152,23 +152,28 @@ def collision_stay(self, collider):
self.collided_box = collider


back = swine.window.Layer(window, "Back")
player = swine.window.Layer(window, "Player")
forward = swine.window.Layer(window, "Forward")


pig_sprite = swine.graphics.Sprite("pig_idle_0.png", swine.object.Anchor.MIDDLE_CENTER)
pig = swine.object.GameObject(scene_one, "Player", [PlayerMove(), GrabBox(),
swine.component.Transform(),
swine.component.SpriteRenderer(pig_sprite, 6),
swine.component.SpriteRenderer(pig_sprite, player, 6),
swine.component.physics.RigidBody(1, False, False),
swine.component.physics.collider.PolygonCollider(),
swine.component.quicktime.QuickTimeHold(key.X, 60, reset="slow", command=lambda: print("X was held")),
swine.component.quicktime.QuickTimeMash(key.F, times=5, command=lambda: print("F was mashed"))])

box_sprite = swine.graphics.Sprite("metal_box.png", swine.object.Anchor.MIDDLE_CENTER)
box = swine.object.GameObject(scene_one, "Box1", [swine.component.Transform(Vec2d(85, -20)),
swine.component.SpriteRenderer(box_sprite, 4),
swine.component.SpriteRenderer(box_sprite, back, 4),
swine.component.physics.RigidBody(4),
swine.component.physics.collider.PolygonCollider()], ["Box"])

box2 = swine.object.GameObject(scene_one, "Box2", [swine.component.Transform(Vec2d(-150, 0)),
swine.component.SpriteRenderer(box_sprite, 4),
swine.component.SpriteRenderer(box_sprite, forward, 4),
swine.component.physics.RigidBody(4),
swine.component.physics.collider.PolygonCollider()], ["Box"])

Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from setuptools import setup

setup(name="swine",
version="2.4.3",
version="2.5.0",
description="A Python game engine",
author="DeflatedPickle",
url="https://github.com/SwineProject/swine",
Expand Down
5 changes: 3 additions & 2 deletions swine/component/sprite_renderer.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,16 @@


class SpriteRenderer(Component):
def __init__(self, image: swine.graphics.Sprite, scale: int = 1):
def __init__(self, image: swine.graphics.Sprite, layer, scale: int = 1):
Component.__init__(self)
self.image = image
self.layer = layer
self.scale = scale

self.sprite: pyglet.sprite.Sprite = None

def load(self):
self.sprite = pyglet.sprite.Sprite(self.image.sprite, batch=self.parent.scene.batch)
self.sprite = pyglet.sprite.Sprite(self.image.sprite, batch=self.parent.scene.batch, group=self.layer.group)
self.sprite.scale = self.scale

def physics_update(self, dt):
Expand Down
25 changes: 14 additions & 11 deletions swine/component/transform.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,13 @@ def __init__(self, position: pymunk.Vec2d = pymunk.Vec2d(0, 0), rotation: int =
self.first_update = True
self.move_to_rigid = False

self.sprite = None
# self.sprite = None
self.sprites = None
self.rigid = None

def start(self):
self.sprite = self.parent.get_component(SpriteRenderer)
# self.sprite = self.parent.get_component(SpriteRenderer)
self.sprites = self.parent.get_multiple_components(SpriteRenderer)
self.rigid = self.parent.get_component(RigidBody)

def load(self):
Expand Down Expand Up @@ -52,17 +54,18 @@ def update(self, dt):
self.rigid.body.angle = math.radians(self.rotation)
self.rigid.body.position = self.position

if self.sprite is not None:
self.sprite.sprite.position = self.position.x, self.position.y
self.sprite.sprite.rotation = math.degrees(-self.rotation)
if self.parent.parent is not None:
parent_transform = self.parent.parent.get_component(Transform)
if parent_transform is not None:
self.scale = parent_transform.scale

if self.parent.parent is not None:
parent_transform = self.parent.parent.get_component(Transform)
if parent_transform is not None:
self.scale = parent_transform.scale
if self.sprites is not None:
for sprite in self.sprites:
sprite.sprite.position = self.position.x, self.position.y
sprite.sprite.rotation = math.degrees(-self.rotation)

self.sprite.sprite.scale_x = self.scale[0]
self.sprite.sprite.scale_y = self.scale[1]
sprite.sprite.scale_x = self.scale[0]
sprite.sprite.scale_y = self.scale[1]

# Rigid Body
if self.rigid is not None and self.parent.parent is None:
Expand Down
9 changes: 9 additions & 0 deletions swine/object/game_object.py
Original file line number Diff line number Diff line change
Expand Up @@ -145,3 +145,12 @@ def get_component(self, type_: Type[Component]) -> Optional[Component]:
return comp

return None

def get_multiple_components(self, type_: Type[Component]) -> List[Component]:
comp_list = []

for comp in self.components:
if type(comp) == type_:
comp_list.append(comp)

return comp_list
1 change: 1 addition & 0 deletions swine/window/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@

from .scene import Scene
from .window import Window
from .layer import Layer
19 changes: 19 additions & 0 deletions swine/window/layer.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import pyglet


class Layer(object):
def __init__(self, window, name: str, index=-1):
self.window = window
self.name = name

if index == -1:
self.index = len(self.window.layers)

else:
self.index = index

self.window.layers.append(self)

self.group = pyglet.graphics.OrderedGroup(self.index)
9 changes: 9 additions & 0 deletions swine/window/window.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ def __init__(self, debug: bool = False):

self.terminal = None

self.layers = []

if self.debug:
self.options = DrawOptions()

Expand Down Expand Up @@ -72,3 +74,10 @@ def update(self, dt):

if self.terminal is not None:
self.terminal.update()

def get_layer_by_name(self, name: str):
for layer in self.layers:
if layer.name == name:
return layer

return None

0 comments on commit 9d17e2c

Please sign in to comment.