aboutsummaryrefslogtreecommitdiff
path: root/core/map.lua
blob: e6732eefb6371ce732fed0d70e49171f66d6351c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
Class = require("libs.hump.class")
Gamestate = require("libs.hump.gamestate")
STI = require("libs.sti")
local Moonshine = require("libs.moonshine")
Camera = require("libs.hump.camera")
Windfield = require("libs.windfield")
require("libs.tserial")
require("core.notifications")
require("entities.enemy")
require("entities.pickable")
anim8 = require("libs.anim8.anim8")
local zoomFactor = 3
local player = {
  x=0,
  y=0,
  sprite=love.graphics.newImage("assets/images/player_demo.png"),
  speed=70,
  dir="down",
  spriteSheet= love.graphics.newImage("assets/images/aswani.png")
}
local fullscreen = true

local windowWidth, windowHeight = love.graphics.getDimensions()
local _w = windowWidth/zoomFactor
local _h = windowHeight/zoomFactor

Map = Class {
  __include=Gamestate,
  init = function(self, mapName, debug)
    self.debug = debug and debug or false
    initAnimations()
    _gameWorld = Windfield.newWorld(0,0)
    _gameWorld:setQueryDebugDrawing(debug and debug or false) -- Remove when deploy
    _gameWorld:addCollisionClass('interactive')
    _gameWorld:addCollisionClass('pickable')
    _gameWorld:addCollisionClass('enemy')
    _gameWorld:addCollisionClass('player')
    effect = Moonshine(windowWidth, windowHeight, Moonshine.effects.crt)
                    .chain(Moonshine.effects.vignette)
                    .chain(Moonshine.effects.scanlines)
                    .chain(Moonshine.effects.chromasep)

    effect.scanlines.thickness = .2
    effect.scanlines.opacity = .5
    effect.chromasep.angle = 1
    effect.chromasep.radius = 3

    notifications = Notifications(zoomFactor)
    camera = Camera()
    camera:zoom(zoomFactor)
    currentMap = STI("assets/maps/"..mapName..".lua")
    if currentMap.layers["entities"] then
        for i,obj in pairs(currentMap.layers["entities"].objects) do
          if obj.name=="player" and obj.type=="player" then
            player.x = obj.x
            player.y = obj.y
            player.collider = _gameWorld:newBSGRectangleCollider(player.x, player.y, 16, 16, 0)
            
            player.collider:setFixedRotation(true)
            player.collider:setCollisionClass('player')
            player.collider:setObject(player)
          end

          if obj.type=="interactive" then
            local collider = _gameWorld:newRectangleCollider(obj.x, obj.y, obj.width, obj.height)
            local interactiveData = {
              type = obj.type,
              interactive_type = obj.properties.interactive_type,
              message = obj.properties.message and obj.properties.message or nil,
              talker_data = obj.properties.talker_data and obj.properties.talker_data or nil
            }
            collider:setObject(interactiveData)
            collider:setCollisionClass('interactive')
            collider:setType("static")
          end

          if obj.type=="pickable" then
            local pickable = Pickable(obj.x, obj.y, obj.width, obj.height, obj.name and obj.name or nil, _gameWorld)
            pickable:setCollider(obj.properties)
            table.insert(self.pickables, pickable)
          end

          if obj.type=="enemy" then
            local enemy = Enemy(obj.x, obj.y, _gameWorld)
            enemy:setCollider()
            table.insert(self.enemies, enemy)
          end
        end
    end

    if currentMap.layers["walls"] then
      for i,obj in pairs(currentMap.layers["walls"].objects) do
        local wall = _gameWorld:newRectangleCollider(obj.x, obj.y, obj.width, obj.height)
        wall:setType("static")
      end
    end
  end,
  entities={},
  enemies={},
  pickables={},
  debug=false
}

function Map:update(dt)
  local vx = 0
  local vy = 0
  local isMoving = false
  if love.keyboard.isDown("w") then
    vy = player.speed * -1
    player.dir = player.animations.up
    isMoving = true
  end
  
  if love.keyboard.isDown("a") then
    vx = player.speed * -1
    player.dir = player.animations.left
    isMoving = true
  end
  
  if love.keyboard.isDown("s") then
    vy = player.speed
    player.dir = player.animations.down
    isMoving = true
  end

  if love.keyboard.isDown("d") then
    vx = player.speed
    player.dir = player.animations.right
    isMoving = true
  end

  if isMoving == false then
    player.dir:gotoFrame(2)
  end
  player.dir:update(dt)

  player.collider:setLinearVelocity(vx, vy)

  _gameWorld:update(dt)
  player.x = player.collider:getX() - 8
  player.y = player.collider:getY() - 8
  player.collider:setObject(player)

  camera:lookAt(player.x, player.y)
  currentMap:update(dt)

  if camera.x < _w/2 then
    camera.x = _w/2
  end

  if camera.y < _h/2 then
    camera.y = _h/2
  end

  local mapWidth = currentMap.width * currentMap.tilewidth
  local mapHeight = currentMap.height * currentMap.tileheight

  if camera.x > (mapWidth - _w/2) then
    camera.x = (mapWidth - _w/2)
  end

  if camera.y > (mapHeight - _h/2) then
    camera.y = (mapHeight - _h/2)
  end

  if player.collider:enter('interactive') or player.collider:enter('pickable')then
    local _interColliderData = player.collider:getEnterCollisionData('interactive') or player.collider:getEnterCollisionData('pickable')
    local interactiveCollider = _interColliderData.collider
    local interactiveData = interactiveCollider:getObject()
    if interactiveData.message then 
      notifications:send(interactiveData.message)
    end
  end
  notifications:update(dt)
  for i,enemy in pairs(self.enemies) do
    if enemy.dead then
      table.remove(self.enemies, i)
    else
      enemy:update(dt)
    end
  end

  for i,pickable in pairs(self.pickables) do
    if pickable.acquired then
      table.remove(self.pickables, i)
    else
      pickable:update(dt)
    end
  end
  
end

function Map:draw()
  effect(function()
    camera:attach()
      drawMapLayer("water")
      drawMapLayer("ground")
      drawMapLayer("decorations")
      --love.graphics.draw(player.sprite, player.x, player.y)
      player.dir:draw(player.spriteSheet, player.x, player.y, nil, 0.5, 0.5)
      drawMapLayer("foreground")
      if self.debug then
        _gameWorld:draw() -- Debug Collision Draw
      end
      for i,enemy in pairs(self.enemies) do
        enemy:draw()
      end

      for i,pickable in pairs(self.pickables) do
        pickable:draw()
      end
    camera:detach()
    notifications:draw()
  end)

end

function Map:keypressed(key, scancode)
  if(scancode=="f4") then
    fullscreen = not fullscreen
    love.window.setFullscreen(fullscreen)
    resize()
  end
end

function drawMapLayer(layerName)
  if currentMap.layers[layerName].visible then
    currentMap:drawLayer(currentMap.layers[layerName])
  end
end

function resize()
	_width, _height = love.graphics.getDimensions()
  if not(fullscreen) then
		_width = 1280
		_height = 720
	end
	effect.resize(_width,_height)
  notifications.calculateWindowDimensions()
end

function initAnimations()
  player.grid=anim8.newGrid(32,32,player.spriteSheet:getWidth(), player.spriteSheet:getHeight())
  
  player.animations = {}
  player.animations.down = anim8.newAnimation(player.grid('1-3', 1),0.1)
  player.animations.left = anim8.newAnimation(player.grid('1-3', 2),0.1)
  player.animations.right = anim8.newAnimation(player.grid('1-3', 3),0.1)
  player.animations.up = anim8.newAnimation(player.grid('1-3', 4),0.1)
  player.dir = player.animations.down
end

return Map