This repository has been archived on 2024-07-22. You can view files and clone it, but cannot push or open issues or pull requests.
Doric/demo/src/Snake.ts

369 lines
11 KiB
TypeScript
Raw Normal View History

2019-10-28 11:28:46 +08:00
import { text, loge, log, ViewHolder, Stack, ViewModel, Gravity, Text, Color, HLayout, VLayout, Group, VMPanel, LayoutSpec, vlayout, hlayout, takeNonNull, stack } 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-10-25 14:13:35 +08:00
panel!: Stack
start?: 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([
2019-10-28 11:28:46 +08:00
text({
text: "Snake",
textSize: 20,
textColor: Color.parse("#ffffff"),
layoutConfig: {
alignment: new Gravity().centerX(),
margin: {
top: 20
},
widthSpec: LayoutSpec.WRAP_CONTENT,
heightSpec: LayoutSpec.WRAP_CONTENT,
},
}),
(new Stack).also(panel => {
panel.bgColor = Color.parse('#00ff00')
this.panel = panel
}),
hlayout([
text({
text: "Start",
textSize: 30,
textColor: Color.parse("#ffffff"),
layoutConfig: {
2019-10-25 03:15:23 +08:00
widthSpec: LayoutSpec.WRAP_CONTENT,
heightSpec: LayoutSpec.WRAP_CONTENT,
},
2019-10-28 11:28:46 +08:00
}).also(it => this.start = it),
]).also(it => {
it.layoutConfig = {
widthSpec: LayoutSpec.WRAP_CONTENT,
heightSpec: LayoutSpec.WRAP_CONTENT,
}
}),
vlayout([
hlayout([
text({
width: 50,
height: 50,
text: "↑",
textSize: 30,
textAlignment: new Gravity().center(),
bgColor: Color.parse('#ffff00'),
2019-11-16 18:47:03 +08:00
layoutConfig: {
widthSpec: LayoutSpec.EXACTLY,
heightSpec: LayoutSpec.EXACTLY,
},
2019-10-28 11:28:46 +08:00
}).also(it => this.up = it)
2019-10-25 03:15:23 +08:00
]).also(it => {
it.layoutConfig = {
widthSpec: LayoutSpec.WRAP_CONTENT,
heightSpec: LayoutSpec.WRAP_CONTENT,
}
2019-10-28 11:28:46 +08:00
}),
hlayout([
text({
width: 50,
height: 50,
text: "←",
textSize: 30,
textAlignment: new Gravity().center(),
bgColor: Color.parse('#ffff00'),
2019-11-16 18:47:03 +08:00
layoutConfig: {
widthSpec: LayoutSpec.EXACTLY,
heightSpec: LayoutSpec.EXACTLY,
},
2019-10-28 11:28:46 +08:00
}).also(it => this.left = it),
text({
width: 50,
height: 50,
text: "↓",
textSize: 30,
textAlignment: new Gravity().center(),
bgColor: Color.parse('#ffff00'),
2019-11-16 18:47:03 +08:00
layoutConfig: {
widthSpec: LayoutSpec.EXACTLY,
heightSpec: LayoutSpec.EXACTLY,
},
2019-10-28 11:28:46 +08:00
}).also(it => this.down = it),
text({
width: 50,
height: 50,
text: "→",
textSize: 30,
textAlignment: new Gravity().center(),
bgColor: Color.parse('#ffff00'),
2019-11-16 18:47:03 +08:00
layoutConfig: {
widthSpec: LayoutSpec.EXACTLY,
heightSpec: LayoutSpec.EXACTLY,
},
2019-10-28 11:28:46 +08:00
}).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,
}
}),
2019-10-25 03:15:23 +08:00
]).also(it => {
it.space = 20
it.layoutConfig = {
alignment: new Gravity().centerX().top(),
2019-10-25 14:13:35 +08:00
widthSpec: LayoutSpec.AT_MOST,
heightSpec: LayoutSpec.AT_MOST,
2019-10-25 03:15:23 +08:00
}
2019-10-25 14:13:35 +08:00
it.gravity = new Gravity().centerX()
}).in(root)
2019-07-25 14:34:54 +08:00
}
2019-10-25 03:15:23 +08:00
bind(state: SnakeModel): void {
2019-10-25 14:13:35 +08:00
log('build', state)
2019-10-25 03:15:23 +08:00
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
2019-11-20 11:18:44 +08:00
item.corners = 5
item.shadow = {
color: Color.GRAY,
opacity: 1,
radius: 3,
offsetX: 3,
offsetY: 3,
}
2019-10-25 03:15:23 +08:00
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-10-25 14:13:35 +08:00
takeNonNull(v.start)(it => it.onClick = this.start)
takeNonNull(v.left)(it => it.onClick = this.left)
takeNonNull(v.right)(it => it.onClick = this.right)
takeNonNull(v.up)(it => it.onClick = this.up)
takeNonNull(v.down)(it => it.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
}