MCPcopy Create free account
hub / github.com/codecombat/codecombat / shiftLayout

Function shiftLayout

app/lib/level-generation.js:1558–1623  ·  view source on GitHub ↗
({ thangs, visitedPositions })

Source from the content-addressed store, hash-verified

1556}
1557
1558function shiftLayout ({ thangs, visitedPositions }) {
1559 // Keep track of the min/max position of all significant Thangs plus places the hero visits
1560 // Once we know the needed bounds and offset of the level, we can shift everything to start from row 0, col 0
1561 // We maintain standard level viewport aspect ratio, so we may center the level along the major axis
1562 let [minCol, minRow, maxCol, maxRow] = [9001, 9001, -9001, -9001]
1563
1564 for (const pos of visitedPositions) {
1565 minCol = Math.min(minCol, pos.x)
1566 minRow = Math.min(minRow, pos.y)
1567 maxCol = Math.max(maxCol, pos.x)
1568 maxRow = Math.max(maxRow, pos.y)
1569 }
1570
1571 for (const thang of thangs) {
1572 const spriteName = thangTypesToSpriteNames[thang.thangType]
1573 if (!significantSpriteNames.concat(['Hero Placeholder']).includes(spriteName)) {
1574 continue
1575 }
1576 const complexPos = _.find(thang.components, (component) => component.original === PhysicalID).config.pos
1577 const simplePos = simplifyPos(complexPos)
1578 minCol = Math.min(minCol, simplePos.x)
1579 minRow = Math.min(minRow, simplePos.y)
1580 maxCol = Math.max(maxCol, simplePos.x)
1581 maxRow = Math.max(maxRow, simplePos.y)
1582 // if (simplePos.x >= placeholderPositionSimple || simplePos.y >= placeholderPositionSimple) {
1583 // console.log('Found unmoved', spriteName, thang.id, 'at', simplePos.x, simplePos.y)
1584 // }
1585 }
1586
1587 const size = { cols: maxCol - minCol + 1, rows: maxRow - minRow + 1 }
1588 size.cols = Math.max(size.cols, 3)
1589 size.rows = Math.max(size.rows, 2)
1590
1591 const targetAspectRatio = 20 / 17
1592 const currentAspectRatio = size.cols / size.rows
1593
1594 let offsetX = 0
1595 let offsetY = 0
1596
1597 if (currentAspectRatio > targetAspectRatio) {
1598 // Width is the major dimension, adjust height
1599 const targetRows = Math.round(size.cols / targetAspectRatio)
1600 offsetY = Math.floor((targetRows - size.rows) / 2)
1601 size.rows = targetRows
1602 } else {
1603 // Height is the major dimension, adjust width
1604 const targetCols = Math.round(size.rows * targetAspectRatio)
1605 offsetX = Math.floor((targetCols - size.cols) / 2)
1606 size.cols = targetCols
1607 }
1608
1609 const offset = { x: -minCol + offsetX, y: -minRow + offsetY }
1610
1611 for (const thang of thangs) {
1612 const spriteName = thangTypesToSpriteNames[thang.thangType]
1613 if (!significantSpriteNames.concat(['Hero Placeholder']).includes(spriteName)) {
1614 continue
1615 }

Callers 1

Calls 1

simplifyPosFunction · 0.85

Tested by

no test coverage detected