<commit_before>//
//  LGRenderingSystem.swift
//  swift-engine
//
//  Created by <NAME> on 7/14/14.
//  Copyright (c) 2014 <NAME>. All rights reserved.
//

import SpriteKit

class LGRenderingSystem: LGSystem
{
	var scene: LGScene
	final var positions	= [LGPosition]()
	final var sprites	= [LGSprite]()
	
	init(scene: LGScene)
	{
		self.scene = scene
		
		super.init()
		self.updatePhase = .Render
	}
	
	override func accepts(entity: LGEntity) -> Bool
	{
		return entity.has(LGPosition) && entity.has(LGSprite)
	}
	
	override func add(entity: LGEntity)
	{
		super.add(entity)
		
		let position	= entity.get(LGPosition)!
		let sprite		= entity.get(LGSprite)!
		var pos			= 1
		
		var node: SKSpriteNode!
		if let _ = sprite.node
		{
			node = sprite.node
		}
		else
		{
			node = SKSpriteNode()
			node.anchorPoint = CGPoint(x: 0.5, y: 0)
			sprite.node = node
		}
		scene.addChild(node)
		
		if let state = sprite.currentState
		{
			pos = state.position
		}
		
		if let texture = sprite.spriteSheet?.textureAtPosition(pos)
		{
			texture.filteringMode = .Nearest
			
			sprite.node.texture = texture
			sprite.node.size = texture.size()
		}
		else if let body = entity.get(LGPhysicsBody)
		{
			sprite.node.color = UIColor.whiteColor()
			sprite.node.size = CGSize(width: CGFloat(body.width), height: CGFloat(body.height))
		}
		
		positions += position
		sprites += sprite
	}
	
	override func update()
	{
		// TODO: Don't render everything every time... only render moved/changed things
		for id in 0 ..< entities.count
		{
			let sprite = sprites[id]
			let position = positions[id]
			
			// Update sprite frame
			
			if let state = sprite.currentState
			{
				// TODO: Don't make it fetch a new texture using textureAtPosition every time
				
				if ++state.counter > state.duration
				{
					state.position++
					if state.position > state.end
					{
						if state.loops
						{
							state.position = state.start
						}
						else
						{
							state.position = state.end
						}
					}
					sprite.node.texture = sprite.spriteSheet?.textureAtPosition(state.position)
					state.counter = 0
				}
			}
			
			// Update sprite position and orientation
			
			sprite.node.position.x	= CGFloat(position.x + sprite.offset.x + CGFloat(sprite.node.size.width / 2))
			sprite.node.position.y	= CGFloat(position.y + sprite.offset.y)
			sprite.node.xScale		= CGFloat(sprite.xScale)
			sprite.node.yScale		= CGFloat(sprite.yScale)
		}
	}
}<commit_msg>Added rotation to LGSprite<commit_after>//
//  LGRenderingSystem.swift
//  swift-engine
//
//  Created by <NAME> on 7/14/14.
//  Copyright (c) 2014 <NAME>. All rights reserved.
//

import SpriteKit

class LGRenderingSystem: LGSystem
{
	var scene: LGScene
	final var positions	= [LGPosition]()
	final var sprites	= [LGSprite]()
	
	init(scene: LGScene)
	{
		self.scene = scene
		
		super.init()
		self.updatePhase = .Render
	}
	
	override func accepts(entity: LGEntity) -> Bool
	{
		return entity.has(LGPosition) && entity.has(LGSprite)
	}
	
	override func add(entity: LGEntity)
	{
		super.add(entity)
		
		let position	= entity.get(LGPosition)!
		let sprite		= entity.get(LGSprite)!
		var pos			= 1
		
		var node: SKSpriteNode!
		if let _ = sprite.node
		{
			node = sprite.node
		}
		else
		{
			node = SKSpriteNode()
			node.anchorPoint = CGPoint(x: 0.5, y: 0)
			sprite.node = node
		}
		scene.addChild(node)
		
		if let state = sprite.currentState
		{
			pos = state.position
		}
		
		if let texture = sprite.spriteSheet?.textureAtPosition(pos)
		{
			texture.filteringMode = .Nearest
			
			sprite.node.texture = texture
			sprite.node.size = texture.size()
		}
		else if let body = entity.get(LGPhysicsBody)
		{
			sprite.node.color = UIColor.whiteColor()
			sprite.node.size = CGSize(width: CGFloat(body.width), height: CGFloat(body.height))
		}
		
		positions += position
		sprites += sprite
	}
	
	override func update()
	{
		// TODO: Don't render everything every time... only render moved/changed things
		for id in 0 ..< entities.count
		{
			let sprite = sprites[id]
			let position = positions[id]
			
			// Update sprite frame
			
			if let state = sprite.currentState
			{
				// TODO: Don't make it fetch a new texture using textureAtPosition every time
				
				if ++state.counter > state.duration
				{
					state.position++
					if state.position > state.end
					{
						if state.loops
						{
							state.position = state.start
						}
						else
						{
							state.position = state.end
						}
					}
					sprite.node.texture = sprite.spriteSheet?.textureAtPosition(state.position)
					state.counter = 0
				}
			}
			
			// Update sprite position and orientation
			
			sprite.node.position.x	= CGFloat(position.x + sprite.offset.x + CGFloat(sprite.node.size.width / 2))
			sprite.node.position.y	= CGFloat(position.y + sprite.offset.y)
			sprite.node.xScale		= CGFloat(sprite.xScale)
			sprite.node.yScale		= CGFloat(sprite.yScale)
			sprite.node.zRotation	= CGFloat(sprite.rotation)
		}
	}
}
{'fraction_non_alphanumeric': 0.11582545747033983, 'fraction_numerical': 0.006434747637241102, 'mean_word_length': 2.6122004357298474, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2008077', 'n_tokens_mistral': 2186, 'n_tokens_neox': 1723, 'n_words': 383}