2019-10-25 03:15:23 +08:00
|
|
|
import { loge, log, ViewHolder, Stack, ViewModel, Gravity, Text, Color, HLayout, VLayout, Group, VMPanel, LayoutSpec, vlayout, hlayout } from "doric";
|
2019-07-25 11:59:20 +08:00
|
|
|
|
|
|
|
type SnakeNode = {
|
|
|
|
x: number
|
|
|
|
y: number
|
|
|
|
prev?: SnakeNode
|
|
|
|
next?: SnakeNode
|
|
|
|
}
|
2019-07-24 19:58:30 +08:00
|
|
|
|
|
|
|
|
2019-07-25 11:59:20 +08:00
|
|
|
enum Direction {
|
2019-07-24 20:40:25 +08:00
|
|
|
left = 0,
|
|
|
|
right = 1,
|
2019-07-25 11:59:20 +08:00
|
|
|
up = 2,
|
|
|
|
down = 3,
|
2019-07-24 20:40:25 +08:00
|
|
|
}
|
2019-07-24 19:58:30 +08:00
|
|
|
|
2019-07-24 20:40:25 +08:00
|
|
|
enum State {
|
|
|
|
idel,
|
|
|
|
run,
|
|
|
|
fail,
|
|
|
|
}
|
|
|
|
|
|
|
|
class SnakeModel {
|
|
|
|
state = State.idel
|
2019-07-25 11:59:20 +08:00
|
|
|
direction = Direction.right
|
2019-07-24 20:40:25 +08:00
|
|
|
|
2019-07-25 00:14:55 +08:00
|
|
|
width: number
|
|
|
|
height: number
|
|
|
|
|
|
|
|
constructor(w: number, h: number) {
|
|
|
|
this.width = w
|
|
|
|
this.height = h
|
|
|
|
}
|
2019-07-25 11:59:20 +08:00
|
|
|
food = { x: -1, y: -1 }
|
2019-07-24 20:40:25 +08:00
|
|
|
|
|
|
|
head: SnakeNode = {
|
|
|
|
x: 0,
|
|
|
|
y: 0,
|
|
|
|
}
|
|
|
|
|
|
|
|
refreshFood() {
|
|
|
|
this.food.x = Math.floor(Math.random() * (this.width - 1))
|
|
|
|
this.food.y = Math.floor(Math.random() * (this.height - 1))
|
|
|
|
}
|
|
|
|
|
|
|
|
get tail() {
|
|
|
|
let node = this.head
|
|
|
|
while (node.next !== undefined) {
|
|
|
|
node = node.next
|
|
|
|
}
|
|
|
|
return node
|
|
|
|
}
|
|
|
|
get score() {
|
|
|
|
let node = this.head
|
|
|
|
let n = 0
|
|
|
|
while (node.next !== undefined) {
|
|
|
|
n++
|
|
|
|
node = node.next
|
|
|
|
}
|
|
|
|
return n
|
|
|
|
}
|
|
|
|
|
|
|
|
forward(node: SnakeNode) {
|
|
|
|
switch (this.direction) {
|
|
|
|
case Direction.left:
|
2019-07-25 11:59:20 +08:00
|
|
|
node.x -= 1
|
2019-07-24 20:40:25 +08:00
|
|
|
break;
|
|
|
|
case Direction.right:
|
2019-07-25 11:59:20 +08:00
|
|
|
node.x += 1
|
2019-07-24 20:40:25 +08:00
|
|
|
break;
|
|
|
|
case Direction.up:
|
2019-07-25 11:59:20 +08:00
|
|
|
node.y -= 1
|
2019-07-24 20:40:25 +08:00
|
|
|
break;
|
|
|
|
case Direction.down:
|
2019-07-25 11:59:20 +08:00
|
|
|
node.y += 1
|
2019-07-24 20:40:25 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2019-07-24 19:58:30 +08:00
|
|
|
|
|
|
|
|
2019-07-24 20:40:25 +08:00
|
|
|
step() {
|
|
|
|
if (this.state !== State.run) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
let tail = this.tail
|
|
|
|
while (tail.prev != undefined) {
|
|
|
|
tail.x = tail.prev.x
|
|
|
|
tail.y = tail.prev.y
|
|
|
|
tail = tail.prev
|
|
|
|
}
|
|
|
|
this.forward(this.head)
|
|
|
|
if (this.head.x < 0 || this.head.x >= this.width
|
|
|
|
|| this.head.y < 0 || this.head.y >= this.height) {
|
|
|
|
//If out of bound
|
2019-08-01 15:33:30 +08:00
|
|
|
loge('out of bound')
|
2019-07-24 20:40:25 +08:00
|
|
|
this.state = State.fail
|
|
|
|
} else if (this.head.x == this.food.x && this.head.y == this.food.y) {
|
|
|
|
//If eat food
|
2019-07-25 14:34:54 +08:00
|
|
|
|
|
|
|
let head: SnakeNode = { x: this.food.x, y: this.food.y }
|
|
|
|
log('eat food', head)
|
2019-07-24 20:40:25 +08:00
|
|
|
this.forward(head)
|
2019-07-25 14:34:54 +08:00
|
|
|
this.head.prev = head
|
|
|
|
head.next = this.head
|
2019-07-24 20:40:25 +08:00
|
|
|
this.head = head
|
|
|
|
this.refreshFood()
|
|
|
|
}
|
|
|
|
if (this.crashAtSelf()) {
|
|
|
|
//If crash at self
|
2019-07-25 14:34:54 +08:00
|
|
|
loge('crash at self')
|
2019-07-24 20:40:25 +08:00
|
|
|
this.state = State.fail
|
|
|
|
}
|
|
|
|
}
|
2019-07-24 19:58:30 +08:00
|
|
|
|
2019-07-24 20:40:25 +08:00
|
|
|
crashAtSelf() {
|
|
|
|
let cur = this.head.next
|
2019-07-25 14:34:54 +08:00
|
|
|
while (cur !== undefined) {
|
2019-07-24 20:40:25 +08:00
|
|
|
if (cur.x == this.head.x && cur.y == this.head.y) {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
cur = cur.next
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
2019-07-25 14:34:54 +08:00
|
|
|
|
|
|
|
reset() {
|
|
|
|
this.direction = Direction.right
|
|
|
|
this.state = State.run
|
|
|
|
this.head.x = 0
|
|
|
|
this.head.y = 0
|
|
|
|
this.head.next = undefined
|
|
|
|
this.refreshFood()
|
|
|
|
}
|
2019-07-24 19:58:30 +08:00
|
|
|
}
|
|
|
|
|
2019-10-25 03:15:23 +08:00
|
|
|
class SnakeView extends ViewHolder<SnakeModel> {
|
|
|
|
|
2019-07-25 00:14:55 +08:00
|
|
|
panel: Stack = new Stack
|
2019-07-25 11:59:20 +08:00
|
|
|
start: Text = new Text
|
2019-07-25 14:34:54 +08:00
|
|
|
up?: Text
|
|
|
|
down?: Text
|
|
|
|
left?: Text
|
|
|
|
right?: Text
|
|
|
|
|
2019-07-24 19:58:30 +08:00
|
|
|
build(root: Group): void {
|
|
|
|
root.bgColor = Color.parse('#000000')
|
2019-10-25 03:15:23 +08:00
|
|
|
vlayout([
|
|
|
|
() => {
|
|
|
|
return (new Text).also(title => {
|
|
|
|
title.text = "Snake"
|
|
|
|
title.textSize = 20
|
|
|
|
title.textColor = Color.parse("#ffffff")
|
|
|
|
title.layoutConfig = {
|
|
|
|
alignment: new Gravity().centerX(),
|
|
|
|
margin: {
|
|
|
|
top: 20
|
|
|
|
},
|
|
|
|
widthSpec: LayoutSpec.WRAP_CONTENT,
|
|
|
|
heightSpec: LayoutSpec.WRAP_CONTENT,
|
|
|
|
}
|
|
|
|
})
|
2019-07-25 11:59:20 +08:00
|
|
|
},
|
2019-10-25 03:15:23 +08:00
|
|
|
() => {
|
|
|
|
return (new Stack).also(panel => {
|
|
|
|
panel.bgColor = Color.parse('#00ff00')
|
|
|
|
|
|
|
|
})
|
|
|
|
},
|
|
|
|
() => {
|
|
|
|
return hlayout([
|
|
|
|
() => {
|
|
|
|
return (new Text).also(it => {
|
|
|
|
it.text = "Start"
|
|
|
|
it.textSize = 30
|
|
|
|
it.textColor = Color.parse("#ffffff")
|
|
|
|
it.layoutConfig = {
|
|
|
|
widthSpec: LayoutSpec.WRAP_CONTENT,
|
|
|
|
heightSpec: LayoutSpec.WRAP_CONTENT,
|
|
|
|
}
|
|
|
|
this.start = it
|
|
|
|
})
|
|
|
|
},
|
|
|
|
]).also(it => {
|
|
|
|
it.layoutConfig = {
|
|
|
|
widthSpec: LayoutSpec.WRAP_CONTENT,
|
|
|
|
heightSpec: LayoutSpec.WRAP_CONTENT,
|
|
|
|
}
|
|
|
|
})
|
|
|
|
},
|
|
|
|
() => {
|
|
|
|
return vlayout([
|
|
|
|
() => {
|
|
|
|
return hlayout([
|
|
|
|
() => {
|
|
|
|
return this.buildController("↑").also(it => {
|
|
|
|
this.up = it
|
|
|
|
})
|
|
|
|
}
|
|
|
|
]).also(it => {
|
|
|
|
it.layoutConfig = {
|
|
|
|
widthSpec: LayoutSpec.WRAP_CONTENT,
|
|
|
|
heightSpec: LayoutSpec.WRAP_CONTENT,
|
|
|
|
}
|
|
|
|
})
|
|
|
|
},
|
|
|
|
() => {
|
|
|
|
return hlayout([
|
|
|
|
() => {
|
|
|
|
return this.buildController("←").also(it => {
|
|
|
|
this.left = it
|
|
|
|
})
|
|
|
|
},
|
|
|
|
() => {
|
|
|
|
return this.buildController("↓").also(it => {
|
|
|
|
this.down = it
|
|
|
|
})
|
|
|
|
},
|
|
|
|
() => {
|
|
|
|
return this.buildController("→").also(it => {
|
|
|
|
this.right = it
|
|
|
|
})
|
|
|
|
},
|
|
|
|
]).also(it => {
|
|
|
|
it.layoutConfig = {
|
|
|
|
widthSpec: LayoutSpec.WRAP_CONTENT,
|
|
|
|
heightSpec: LayoutSpec.WRAP_CONTENT,
|
|
|
|
}
|
|
|
|
it.space = 10
|
|
|
|
})
|
|
|
|
},
|
|
|
|
])
|
|
|
|
.also(controlArea => {
|
|
|
|
controlArea.gravity = new Gravity().centerX()
|
|
|
|
controlArea.space = 10
|
|
|
|
controlArea.layoutConfig = {
|
|
|
|
alignment: new Gravity().centerX(),
|
|
|
|
widthSpec: LayoutSpec.WRAP_CONTENT,
|
|
|
|
heightSpec: LayoutSpec.WRAP_CONTENT,
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
]).also(it => {
|
|
|
|
it.space = 20
|
|
|
|
it.layoutConfig = {
|
|
|
|
alignment: new Gravity().centerX().top(),
|
|
|
|
widthSpec: LayoutSpec.WRAP_CONTENT,
|
|
|
|
heightSpec: LayoutSpec.WRAP_CONTENT,
|
|
|
|
}
|
|
|
|
})
|
2019-07-25 14:34:54 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
buildController(text: string) {
|
|
|
|
const ret = new Text
|
|
|
|
ret.width = ret.height = 50
|
|
|
|
ret.bgColor = Color.parse('#ffff00')
|
|
|
|
ret.text = text
|
|
|
|
ret.textSize = 30
|
|
|
|
ret.textAlignment = new Gravity().center()
|
|
|
|
return ret
|
2019-07-24 19:58:30 +08:00
|
|
|
}
|
2019-10-25 03:15:23 +08:00
|
|
|
|
|
|
|
bind(state: SnakeModel): void {
|
|
|
|
this.panel.width = state.width * 10
|
|
|
|
this.panel.height = state.height * 10
|
|
|
|
let node: SnakeNode | undefined = state.head
|
|
|
|
let nodes: SnakeNode[] = []
|
|
|
|
while (node != undefined) {
|
|
|
|
nodes.push(node)
|
|
|
|
node = node.next
|
|
|
|
}
|
|
|
|
nodes.push(state.food)
|
|
|
|
nodes.forEach((e, index) => {
|
|
|
|
|
|
|
|
let item = this.panel.children[index]
|
|
|
|
if (item === undefined) {
|
|
|
|
item = new Stack
|
|
|
|
item.width = item.height = 10
|
|
|
|
this.panel.addChild(item)
|
|
|
|
}
|
|
|
|
if (index === nodes.length - 1) {
|
|
|
|
item.bgColor = Color.parse('#ffff00')
|
|
|
|
} else {
|
|
|
|
item.bgColor = Color.parse('#ff0000')
|
|
|
|
}
|
|
|
|
item.x = e.x * 10
|
|
|
|
item.y = e.y * 10
|
|
|
|
})
|
|
|
|
|
|
|
|
if (nodes.length < this.panel.children.length) {
|
|
|
|
this.panel.children.length = nodes.length
|
|
|
|
}
|
|
|
|
}
|
2019-07-24 19:58:30 +08:00
|
|
|
}
|
|
|
|
|
2019-07-24 20:40:25 +08:00
|
|
|
class SnakeVM extends ViewModel<SnakeModel, SnakeView>{
|
2019-07-25 00:14:55 +08:00
|
|
|
timerId?: any
|
2019-07-24 19:58:30 +08:00
|
|
|
|
2019-07-25 11:59:20 +08:00
|
|
|
start = () => {
|
2019-07-25 00:14:55 +08:00
|
|
|
if (this.timerId !== undefined) {
|
|
|
|
clearInterval(this.timerId)
|
|
|
|
}
|
2019-10-25 03:15:23 +08:00
|
|
|
this.updateState(it => it.reset())
|
2019-07-25 00:14:55 +08:00
|
|
|
this.timerId = setInterval(() => {
|
2019-10-25 03:15:23 +08:00
|
|
|
this.updateState(it => it.step())
|
|
|
|
if (this.getState().state === State.fail) {
|
|
|
|
loge('Game Over')
|
|
|
|
this.stop()
|
|
|
|
}
|
2019-07-25 14:34:54 +08:00
|
|
|
}, 500)
|
2019-07-25 00:14:55 +08:00
|
|
|
}
|
|
|
|
|
2019-07-25 11:59:20 +08:00
|
|
|
stop = () => {
|
2019-07-25 00:14:55 +08:00
|
|
|
if (this.timerId !== undefined) {
|
|
|
|
clearInterval(this.timerId)
|
|
|
|
this.timerId = undefined
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-25 14:34:54 +08:00
|
|
|
left = () => {
|
2019-10-25 03:15:23 +08:00
|
|
|
this.updateState(it => it.direction = Direction.left)
|
2019-07-25 14:34:54 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
right = () => {
|
2019-10-25 03:15:23 +08:00
|
|
|
this.updateState(it => it.direction = Direction.right)
|
2019-07-25 14:34:54 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
up = () => {
|
2019-10-25 03:15:23 +08:00
|
|
|
this.updateState(it => it.direction = Direction.up)
|
2019-07-25 14:34:54 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
down = () => {
|
2019-10-25 03:15:23 +08:00
|
|
|
this.updateState(it => it.direction = Direction.down)
|
2019-07-25 14:34:54 +08:00
|
|
|
}
|
|
|
|
|
2019-10-25 03:15:23 +08:00
|
|
|
onAttached(state: SnakeModel, v: SnakeView): void {
|
2019-07-25 11:59:20 +08:00
|
|
|
v.start.onClick = this.start
|
2019-07-25 14:34:54 +08:00
|
|
|
if (v.left) {
|
|
|
|
v.left.onClick = this.left
|
|
|
|
}
|
|
|
|
if (v.right) {
|
|
|
|
v.right.onClick = this.right
|
|
|
|
}
|
|
|
|
if (v.up) {
|
|
|
|
v.up.onClick = this.up
|
|
|
|
}
|
|
|
|
if (v.down) {
|
|
|
|
v.down.onClick = this.down
|
|
|
|
}
|
2019-07-24 19:58:30 +08:00
|
|
|
}
|
2019-10-25 03:15:23 +08:00
|
|
|
|
2019-07-24 19:58:30 +08:00
|
|
|
}
|
2019-08-13 11:02:46 +08:00
|
|
|
@Entry
|
2019-07-24 20:40:25 +08:00
|
|
|
class SnakePanel extends VMPanel<SnakeModel, SnakeView>{
|
2019-10-25 03:15:23 +08:00
|
|
|
getViewModelClass() {
|
2019-07-24 19:58:30 +08:00
|
|
|
return SnakeVM
|
|
|
|
}
|
2019-10-25 03:15:23 +08:00
|
|
|
getState(): SnakeModel {
|
2019-07-25 11:59:20 +08:00
|
|
|
return new SnakeModel(35, 35)
|
2019-07-24 19:58:30 +08:00
|
|
|
}
|
2019-10-25 03:15:23 +08:00
|
|
|
getViewHolderClass() {
|
|
|
|
return SnakeView
|
2019-07-24 19:58:30 +08:00
|
|
|
}
|
2019-07-18 16:29:24 +08:00
|
|
|
}
|