sdfgeoff

move_to_tile.py

Jan 12th, 2017
166
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. '''Script for:
  2. http://blender.stackexchange.com/questions/70761/how-do-i-move-game-pieces-around-a-three-dimensional-board-in-a-turn-based-game
  3. '''
  4.  
  5. import bge
  6.  
  7. PIECE_PROPERTY = 'PIECE'
  8. TILE_PROPERTY = 'TILE'
  9.  
  10. PIECE_SELECT_COLOR = [1, 1, 1, 1]
  11. PIECE_DESELECT_COLOR = [0.5, 0.5, 0.5, 1]
  12.  
  13. TILE_SELECT_COLOR = [1, 1, 1, 1]
  14. TILE_DESELECT_COLOR = [0.5, 0.5, 0.5, 1]
  15.  
  16. ALIGN_TO_FACE_NORMAL = True  # Aligns to face normal of a random vertex in the hit object
  17.  
  18. bge.render.showMouse(True)
  19.  
  20. def init(cont):
  21.     '''Deselect all pieces'''
  22.     for obj in cont.owner.scene.objects:
  23.         if PIECE_PROPERTY in obj:
  24.             obj.color = PIECE_DESELECT_COLOR
  25.         elif TILE_PROPERTY in obj:
  26.             obj.color = TILE_DESELECT_COLOR
  27.     cont.owner['activePiece'] = None
  28.     cont.owner['activeTile'] = None
  29.     cont.script = __name__ + '.run'
  30.  
  31. def run(cont):
  32.     # Get teh object the mouse is over by casting a ray from the camera:
  33.     cam = cont.owner.scene.active_camera
  34.     pos = bge.logic.mouse.position
  35.     hit_obj = cam.getScreenRay(pos[0], pos[1], 100)
  36.    
  37.    
  38.     if bge.logic.mouse.events[bge.events.LEFTMOUSE] == bge.logic.KX_INPUT_JUST_ACTIVATED:
  39.         if hit_obj is not None:
  40.             # Select the piece
  41.             if PIECE_PROPERTY in hit_obj:
  42.                 select_piece(cont, hit_obj)
  43.                
  44.             elif TILE_PROPERTY in hit_obj and cont.owner['activePiece'] is not None:
  45.                 move_piece_to_tile(cont.owner['activePiece'], cont.owner['activeTile'])
  46.            
  47.         else:
  48.             deselect_piece(cont)
  49.            
  50.    
  51.     hover(cont, hit_obj)
  52.  
  53. def move_piece_to_tile(piece, tile_obj):
  54.     piece.worldTransform = tile_obj.worldTransform
  55.     if ALIGN_TO_FACE_NORMAL:
  56.         # Align the tile to the direction of tile (using a random vertex - I hope the tiles are flat)
  57.         mesh = tile_obj.meshes[0]
  58.         vertex = mesh.getVertex(0, 0)
  59.         normal = vertex.normal  # Vertex's normal direction
  60.         up = tile_obj.worldOrientation * normal  # Rotate normal by tile objects orientation
  61.         piece.alignAxisToVect(up, 2, 1)
  62.    
  63.  
  64. def deselect_piece(cont):
  65.     '''Deselects the currently selected piece'''
  66.     if cont.owner['activePiece'] is not None:
  67.         cont.owner['activePiece'].color = PIECE_DESELECT_COLOR
  68.         cont.owner['activePiece'] = None
  69.  
  70. def select_piece(cont, hit_obj):
  71.     '''Deselect current piece and select the newpiece the mouse is over '''
  72.     #Deselect old piece
  73.     deselect_piece(cont)
  74.  
  75.     #Select the new piece
  76.     hit_obj.color = PIECE_SELECT_COLOR
  77.     cont.owner['activePiece'] = hit_obj
  78.        
  79.        
  80. def hover(cont, hit_obj):
  81.     '''Does tile highlighting/hovering'''
  82.     if hit_obj is not None:
  83.         #Remove highlighting of existing tile
  84.         if cont.owner['activeTile'] is not None:
  85.             cont.owner['activeTile'].color = TILE_DESELECT_COLOR
  86.            
  87.         #If we're hovering over a tile, highlight it
  88.         if TILE_PROPERTY in hit_obj:
  89.             cont.owner['activeTile'] = hit_obj
  90.             hit_obj.color = TILE_SELECT_COLOR
  91.     else:
  92.         # If we're not hovering over a tile, remove the highlighting and deselect the tile
  93.         if cont.owner['activeTile'] is not None:
  94.             cont.owner['activeTile'].color = TILE_DESELECT_COLOR
  95.         cont.owner['activeTile'] = None
RAW Paste Data