Guest User

Untitled

a guest
Jun 24th, 2018
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.71 KB | None | 0 0
  1. function generateMap(width, height, monsters)
  2.     map = {}                    -- map content
  3.     map_visibility = {}         -- map visibility
  4.     for y=1, height do
  5.         table.insert(map, {})
  6.         table.insert(map_visibility, {})
  7.         for x=1, width do
  8.             table.insert(map[#map], 0)
  9.             table.insert(map_visibility[#map_visibility], 0)
  10.         end
  11.     end
  12.     for id=1, 2 do              -- for player id 1 and 2
  13.         for n=1, monsters do    -- for n monsters
  14.             while true do       -- find random position
  15.                 x = math.random(width)
  16.                 y = math.random(height)
  17.                 if map[y][x] == 0 then break end
  18.             end
  19.             map[y][x] = id      -- set it
  20.         end
  21.     end
  22. end
  23.  
  24. function love.load()
  25.     generateMap(5, 5, 2)        -- 5x5 map with 2 monsters per player
  26.     turn = math.random(2)       -- random start
  27.     nextTurn()                  -- for message
  28. end
  29.  
  30. function nextTurn()
  31.     turn = turn + 1
  32.     if turn > 2 then
  33.         turn = 1
  34.     end
  35.     selection = { x = -1, y = -1 }
  36.     print("PLAYER " .. tostring(turn) .. "'s TURN NOW")
  37. end
  38.  
  39. function love.draw()
  40.     for y=1, #map do
  41.         for x=1, #map[y] do
  42.             if map_visibility[y][x] == 0 then
  43.                 love.graphics.setColor(255, 255, 255, 255)
  44.                 love.graphics.rectangle("line", x * 32, y * 32, 32, 32)
  45.             else
  46.                 love.graphics.setColor(255, 255, 255, 255)  -- empty field (white)
  47.                 if map[y][x] == 1 then      -- player 1 monster (red)
  48.                     love.graphics.setColor(255, 0, 0, 255)
  49.                 elseif map[y][x] == 2 then  -- player 2 monster (blue)
  50.                     love.graphics.setColor(0, 0, 255, 255)
  51.                 end
  52.                 love.graphics.rectangle("fill", x * 32, y * 32, 32, 32)
  53.             end
  54.         end
  55.     end
  56. end
  57.  
  58. function love.mousepressed(x, y, button)
  59.     x = math.floor(x / 32)  -- round down to get "real" coordinates
  60.     y = math.floor(y / 32)
  61.     if y >= 1 and y <= #map and x >= 1 and x <= #map[y] then    -- check if valid
  62.         if button == "l" then   -- left mouse button
  63.             if selection.x ~= -1 and map[y][x] ~= turn and map_visibility[y][x] == 1 then
  64.                 if x >= selection.x - 1 and x <= selection.x + 1 and y >= selection.y - 1 and y <= selection.y + 1 then
  65.                     map[y][x] = map[selection.y][selection.x]   -- moved if a monster was selected
  66.                     map[selection.y][selection.x] = 0
  67.                     nextTurn()
  68.                 else
  69.                     print("Illegal move!")
  70.                 end
  71.             elseif map_visibility[y][x] == 0 then
  72.                 map_visibility[y][x] = 1    -- set visibile if hidden
  73.                 nextTurn()
  74.             elseif map[y][x] ~= 0 then
  75.                 if map[y][x] == turn then
  76.                     selection.x = x
  77.                     selection.y = y
  78.                 else
  79.                     print("You cannot move another player's monster")
  80.                 end
  81.             end
  82.         elseif button == "r" then   -- right mouse button
  83.             if map_visibility[y][x] == 0 then
  84.                 if map[y][x] == 0 then
  85.                     print("Field: EMPTY")
  86.                 else
  87.                     print("Field: PLAYER " + tostring(map[y][x]) + " MONSTER")
  88.                 end
  89.                 nextTurn()
  90.             end
  91.         end
  92.     else
  93.         selection = { x = -1, y = -1 }
  94.     end
  95. end
Add Comment
Please, Sign In to add comment