# Copyright (c) 2007 SIEGE Development Team
# All rights reserved.
#
# This file is part of libSIEGE.
#
# This software is copyrighted work licensed under the terms of the
# 2-clause BSD license. Please consult the file "license.txt" for
# details.
#
# If you did not recieve the file with this program, please email
# Tim Chas <darkuranium@gmail.com>.

from siege.common cimport *
cimport turtle

cdef class Turtle:
    cdef SGTurtle* handle

    def __init__(self, x, y, degs = 0, draw = True):
        assert self.handle is NULL
        
        self.handle = sgTurtleCreateDegs(x, y, degs, draw)
        if self.handle is NULL:
            raise MemoryError()

    def __dealloc__(self):
        if self.handle is not NULL:
            self.destroy()

    cpdef destroy(self):
        assert self.handle is not NULL
        
        sgTurtleDestroy(self.handle)
        self.handle = NULL

    def reset(self):
        assert self.handle is not NULL
        
        sgTurtleReset(self.handle)
    def push(self):
        assert self.handle is not NULL
        
        sgTurtlePush(self.handle)
    def pop(self):
        assert self.handle is not NULL
        
        sgTurtlePop(self.handle)

    def step(self, dist):
        assert self.handle is not NULL
        
        sgTurtleStep(self.handle, dist)
    def move(self, x, y):
        assert self.handle is not NULL
        
        sgTurtleMove(self.handle, x, y)
    def jump(self, x, y):
        assert self.handle is not NULL
        
        sgTurtleJump(self.handle, x, y)

    def penUp(self):
        assert self.handle is not NULL
        
        sgTurtlePenUp(self.handle)
    def penDown(self):
        assert self.handle is not NULL
        
        sgTurtlePenDown(self.handle)

    property pen:		
        def __set__(self, pen):
            assert self.handle is not NULL
            
            sgTurtleSetPen(self.handle, pen)
        def __get__(self):
            assert self.handle is not NULL
            
            return bool(sgTurtleGetPen(self.handle))

    property pos:
        def __set__(self, p):
            assert self.handle is not NULL
            
            sgTurtleSetPos(self.handle, p[0], p[1])
        def __get__(self):
            assert self.handle is not NULL
            
            cdef float x, y
            sgTurtleGetPos(self.handle, &x, &y)
            return (x, y)
    property x:
        def __set__(self, x):
            assert self.handle is not NULL
            
            sgTurtleSetPosX(self.handle, x)
        def __get__(self):
            assert self.handle is not NULL
            
            return sgTurtleGetPosX(self.handle)
    property y:
        def __set__(self, y):
            assert self.handle is not NULL
            
            sgTurtleSetPosY(self.handle, y)
        def __get__(self):
            assert self.handle is not NULL
            
            return sgTurtleGetPosY(self.handle)

    def turnLeftRads(self, rads):
        assert self.handle is not NULL
        
        sgTurtleTurnLeftRads(self.handle, rads)
    def turnLeftDegs(self, degs):
        assert self.handle is not NULL
        
        sgTurtleTurnLeftDegs(self.handle, degs)

    def turnRightRads(self, rads):
        assert self.handle is not NULL
        
        sgTurtleTurnRightRads(self.handle, rads)
    def turnRightDegs(self, degs):
        assert self.handle is not NULL
        
        sgTurtleTurnRightDegs(self.handle, degs)

    property angleRads:
        def __set__(self, rads):
            assert self.handle is not NULL
            
            sgTurtleSetAngleRads(self.handle, rads)
        def __get__(self):
            assert self.handle is not NULL
            
            return sgTurtleGetAngleRads(self.handle)
    property angleDegs:
        def __set__(self, degs):
            assert self.handle is not NULL
            
            sgTurtleSetAngleDegs(self.handle, degs)
        def __get__(self):
            assert self.handle is not NULL
            
            return sgTurtleGetAngleDegs(self.handle)
