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

Function adjustTerrain

app/lib/level-generation.js:1625–1698  ·  view source on GitHub ↗
({ offset, size, level, visitedPositions })

Source from the content-addressed store, hash-verified

1623}
1624
1625function adjustTerrain ({ offset, size, level, visitedPositions }) {
1626 // Turn every unused square to water (Junior Wall)
1627 // Cut off unnecessary squares on top & right
1628 // Adjust camera to match the new size
1629 // console.log('Need to adjust terrain', offset, size, level, visitedPositions)
1630 const visitedPositionsOffset = {}
1631 for (const pos of visitedPositions) {
1632 const offsetPos = pos.copy().add(offset)
1633 visitedPositionsOffset[offsetPos.x] = visitedPositionsOffset[offsetPos.x] || {}
1634 visitedPositionsOffset[offsetPos.x][offsetPos.y] = true
1635 }
1636
1637 // Turn any unused floors (walkable beach sand) into walls (unwalkable ocean)
1638 // Include one row past the right/top as the natural border
1639 for (let col = 0; col <= size.cols; ++col) {
1640 for (let row = 0; row <= size.rows; ++row) {
1641 const pos = { x: col, y: row }
1642 let thang = findThangAt(pos, level.thangs)
1643 if (thang) {
1644 const spriteName = thangTypesToSpriteNames[thang.thangType]
1645 if (spriteName === 'Goal Junior') {
1646 const floor = findThangAt(pos, _.without(level.thangs, thang))
1647 thang = floor // Turn the floor under the raft into a wall (since raft goes on the ocean)
1648 // TODO: turn the raft facing the ocean
1649 } else if (visitedPositionsOffset[col]?.[row]) {
1650 continue
1651 } else if (significantSpriteNames.concat(['Hero Placeholder']).includes(spriteName)) {
1652 continue
1653 }
1654 // We have a floor here and we never use it, so turn it into a wall
1655 thang.thangType = spriteNamesToThangTypes['Junior Wall'].original
1656 thang.id = thang.id.replace(/Floor/, 'Wall')
1657 const physicalConfig = _.find(thang.components, (component) => component.original === PhysicalID).config
1658 physicalConfig.pos.z = 0.5
1659 }
1660 }
1661 }
1662
1663 // Cut off unnecessary squares on top & right
1664 for (let col = -1; col <= maxCols; ++col) {
1665 for (let row = -1; row <= maxRows; ++row) {
1666 if (col <= size.cols && row <= size.rows) { continue }
1667 const pos = { x: col, y: row }
1668 const thang = findThangAt(pos, level.thangs)
1669 _.pull(level.thangs, thang)
1670 }
1671 }
1672
1673 // Adjust camera to match the new size
1674 const bounds = level.scripts[0].noteChain[0].surface.focus.bounds
1675 const largest = size.cols > size.rows * 20 / 17 ? size.cols : size.rows
1676 if (largest === size.cols) {
1677 bounds[1].x = 4 + 8 * largest + 4
1678 bounds[1].y = (4 + 8 * largest + 4) * 17 / 20
1679 } else {
1680 bounds[1].x = (4 + 8 * largest + 4) * 20 / 17
1681 bounds[1].y = 4 + 8 * largest + 4
1682 }

Callers 1

Calls 1

findThangAtFunction · 0.85

Tested by

no test coverage detected