def SetTexture(self, texture, forcedSize=None, shaderPass=None): self.sprite = Sprite(texture) self.shaderPass = shaderPass self.forcedSize = Vec2(forcedSize) self.sprite.origin = Vec2(texture.size) / 2
def Step(self): if self.target: diff = Vec2(self.target.position) - Vec2(self.entity.position) dist = max( self.entity.position.get_distance(self.target.position)**2, 3) direction = diff.normalized() self.entity.FetchComponent( VelocityComponent).velocity = self.entity.FetchComponent( VelocityComponent).velocity + direction * ( (self.factor * self.core.time.GetDelta()) / (dist))
def __init__(self, position=Vec2(0, 0), rotation=0.0, size=Vec2(100, 100)): """Constructor :param position: Coordinates at which entity will be created :type position: Vec2 :param rotation: Initial entity rotation, degrees :type rotation: float :param size: Size of entities' bounding box :type size: Vec2""" Entity.__init__(self, position, rotation, size) self.components = []
def SetTexture(self, texture, shaderPass=None, hasDirectionSheet=False): self.sprite = Sprite( texture, Rectangle((0, 0), (texture.size.x / (8 if hasDirectionSheet else 1), texture.size.y))) self.shaderPass = shaderPass self.hasDirectionSheet = hasDirectionSheet self.sprite.ratio = Vec2(self.scale, self.scale) self.sprite.origin = Vec2( texture.size.x / (8 if hasDirectionSheet else 1), texture.size.y) / 2
def __init__(self, size, position=(0, 0), angle=0.0): """Constructor :param size: The size of the rectangle :type size: :class:`PyNARGE.Vec2` :param position: Where the physics body should be spawned :type position: :class:`PyNARGE.Vec2` :param angle: The body's initial rotation, degrees :type angle: float""" self.size = Vec2(size) self.position = Vec2(position) self.angledeg = angle self.anglerad = 3.141 * float(angle) / 180.0
def __init__(self, size, position=(0, 0), friction=0.3, density=1.0): """Constructor :param size: The size of the rectangle :type size: :class:`PyNARGE.Vec2` :param position: Where the physics body should be spawned :type position: :class:`PyNARGE.Vec2` :param friction: How much friction the object will experience [0-1] :type friction: float :param density: How dense the object will be :type density: float""" self.size = Vec2(size) self.position = Vec2(position) self.body = None self.friction = friction self.density = density
def __init__(self, core): self.core = core self.window = None self.size = Vec2(0, 0) self.shaderpasses = [] self.cameraView = None self.uiView = None
def SetCameraPosition(self, position): """Set the camera's position in world coordinates :param position: The camera's position :type position: :class:`PyNARGE.Vec2`""" self.cameraView.center = Vec2(position) for p in self.shaderpasses: p.SetCenter(position)
def __init__(self, texture, shaderPass=None, hasDirectionSheet=False): self.sprite = Sprite( texture, Rectangle((0, 0), (texture.size.x / (8 if hasDirectionSheet else 1), texture.size.y))) self.shaderPass = shaderPass self.alpha = 255 self.scale = pixelDoubleFactor self.hasDirectionSheet = hasDirectionSheet self.sprite.origin = Vec2( texture.size.x / (8 if hasDirectionSheet else 1), texture.size.y) / 2 self.sprite.color = Color(255, 255, 255, self.alpha) self.sprite.ratio = Vec2(self.scale, self.scale)
def _Init(self): self.Build() if self.core.settings.label_entity_types: self.AddComponent( TextComponent(str(type(self).__name__), self.core.resourceManager.FetchDefaultFontMono(), 12, Vec2(0, -10))) for component in self.components: component.Init() self.Init()
def __init__(self, text, font=None, size=20, color=Color.WHITE, offset=Vec2(0, 0)): self.message = text self.offset = offset self.size = size self.font = font self.color = color self.scale = 1.0
def __init__(self, texture, forcedSize=None, shaderPass=None, offset=(0, 0)): self.sprite = Sprite(texture) self.shaderPass = shaderPass self.offset = Vec2(offset) self.alpha = 255 self.scale = 1 self.forcedSize = None if forcedSize != None: self.forcedSize = Vec2(forcedSize) self.sprite.origin = Vec2(texture.size) / 2 self.sprite.color = Color(255, 255, 255, self.alpha) self.sprite.ratio = Vec2(self.scale, self.scale) if self.forcedSize != None: self.sprite.scale( (float(self.forcedSize.x) / float(self.sprite.texture.size.x), float(self.forcedSize.y) / float(self.sprite.texture.size.y)))
def __init__(self, radius, position=(0, 0), friction=0.3, density=1.0): """Constructor :param radius: The radius of the circular body :type radius: float :param position: Where the physics body should be spawned :type position: :class:`PyNARGE.Vec2` :param friction: How much friction the object will experience [0-1] :type friction: float :param density: How dense the object will be :type density: float""" self.radius = float(radius) self.position = Vec2(position) self.body = None self.friction = friction self.density = density
def Initialize(self, title="PyNARGE Window", size_x=800, size_y=600, fullscreen=False, antialiasing=False): print "Creating render window..." self.size = Vec2(size_x, size_y) self.window = sfml.RenderWindow( sfml.VideoMode(size_x, size_y), title, sfml.window.Style.FULLSCREEN if fullscreen else sfml.window.Style.DEFAULT, sfml.window.ContextSettings(0, 0, 8) if antialiasing else None) self._draw = self.window.draw self.cameraView = self.window.default_view self.uiView = self.window.default_view if self.core.settings.limit_framerate: self.window.framerate_limit = 60
def Draw(self): self.sprite.position = Vec2( 1 + pixelDoubleFactor * int(self.entity.position.x / pixelDoubleFactor), pixelDoubleFactor * int(self.entity.position.y / pixelDoubleFactor)) self.sprite.rotation = -self.entity.rotation + 22.5 self.sprite.rotation %= 360 if self.hasDirectionSheet: index = int(self.sprite.rotation / 45) self.sprite.rotation = 0 self.sprite.texture_rectangle = Rectangle( (index * self.sprite.texture.size.x / 8, 0), (self.sprite.texture.size.x / 8, self.sprite.texture.size.y)) else: self.sprite.rotation = int(self.sprite.rotation / 90) * 90 self.core.renderer.Draw(self.sprite, self.shaderPass)
def ReCenter(self): self.text.origin = Vec2(self.text.local_bounds.size.x / 2, self.size / 2)
def GetCameraPosition(self): """Get the camera's position in world coordinates :returns: :class:`PyNARGE.Vec2` - The camera's position""" return Vec2(self.cameraView.center)
def SetVelocity(self, velocity): self.velocity = Vec2(velocity)
def Init(self, startingVelocity=Vec2(0, 0)): self.velocity = startingVelocity
def SetOffset(self, offset): self.offset = Vec2(offset)
def __init__(self, target=None, offset=Vec2(0, 0)): self.target = target self.offset = Vec2(offset)
def LocalToScreen(self, coords): return Vec2( float(coords.x) * self.physicsScale, float(coords.y) * self.physicsScale)
def LocalToWorld(self, coords): return Vec2( float(coords.x) / self.physicsScale, float(coords.y) / self.physicsScale)
def GlobalToScreen(self, coords): return Vec2( float(coords.x) * self.physicsScale + self.core.renderer.window.size.x / 2, self.core.renderer.window.size.y / 2 - float(coords.y) * self.physicsScale)
def GlobalToWorld(self, coords): return Vec2((float(coords.x) - self.core.renderer.window.size.x / 2) / self.physicsScale, (self.core.renderer.window.size.y / 2 - float(coords.y)) / self.physicsScale)