Chess Search

Sunday, January 15, 2017

Friday, January 13, 2017

Work



'''Script for:
http://blender.stackexchange.com/questions/70761/how-do-i-move-game-pieces-around-a-three-dimensional-board-in-a-turn-based-game
'''

import bge

PIECE_PROPERTY = 'PIECE'
TILE_PROPERTY = 'TILE'

PIECE_SELECT_COLOR = [1, 1, 1, 1]
PIECE_DESELECT_COLOR = [0.5, 0.5, 0.5, 1]

TILE_SELECT_COLOR = [1, 1, 1, 1]
TILE_DESELECT_COLOR = [0.5, 0.5, 0.5, 1]

ALIGN_TO_FACE_NORMAL = True  # Aligns to face normal of a random vertex in the hit object

bge.render.showMouse(True)

def init(cont):
    '''Deselect all pieces'''
    for obj in cont.owner.scene.objects:
        if PIECE_PROPERTY in obj:
            obj.color = PIECE_DESELECT_COLOR
        elif TILE_PROPERTY in obj:
            obj.color = TILE_DESELECT_COLOR
    cont.owner['activePiece'] = None
    cont.owner['activeTile'] = None
    cont.script = __name__ + '.run'

def run(cont):
    # Get teh object the mouse is over by casting a ray from the camera:
    cam = cont.owner.scene.active_camera
    pos = bge.logic.mouse.position
    hit_obj = cam.getScreenRay(pos[0], pos[1], 100)
 
 
    if bge.logic.mouse.events[bge.events.LEFTMOUSE] == bge.logic.KX_INPUT_JUST_ACTIVATED:
        if hit_obj is not None:
            # Select the piece
            if PIECE_PROPERTY in hit_obj:
                select_piece(cont, hit_obj)
             
            elif TILE_PROPERTY in hit_obj and cont.owner['activePiece'] is not None:
                move_piece_to_tile(cont.owner['activePiece'], cont.owner['activeTile'])
         
        else:
            deselect_piece(cont)
         
 
    hover(cont, hit_obj)

def move_piece_to_tile(piece, tile_obj):
    piece.worldTransform = tile_obj.worldTransform
    if ALIGN_TO_FACE_NORMAL:
        # Align the tile to the direction of tile (using a random vertex - I hope the tiles are flat)
        mesh = tile_obj.meshes[0]
        vertex = mesh.getVertex(0, 0)
        normal = vertex.normal  # Vertex's normal direction
        up = tile_obj.worldOrientation * normal  # Rotate normal by tile objects orientation
        piece.alignAxisToVect(up, 2, 1)
 

def deselect_piece(cont):
    '''Deselects the currently selected piece'''
    if cont.owner['activePiece'] is not None:
        cont.owner['activePiece'].color = PIECE_DESELECT_COLOR
        cont.owner['activePiece'] = None

def select_piece(cont, hit_obj):
    '''Deselect current piece and select the newpiece the mouse is over '''
    #Deselect old piece
    deselect_piece(cont)

    #Select the new piece
    hit_obj.color = PIECE_SELECT_COLOR
    cont.owner['activePiece'] = hit_obj
     
     
def hover(cont, hit_obj):
    '''Does tile highlighting/hovering'''
    if hit_obj is not None:
        #Remove highlighting of existing tile
        if cont.owner['activeTile'] is not None:
            cont.owner['activeTile'].color = TILE_DESELECT_COLOR
           
        #If we're hovering over a tile, highlight it
        if TILE_PROPERTY in hit_obj:
            cont.owner['activeTile'] = hit_obj
            hit_obj.color = TILE_SELECT_COLOR
    else:
        # If we're not hovering over a tile, remove the highlighting and deselect the tile
        if cont.owner['activeTile'] is not None:
            cont.owner['activeTile'].color = TILE_DESELECT_COLOR
        cont.owner['activeTile'] = None

Thursday, September 3, 2009