/surfacetest.py
#!/usr/bin/env python

import pygame
from Richter.Sprite import *
from Richter.Surface import *
import Richter.Engine as Engine
from StringIO import StringIO

#resolution = (1280,1024)
resolution = (640,480)
#resolution = (320,240)

Engine.init(resolution)

pygame.display.set_caption("surfacetest")

#surfaces = SurfaceSet('data/example1')

# Concave
surfaces = SurfaceSet(StringIO("""
tilesize 100 100
surface 2 4 3 2
surface 3 4 2 3
solid 2 2 5 3
surface 4 3 5 4
"""))

# Octagon
#surfaces = SurfaceSet(StringIO("""
#surface 520 240 461 98
#surface 461 98 319 40
#surface 319 40 178 98
#surface 178 98 120 240
#surface 120 240 178 381
#surface 178 381 320 440
#surface 320 440 461 381
#surface 461 381 520 240
#"""))


player = Sprite('img/star.png')
player.setGravity(CENTER, CENTER)
player.position = [resolution[0]/2,resolution[1]/2]

class gamestate:
	direction = [0,0]
	colliding = False

def input(e):
	if e.type == pygame.KEYDOWN:
		if e.key == pygame.K_LEFT:
			gamestate.direction[0] = -1
		elif e.key == pygame.K_RIGHT:
			gamestate.direction[0] = 1
		elif e.key == pygame.K_DOWN:
			gamestate.direction[1] = -1
		elif e.key == pygame.K_UP:
			gamestate.direction[1] = 1
	elif e.type == pygame.KEYUP:
		if e.key == pygame.K_LEFT or e.key == pygame.K_RIGHT:
			gamestate.direction[0] = 0
		elif e.key == pygame.K_UP or e.key == pygame.K_DOWN:
			gamestate.direction[1] = 0

Engine.eventhandlers.append(input)


def update():
	player.position[0] += gamestate.direction[0]
	player.position[1] += gamestate.direction[1]
	if surfaces.collidePoint(player.position):
		if not gamestate.colliding:
			player.setColor(1.0,0.2,0.2)
			gamestate.colliding = True
	else:
		if gamestate.colliding:
			player.setColor(1.0,1.0,1.0)
			gamestate.colliding = False
		
Engine.updaters.append(update)

def draw():
	surfaces.draw()
	player.draw()

Engine.drawers.append(draw)

Engine.run()