2019-03-29 08:12:41 +01:00
|
|
|
/**
|
|
|
|
* React Component for displaying a City's UI.
|
|
|
|
* This UI shows all of the available locations in the city, and lets the player
|
|
|
|
* visit those locations
|
|
|
|
*/
|
|
|
|
import * as React from "react";
|
|
|
|
|
|
|
|
import { City } from "../City";
|
|
|
|
import { LocationName } from "../data/LocationNames";
|
2021-04-03 02:14:35 +02:00
|
|
|
import { Settings } from "../../Settings/Settings";
|
2019-03-29 08:12:41 +01:00
|
|
|
|
|
|
|
import { StdButton } from "../../ui/React/StdButton";
|
|
|
|
|
|
|
|
type IProps = {
|
|
|
|
city: City;
|
|
|
|
enterLocation: (to: LocationName) => void;
|
|
|
|
}
|
|
|
|
|
|
|
|
export class LocationCity extends React.Component<IProps, any> {
|
2021-05-01 09:17:31 +02:00
|
|
|
asciiCity(): React.ReactNode {
|
|
|
|
const LocationLetter = (location: LocationName): JSX.Element => {
|
2021-03-07 22:14:08 +01:00
|
|
|
if (location)
|
2021-05-01 09:17:31 +02:00
|
|
|
return <span
|
|
|
|
key={location}
|
|
|
|
className="tooltip"
|
|
|
|
style={{color: 'blue', whiteSpace: 'nowrap', margin: '0px', padding: '0px', cursor: 'pointer'}}
|
|
|
|
onClick={this.props.enterLocation.bind(this, location)}>
|
2021-03-07 22:14:08 +01:00
|
|
|
X
|
|
|
|
</span>
|
|
|
|
return <span>*</span>
|
|
|
|
}
|
|
|
|
|
|
|
|
const locationLettersRegex = /[A-Z]/g;
|
|
|
|
const letterMap: any = {'A': 0,'B': 1,'C': 2,'D': 3,'E': 4,'F': 5,'G': 6,
|
|
|
|
'H': 7,'I': 8,'J': 9,'K': 10,'L': 11,'M': 12,'N': 13,'O': 14,
|
|
|
|
'P': 15,'Q': 16,'R': 17,'S': 18,'T': 19,'U': 20,'V': 21,'W': 22,
|
|
|
|
'X': 23,'Y': 24,'Z': 25}
|
|
|
|
|
2021-05-01 09:17:31 +02:00
|
|
|
const lineElems = (s: string): JSX.Element[] => {
|
2021-04-30 05:52:56 +02:00
|
|
|
const elems: any[] = [];
|
|
|
|
const matches: any[] = [];
|
2021-03-07 22:14:08 +01:00
|
|
|
let match: any;
|
|
|
|
while ((match = locationLettersRegex.exec(s)) !== null) {
|
|
|
|
matches.push(match);
|
|
|
|
}
|
|
|
|
if (matches.length === 0) {
|
|
|
|
elems.push(s);
|
|
|
|
return elems;
|
|
|
|
}
|
|
|
|
|
|
|
|
for(let i = 0; i < matches.length; i++) {
|
|
|
|
const startI = i === 0 ? 0 : matches[i-1].index+1;
|
|
|
|
const endI = matches[i].index;
|
|
|
|
elems.push(s.slice(startI, endI))
|
|
|
|
const locationI = letterMap[s[matches[i].index]];
|
2021-05-01 09:17:31 +02:00
|
|
|
elems.push(LocationLetter(this.props.city.locations[locationI]))
|
2021-03-07 22:14:08 +01:00
|
|
|
}
|
|
|
|
elems.push(s.slice(matches[matches.length-1].index+1))
|
|
|
|
return elems;
|
|
|
|
}
|
|
|
|
|
2021-05-01 09:17:31 +02:00
|
|
|
const elems: JSX.Element[] = [];
|
2021-03-07 22:14:08 +01:00
|
|
|
const lines = this.props.city.asciiArt.split('\n');
|
|
|
|
for(const i in lines) {
|
|
|
|
elems.push(<pre key={i}>{lineElems(lines[i])}</pre>)
|
|
|
|
}
|
2019-03-29 08:12:41 +01:00
|
|
|
|
2021-04-03 02:14:35 +02:00
|
|
|
return elems;
|
|
|
|
}
|
|
|
|
|
2021-05-01 09:17:31 +02:00
|
|
|
listCity(): React.ReactNode {
|
2021-04-03 02:14:35 +02:00
|
|
|
const locationButtons = this.props.city.locations.map((locName) => {
|
|
|
|
return (
|
|
|
|
<li key={locName}>
|
|
|
|
<StdButton onClick={this.props.enterLocation.bind(this, locName)} text={locName} />
|
|
|
|
</li>
|
|
|
|
)
|
|
|
|
});
|
|
|
|
|
|
|
|
return (
|
|
|
|
<ul>
|
|
|
|
{locationButtons}
|
|
|
|
</ul>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2021-05-01 09:17:31 +02:00
|
|
|
render(): React.ReactNode {
|
2019-03-29 08:12:41 +01:00
|
|
|
return (
|
2021-03-07 22:14:08 +01:00
|
|
|
<>
|
2021-04-03 02:14:35 +02:00
|
|
|
{Settings.DisableASCIIArt ? this.listCity() : this.asciiCity()}
|
2021-03-07 22:14:08 +01:00
|
|
|
</>
|
2019-03-29 08:12:41 +01:00
|
|
|
)
|
|
|
|
}
|
2021-03-07 22:14:08 +01:00
|
|
|
}
|