# THIS IS A SOURCE CODE FILE FROM I'M NOT EVEN HUMAN THE GAME.
# IT COULD BE USED IN A DIFFERENT PIECE OF SOFTWARE ( LIKE A
# DIFFERENT GAME ), BUT IT WAS ORIGINALLY WRITTEN FOR I'M NOT
# EVEN HUMAN THE GAME.

# THE DEVELOPERS OF THE GAME ARE : (C) J.Y.AMIHUD, AYYZEE AND 
# OTHER CONTRIBUTORS. THIS AND OTHER FILES IN THIS GAME,
# UNLESS SPECIFICALLY NOTED, COULD BE USED UNDER THE TERMS OF
# GNU GENERAL PUBLIC LICENSE VERSION 3 OR ANY LATER VERSION.

import os
import json

# GTK module ( Graphical interface
import gi
gi.require_version('Gtk', '3.0')
from gi.repository import Gtk
import cairo

from modules import ui
from modules import world

def layer(game):
    
    # Setting up a cairo layer
    surface = cairo.ImageSurface(cairo.FORMAT_ARGB32,
                                 game.current['w'],
                                 game.current['h'])
    layer = cairo.Context(surface)
    layer.set_antialias(cairo.ANTIALIAS_NONE) # Disable AA to prevent blured pixels


    
    # Prepare the layer for gameplay and do not draw the editor grid.
    world.draw(game, layer, 0, 0,
               game.current["w"],
               game.current["h"], grid=False)

    # Moving 79th around the map
    col = game.current["state"]["4211D79"]["colision"]
    
    

    # LEFT
    if 65361 in game.current["keys"]:
        if col and col[0] > game.current["state"]["4211D79"]["xyz"][0]\
           or not col:
            game.current["state"]["4211D79"]["xyz"][0] -= 3

    # RIGHT
    if 65363 in game.current["keys"]:
        if col and col[0] < game.current["state"]["4211D79"]["xyz"][0]\
           or not col:
            game.current["state"]["4211D79"]["xyz"][0] += 3

    # UP
    if 65362 in game.current["keys"]:
        if col and col[1] > game.current["state"]["4211D79"]["xyz"][1]\
           or not col:
            game.current["state"]["4211D79"]["xyz"][1] -= 2

    # DOWN
    if 65364 in game.current["keys"]:
        if col and col[1] < game.current["state"]["4211D79"]["xyz"][1]\
           or not col:
            game.current["state"]["4211D79"]["xyz"][1] += 2

    # Camera binding
    game.current["camera"][0] = 0 - game.previous["state"]["4211D79"]["xyz"][0] + (game.current["w"] / 2)
    game.current["camera"][1] = 0 - game.previous["state"]["4211D79"]["xyz"][1] + (game.current["h"] / 2)
    
    return surface
