Skip to content
Snippets Groups Projects
Commit 1a23cf07 authored by Louis's avatar Louis :fire:
Browse files

Add win logic and flags

parent 4ecef87c
No related branches found
No related tags found
No related merge requests found
public/flag-text.png

129 B

public/flag.png 0 → 100644 LFS
public/flag.png

129 B

public/win-text.png

128 B

......@@ -2,7 +2,7 @@ import React, {Component} from 'react'
import './App.css';
import Level from './render/Level'
import LevelData from './game/Level'
import testLevel from './data/testlevel1'
import testLevel from './data/testlevel2'
import Editor from './editor/Editor'
......@@ -14,6 +14,7 @@ class App extends Component<{}> {
rawLevelData: null,
levelData: null,
screen: 'game',
isWin: false,
}
componentDidMount() {
......@@ -23,7 +24,7 @@ class App extends Component<{}> {
async loadLevelData(data = this.state.rawLevelData) {
const levelData = await LevelData.from(data)
await levelData.tick()
this.setState({ levelData, rawLevelData: data })
this.setState({ isWin: false, levelData, rawLevelData: data })
}
render() {
......@@ -51,10 +52,12 @@ class App extends Component<{}> {
(() => {
switch (this.state.screen) {
case 'game':
if (levelData == null) {
if (this.state.isWin) {
return <h2>A winner is you</h2>
} else if (levelData == null) {
return <span>LOADING</span>
} else {
return <Level level={levelData} {...opts} />
return <Level level={levelData} {...opts} onWin={this._win}/>
}
case 'editor': {
return this.state.rawLevelData && (
......@@ -76,6 +79,10 @@ class App extends Component<{}> {
);
}
_win = () => {
this.setState({ isWin: true })
}
set = (name, value = null) => e => {
const targetValue = e.currentTarget.value
this.setState(s => ({ [name]: value && value.call ? value(s) : value || targetValue }))
......
{
"name": "Ba Ba Test Sheep",
"width": 10,
"height": 10,
"connectors": [
{
"type": "is",
"x": 2,
"y": 5
},
{
"type": "is",
"x": 2,
"y": 2
},
{
"type": "is",
"x": 7,
"y": 7
}
],
"nouns": [
{
"name": "charles",
"images": {
"text": "/charles-text.png",
"entity": "/charles.png"
},
"text_start_locations": [
{
"x": 1,
"y": 5
}
],
"entity_start_locations": [
{
"x": 5,
"y": 5
}
]
}, {
"name": "flag",
"images": {
"text": "/flag-text.png",
"entity": "/flag.png"
},
"text_start_locations": [
{
"x": 7,
"y": 6
}
],
"entity_start_locations": [
{
"x": 8,
"y": 3
}
]
},
{
"name": "wall",
"images": {
"text": "/wall-text.png",
"entity": "/wall.png"
},
"text_start_locations": [
{
"x": 1,
"y": 2
}
],
"entity_start_locations": [
{
"x": 0,
"y": 0
},
{
"x": 1,
"y": 0
},
{
"x": 2,
"y": 0
},
{
"x": 3,
"y": 0
},
{
"x": 4,
"y": 0
},
{
"x": 5,
"y": 0
},
{
"x": 6,
"y": 0
},
{
"x": 7,
"y": 0
},
{
"x": 8,
"y": 0
},
{
"x": 9,
"y": 0
},
{
"x": 0,
"y": 2
},
{
"x": 0,
"y": 1
},
{
"x": 0,
"y": 3
},
{
"x": 0,
"y": 4
},
{
"x": 0,
"y": 5
},
{
"x": 0,
"y": 6
},
{
"x": 0,
"y": 7
},
{
"x": 0,
"y": 8
},
{
"x": 0,
"y": 9
},
{
"x": 1,
"y": 9
},
{
"x": 2,
"y": 9
},
{
"x": 3,
"y": 9
},
{
"x": 4,
"y": 9
},
{
"x": 6,
"y": 9
},
{
"x": 7,
"y": 9
},
{
"x": 8,
"y": 9
},
{
"x": 9,
"y": 9
},
{
"x": 5,
"y": 9
},
{
"x": 9,
"y": 3
},
{
"x": 9,
"y": 1
},
{
"x": 9,
"y": 2
},
{
"x": 9,
"y": 4
},
{
"x": 9,
"y": 5
},
{
"x": 9,
"y": 6
},
{
"x": 9,
"y": 7
},
{
"x": 9,
"y": 8
},
{
"x": 6,
"y": 1
},
{
"x": 6,
"y": 2
},
{
"x": 6,
"y": 3
},
{
"x": 6,
"y": 5
},
{
"x": 6,
"y": 6
},
{
"x": 6,
"y": 4
},
{
"x": 6,
"y": 7
},
{
"x": 6,
"y": 8
}
]
}
],
"adjectives": [
{
"name": "you",
"source": {
"type": "internal",
"data": "adjectives/you"
},
"images": {
"text": "/you-text.png"
},
"text_start_locations": [
{
"x": 3,
"y": 5
}
]
},
{
"name": "push",
"source": {
"type": "internal",
"data": "adjectives/push"
},
"images": {
"text": "/push-text.png"
},
"text_start_locations": [
{
"x": 3,
"y": 7
}
]
},
{
"name": "stop",
"source": {
"type": "inline",
"data": {
"id": "stop",
"interactions": {
}
}
},
"images": {
"text": "/stop-text.png"
},
"text_start_locations": [
{
"x": 3,
"y": 2
}
]
},
{
"name": "win",
"source": {
"type": "inline",
"data": {
"id": "win",
"interactions": {}
}
},
"images": {
"text": "/win-text.png"
},
"text_start_locations": [
{
"x": 7,
"y": 8
}
]
}
]
}
\ No newline at end of file
// @flow
import typeof Noun from './Noun'
import Adjective from "./Adjective";
export default class Entity {
x: number
......@@ -33,4 +34,8 @@ export default class Entity {
return this.noun.interact(other.noun, this.position, other.position)
}
is(adjective: Adjective | string) {
return this.noun.adjectives.has(adjective) || Array.from(this.noun.adjectives).some(a => a.id === adjective)
}
}
\ No newline at end of file
......@@ -256,5 +256,9 @@ export default class Level {
noun.set(adjectives.filter(Boolean))
}
}
return this.findEntitiesThat('win').some(target => {
return this.findEntitiesAt(target.x, target.y, e => e !== target).some(other => other.is('you'))
})
}
}
\ No newline at end of file
......@@ -50,7 +50,7 @@ export default class Level extends React.Component<Props> {
handleKeyPress = async e => {
e.preventDefault()
if (this.state.active > 0) {
if (this.state.isWin || this.state.active > 0) {
return
}
const { key } = e
......@@ -79,14 +79,18 @@ export default class Level extends React.Component<Props> {
if (input) {
this.setState(s => ({ active: s.active + 1 }))
await this.props.level.processInput(input)
this.setState(s => ({ nonce: Math.random(), active: s.active - 1 }))
const isWin = await this.props.level.processInput(input)
if (isWin && !this.state.isWin) {
this.props.onWin()
}
this.setState(s => ({ nonce: Math.random(), active: s.active - 1, isWin }))
}
}
render() {
const {scale, size, margin, level} = this.props
const {width, height} = level
return (
<div className="level-container">
{[...range(width)].map(x =>
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment