Skip to content

Commit

Permalink
add some stuff but it's not a game yet
Browse files Browse the repository at this point in the history
  • Loading branch information
isthisstackoverflow committed Jan 29, 2023
1 parent e97af60 commit eddaea0
Show file tree
Hide file tree
Showing 10 changed files with 174 additions and 32 deletions.
4 changes: 3 additions & 1 deletion pages/ratstack/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@
<body>
<div id="map"></div>
<div id="user-interface">
¯¯¯¯¯¯¯¯¯¯¯¯¯🐀 RATS ATE MY PROJECT.
<header id="headerbox"></header>
<div id="modalbox"></div>
<footer id="infobox"></footer>
</div>
<script type="module" src="./index.js"></script>
</body>
Expand Down
36 changes: 35 additions & 1 deletion pages/ratstack/index.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,37 @@
import { createMap } from './map/createMap.js'
import { getRandomPart } from './map/getRandomPart.js';
import { districtSource } from './map/districts.js';

createMap()
const map = createMap()

const headerbox = document.getElementById('headerbox')
const infobox = document.getElementById('infobox')
const modalbox = document.getElementById('modalbox')

const playerFaction = getRandomPart(districtSource.getFeatures())
window.goto = (duration = 2000) => map.getView().fit(playerFaction.getGeometry(), { duration })

const updateHeader = (emoji) => headerbox.innerHTML = `
<span>Your Faction</span>
<h1>${emoji} ${playerFaction.properties.factionName}</h1>
`
updateHeader('🐀')
goto(5000)

const infodefault = 'Hover any region to display information.'
infobox.innerHTML = infodefault
map.on('pointermove', function (e) {
infobox.innerHTML = infodefault
map.forEachFeatureAtPixel(e.pixel, function (feature) {
infobox.innerHTML = `
Faction: ${feature.properties.factionName}<br />
Rats: ${feature.properties.rats}<br />
Food: ${feature.properties.food}
`
return true
})
})

modalbox.innerHTML = `
OH MAN I FORGOT TO IMPLEMENT GAMEPLAY
`
8 changes: 4 additions & 4 deletions pages/ratstack/map/createMap.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { districtVector, select } from "./districts.js"
import { districtVector, hover } from "./districts.js"
const { defaults } = ol.interaction.defaults

const { Map, View } = ol
const { Tile } = ol.layer
Expand All @@ -21,8 +22,7 @@ export const createMap = () => new Map({
}),
districtVector
],
interactions: [
select
],
controls: [],
interactions: [hover],
target,
});
33 changes: 24 additions & 9 deletions pages/ratstack/map/districts.js
Original file line number Diff line number Diff line change
@@ -1,34 +1,49 @@
// Knowing that one day JSON can be loaded fills you with determination.
// import districts from './districts.json' assert {type: 'json'}
import { districts } from './districtsjson.js'
import { enrat } from './enrat/index.js'

const VectorLayer = ol.layer.Vector
const VectorSource = ol.source.Vector
const { Select } = ol.interaction
const { GeoJSON } = ol.format
const { pointerMove } = ol.events.condition
const { Stroke, Style, Fill } = ol.style
const { Stroke, Style, Fill, Text } = ol.style

const style = new Style({
stroke: new Stroke({ color: '#333', width: 1 }),
fill: new Fill({ color: 'rgba(255,255,255,0)' })
const text = (feature) => new Text({
font: '22px sans-serif',
overflow: true,
text: feature.properties.factionName,
fill: new Fill({color: feature.properties.hex}),
stroke: new Stroke({color: '#16161D', width: '2'}),
textAlign: 'center'
})

const hoverStyle = new Style({
stroke: new Stroke({ color: '#000', width: 3 }),
fill: new Fill({ color: 'rgba(255,255,255,0.0)' })
const style = (feature) => new Style({
stroke: new Stroke({ color: '#333', width: 2 }),
fill: new Fill({ color: feature.properties.hex + '66' }),
text: text(feature)
})

const hoverStyle = (feature) => new Style({
stroke: new Stroke({ color: '#16161D', width: 4 }),
fill: new Fill({ color: feature.properties.hex + 'CC' }),
text: text(feature)
})

export const districtSource = new VectorSource({
features: (new GeoJSON({ featureProjection: 'EPSG:3857' })).readFeatures(districts)
features:
new GeoJSON({ featureProjection: 'EPSG:3857' })
.readFeatures(districts)
.map(enrat)
})

export const districtVector = new VectorLayer({
source: districtSource,
style
})

export const select = new Select({
export const hover = new Select({
condition: pointerMove,
style: hoverStyle
})
46 changes: 46 additions & 0 deletions pages/ratstack/map/enrat/getFactionName.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import { getRandomPart } from '../getRandomPart.js'

const hadThat = ['']

const starts = [
'Red', 'Orange', 'Blue', 'Yellow',
'Green', 'Black', 'White', 'Ultraviolet',
'Deadly', 'Amazing', 'Dangerous', 'Astonishing',
'Töfte', 'Knorke', 'Schnieke',
'Sewer', 'Underground',
'Tunnel',
''
]

const mids = [
'Rodent', 'Rat', 'Critter', 'Creature', 'Vermin',
'Tea', 'Sausage', 'Schinken', 'Cheese',
'Nager',
'Snake',
''
]

const ends = [
'Killers', 'Sacks', 'Boys', 'Dimwits',
'Masters', 'Heroes', 'Team', 'Force',
'Pack', 'Horde', 'Flut',
'Rules',
''
]

/**
* @returns {string} randomized faction name
*/
export const getFactionName = () => {
let name = ''
// reroll until unique - need lots of source names
while (hadThat.includes(name)) {
name = [
getRandomPart(starts),
getRandomPart(mids),
getRandomPart(ends)
].join(' ').trim()
}
hadThat.push(name)
return name
}
5 changes: 5 additions & 0 deletions pages/ratstack/map/enrat/getRandomHex.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export const getRandomHex = () =>
'#' +
Math.round(Math.random() * 0xffffff)
.toString(16)
.padStart(6, '0')
14 changes: 14 additions & 0 deletions pages/ratstack/map/enrat/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { getFactionName } from "./getFactionName.js"
import { getRandomHex } from "./getRandomHex.js"

const makeFeatureProperties = () => ({
factionName: getFactionName(),
hex: getRandomHex(),
rats: Math.floor(Math.random() * 900) + 101,
food: Math.floor(Math.random() * 900) + 101,
})

export const enrat = feature => {
feature.properties = makeFeatureProperties()
return feature
}
5 changes: 5 additions & 0 deletions pages/ratstack/map/getRandomPart.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
/**
* @param {*[]} a array of something
* @returns {*} random value from array
*/
export const getRandomPart = a => a[Math.floor(Math.random() * a.length)]
16 changes: 0 additions & 16 deletions pages/ratstack/src/lib/randomColor.js

This file was deleted.

39 changes: 38 additions & 1 deletion pages/ratstack/style.css
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
* {
font-family: 'Courier New', monospace;
color: #16161D;
}

html, body {
Expand All @@ -22,4 +23,40 @@ html, body {

#user-interface * {
pointer-events: all;
}
}

#headerbox {
position: absolute;
top: 0;
background: white;
border: 1px solid #16161D;
padding: 1em;
margin: 1em;
display: flex;
flex-direction: column;
align-items: center;
left: 50%;
transform: translateX(-50%);
}

#headerbox h1 {
margin: 0;
white-space: nowrap;
}

#infobox {
position: absolute;
bottom: 0;
left: 0;
right: 0;
background: white;
border: 1px solid #16161D;
padding: 1em;
margin: 1em;
}

#modalbox {
position: absolute;
background: white;
color: #16161D;
}

0 comments on commit eddaea0

Please sign in to comment.