Skip to content

Commit

Permalink
Map is rendered with a TileDisplay.
Browse files Browse the repository at this point in the history
  • Loading branch information
Trey Tomes committed Dec 19, 2023
1 parent bef2f1f commit b1ffe3d
Show file tree
Hide file tree
Showing 9 changed files with 211 additions and 134 deletions.
102 changes: 18 additions & 84 deletions main.ms
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,14 @@ import "stringUtil"

// Base import.
ensureImport "features"
ensureImport "constants"
ensureImport "math"
ensureImport "sounds"
ensureImport "point"
ensureImport "rect"

SCREEN_MAX_X = 67
SCREEN_MAX_Y = 25
SCREEN_MAX_X = constants.TILE_DISPLAY_WIDTH - 1
SCREEN_MAX_Y = constants.TILE_DISPLAY_HEIGHT - 1
SCREEN_SIZE = point.make(SCREEN_MAX_X, SCREEN_MAX_Y)

// "Graphics"
Expand Down Expand Up @@ -72,65 +73,14 @@ ensureImport "townGenerator"
ensureImport "mapgen"
ensureImport "fov"

ensureImport "constants"
ensureImport "keybindings"
ensureImport "settings"

Display = {}

HeadsUpDisplay = {}

HeadsUpDisplay.initTiles = function(displayNumber)
display(displayNumber).mode = displayMode.tile
display(displayNumber).tileSet = file.loadImage("assets/OEM437_8.png")
display(displayNumber).tileSetTileSize = 8
display(displayNumber).cellSize = 16
display(displayNumber).extent = [constants.TILE_DISPLAY_WIDTH, constants.TILE_DISPLAY_HEIGHT]
return display(displayNumber)
end function

HeadsUpDisplay.init = function(displayNumberFront, displayNumberBack)
self.display = self.initTiles(displayNumberFront)
self.displayBack = self.initTiles(displayNumberBack)
return self
end function

HeadsUpDisplay.setCellBackColor = function(x, y, backColor)
if backColor isa Color then
backColor = backColor.str()
end if
self.displayBack.setCell x, y, constants.TILE_INDEX_SOLID
self.displayBack.setCellTint x, y, backColor
end function

HeadsUpDisplay.setCell = function(x, y, tile, color="#FFFFFF", backColor = "#00000000")
if tile isa string then
tile = code(tile)
end if
if color isa Color then
color = color.str()
end if
self.display.setCell x, y, tile
self.display.setCellTint x, y, color
self.setCellBackColor x, y, backColor
end function

HeadsUpDisplay.clear = function()
for y in range(0, constants.TILE_DISPLAY_YMAX)
for x in range(0, constants.TILE_DISPLAY_XMAX)
self.setCell x, y, 0, color.black, color.clear
end for
end for
end function

HeadsUpDisplay.print = function(text, x, y, color="#FFFFFF", backColor="#00000000")
for ch in text
self.setCell x, y, code(ch), color, backColor
x += 1
end for
end function

Display.initialize = function()
MapDisplay = require("MapDisplay")

HUD = 0
HUD_BACK = 1
PARTICLES_0 = 2
Expand All @@ -149,12 +99,7 @@ Display.initialize = function()
return display(n)
end function

activateTiles = function(n)
return HeadsUpDisplay.init(n)
end function

// Display.hud = activate(0)
Display.hud = HeadsUpDisplay.init(HUD, HUD_BACK)
Display.hud = ui.HeadsUpDisplay.init(HUD, HUD_BACK)

Display.particles = activate(PARTICLES_0)
deactivate(PARTICLES_0)
Expand All @@ -175,21 +120,7 @@ Display.initialize = function()
end if
end function

Display.map = activate(MAP_0)
deactivate(MAP_0)
activate(MAP_1)

Display.flipMap = function()
if display(MAP_0).mode == displayMode.off then
activate(MAP_0)
Display.map = activate(MAP_1)
deactivate(MAP_1)
else
activate(MAP_1)
Display.map = activate(MAP_0)
deactivate(MAP_0)
end if
end function
Display.map = MapDisplay.init(MAP_0, MAP_1)
end function

Display.clear = function()
Expand All @@ -206,10 +137,10 @@ Display.clear = function()

//Display.map.backColor = color.clear
//Display.map.clear()
//Display.flipMap()
//Display.map.flip
//Display.map.backColor = color.clear
//Display.map.clear()
//Display.flipMap()
//Display.map.flip
end function

Service = {}
Expand Down Expand Up @@ -303,19 +234,21 @@ main = function()
Display.clear()

Service.world.map.clearScreen(Display.map)
Service.world.map.draw(Display.map, Service.world.player, renderOffset)
Service.world.map.draw_v1(Display.map, Service.world.player, renderOffset)
drawEntities(Display.map, Service.world.map, renderOffset)
Display.flipMap()
Display.map.flip
Display.map.setCell 5, 5, "*", color.red, color.blue

drawHUD(Display.hud, Service.world.player)

deltaTime = 0
while isRunning
frameStart = time * 1000

updateParticles(deltaTime, Service.world.map, renderOffset)
// updateParticles(deltaTime, Service.world.map, renderOffset) // TODO

// The next step in the game occurs when the player presses a key.
// Service.messages.report("move?")
if key.available then
map = Service.world.map

Expand Down Expand Up @@ -347,10 +280,11 @@ main = function()
Service.messages.report("YOU ARE DEAD!")
end if

Service.world.map.draw(Display.map, Service.world.player, renderOffset)
// Service.messages.report("move")
Service.world.map.draw_v1(Display.map, Service.world.player, renderOffset)
drawEntities(Display.map, Service.world.map, renderOffset)

Display.flipMap()
Display.map.flip

drawHUD(Display.hud, Service.world.player)
end if

Expand Down
95 changes: 95 additions & 0 deletions src/MapDisplay.ms
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
MapDisplay = {}

MapDisplay.initTiles = function(displayNumber)
dsp = new TileDisplay // Create a detached TileDisplay.
// display(displayNumber).mode = displayMode.tile
dsp.tileSet = file.loadImage("assets/OEM437_8.png")
dsp.tileSetTileSize = 8
dsp.cellSize = 16
dsp.extent = [constants.TILE_DISPLAY_WIDTH, constants.TILE_DISPLAY_HEIGHT]
return dsp
end function

MapDisplay.init = function(displayNumberFront, displayNumberBack)
self.isActive = false

self.displayNumberFront = displayNumberFront
self.displayNumberBack = displayNumberBack

self.activeDisplay = 0

self.display0 = self.initTiles(displayNumberFront)
self.display0Back = self.initTiles(displayNumberBack)

self.display1 = self.initTiles(displayNumberFront)
self.display1Back = self.initTiles(displayNumberBack)

self.display = self.display0
self.displayBack = self.display0Back
self.display.install self.displayNumberFront
self.displayBack.install self.displayNumberBack

return self
end function

MapDisplay.flip = function()
if self.activeDisplay == 0 then
self.activeDisplay = 1

self.display = self.display1
self.displayBack = self.display1Back

self.display0.install self.displayNumberFront
self.display0Back.install self.displayNumberBack
else
self.activeDisplay = 0

self.display = self.display0
self.displayBack = self.display0Back

self.display1.install self.displayNumberFront
self.display1Back.install self.displayNumberBack
end if
return self
end function

MapDisplay.setCellBackColor = function(x, y, backColor)
if backColor isa Color then
backColor = backColor.str()
end if
self.displayBack.setCell x, y, constants.TILE_INDEX_SOLID
self.displayBack.setCellTint x, y, backColor
end function

MapDisplay.setCell = function(x, y, tile, color="#FFFFFF", backColor = "#00000000")
if tile isa string then
tile = code(tile)
end if
if tile > 255 then
tile = code("?")
end if

if color isa Color then
color = color.str()
end if
self.display.setCell x, y, tile
self.display.setCellTint x, y, color
self.setCellBackColor x, y, backColor
end function

MapDisplay.clear = function()
for y in range(0, constants.TILE_DISPLAY_YMAX)
for x in range(0, constants.TILE_DISPLAY_XMAX)
self.setCell x, y, 0, color.black, color.clear
end for
end for
end function

MapDisplay.print = function(text, x, y, color="#FFFFFF", backColor="#00000000")
for ch in text
self.setCell x, y, code(ch), color, backColor
x += 1
end for
end function

return MapDisplay
8 changes: 4 additions & 4 deletions src/entity.ms
Original file line number Diff line number Diff line change
Expand Up @@ -62,16 +62,16 @@ Entity.canSee = function(pnt)
return deltaX * deltaX + deltaY * deltaY <= self.rangeOfVision * self.rangeOfVision
end function

Entity.draw = function(textDisplay, renderOffset)
Entity.draw = function(dsp, renderOffset)
//self.tile.draw(self.position.x, self.position.y)
x = renderOffset.x + self.position.x
y = renderOffset.y + self.position.y

colorMod = self.currentHP / self.maxHP
foregroundColor = Color.lerp(Color.red, self.tile.foregroundColor, colorMod)
textDisplay.setCellColor x, y, foregroundColor.str()
textDisplay.setCellBackColor x, y, self.tile.backgroundColor.str()
textDisplay.setCell x, y, self.tile.char
// dsp.setCellColor x, y, foregroundColor.str()
// dsp.setCellBackColor x, y, self.tile.backgroundColor.str()
dsp.setCell x, y, self.tile.char, foregroundColor, self.tile.backgroundColor
end function

Entity.act = function(map)
Expand Down
3 changes: 2 additions & 1 deletion src/factories/entities.ms
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
// Entity Factory

makePlayer = function()
t = tile.make(char(57872), Color.white, Color.black, true, false)
// t = tile.make(char(57872), Color.white, Color.black, true, false) // TODO
t = tile.make(char(2), Color.white, Color.black, true, false)
e = (new entity.Entity).init(t, races.human, classes.fighter)

e.name = "player" //e.race.name
Expand Down
Loading

0 comments on commit b1ffe3d

Please sign in to comment.