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/js-framework/demo.ts

230 lines
4.9 KiB
TypeScript
Raw Normal View History

2019-07-24 19:58:30 +08:00
import { StackConfig, ViewHolder, VMPanel, View, ViewModel, WRAP_CONTENT, Gravity, Mutable, NativeCall, Text, Color, VLayout, Panel, log, logw, loge, Group, Stack, } from "./index"
2019-07-24 19:26:25 +08:00
2019-07-24 16:38:34 +08:00
interface CountModel {
count: number
}
2019-07-18 20:11:01 +08:00
2019-07-24 19:26:25 +08:00
class CounterView extends ViewHolder {
number = new Text
counter = new Text
build(root: Group) {
2019-07-24 16:38:34 +08:00
const vlayout = new VLayout
2019-07-24 19:26:25 +08:00
this.number.textSize = 40
this.number.layoutConfig = {
2019-07-24 16:38:34 +08:00
alignment: new Gravity().center()
2019-07-23 13:01:45 +08:00
}
2019-07-24 19:26:25 +08:00
this.counter = new Text
this.counter.text = "点击计数"
this.counter.textSize = 20
2019-07-24 11:43:38 +08:00
2019-07-24 16:38:34 +08:00
vlayout.space = 20
vlayout.layoutConfig = {
alignment: new Gravity().center()
2019-07-24 11:43:38 +08:00
}
2019-07-24 19:26:25 +08:00
vlayout.addChild(this.number)
vlayout.addChild(this.counter)
2019-07-24 17:24:00 +08:00
2019-07-24 16:38:34 +08:00
root.addChild(vlayout)
2019-07-24 19:26:25 +08:00
}
setNumber(n: number) {
this.number.text = n.toString()
}
2019-07-24 11:43:38 +08:00
2019-07-24 19:26:25 +08:00
setCounter(v: Function) {
this.counter.onClick = v
}
}
2019-07-24 10:14:17 +08:00
2019-07-24 19:26:25 +08:00
class CounterVM extends ViewModel<CountModel, CounterView> {
2019-07-24 20:40:25 +08:00
2019-07-24 19:26:25 +08:00
binding(v: CounterView, model: CountModel): void {
v.setNumber(model.count)
2019-07-24 20:40:25 +08:00
v.setCounter(() => {
this.getModel().count++
})
2019-07-24 16:38:34 +08:00
}
}
2019-07-24 19:26:25 +08:00
class MyPage extends VMPanel<CountModel, CounterView>{
2019-07-24 16:38:34 +08:00
2019-07-24 19:26:25 +08:00
getVMClass() {
return CounterVM
}
getModel() {
return {
2019-07-24 17:24:00 +08:00
count: 0,
add: function () {
this.count++
2019-07-24 19:26:25 +08:00
},
}
2019-07-18 16:29:24 +08:00
}
2019-07-22 14:36:32 +08:00
2019-07-24 19:26:25 +08:00
getViewHolder() {
return new CounterView
}
2019-07-22 13:41:38 +08:00
@NativeCall
2019-07-18 16:29:24 +08:00
log() {
2019-07-22 13:41:38 +08:00
log("Hello.HEGO")
logw("Hello.HEGO")
loge("Hello.HEGO")
2019-07-18 16:29:24 +08:00
}
2019-07-24 19:58:30 +08:00
}
2019-07-24 20:40:25 +08:00
type Postion = { x: number, y: number }
enum Direction {
left = 0,
right = 1,
up = 2,
down = 3,
}
interface SnakeNode {
prev?: SnakeNode
next?: SnakeNode
x: number
y: number
}
enum State {
idel,
run,
fail,
}
class SnakeModel {
state = State.idel
direction = Direction.left
width = 10
height = 10
food = { x: 0, y: 0 }
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:
node.x -= 10
break;
case Direction.right:
node.x += 10
break;
case Direction.up:
node.y -= 10
break;
case Direction.down:
node.y += 10
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
this.state = State.fail
} else if (this.head.x == this.food.x && this.head.y == this.food.y) {
//If eat food
let head = { x: this.food.x, y: this.food.y, next: this.head }
this.head.prev = head
this.forward(head)
this.head = head
this.refreshFood()
}
if (this.crashAtSelf()) {
//If crash at self
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
while (cur) {
if (cur.x == this.head.x && cur.y == this.head.y) {
return true
}
cur = cur.next
}
return false
}
2019-07-24 19:58:30 +08:00
}
class SnakeView extends ViewHolder {
build(root: Group): void {
root.bgColor = Color.parse('#000000')
const title = new Text
title.text = "Snake"
title.textSize = 20
title.textColor = Color.parse("#ffffff")
title.layoutConfig = {
alignment: new Gravity().centerX().top(),
margin: {
top: 20
}
} as StackConfig
root.addChild(title)
}
}
2019-07-24 20:40:25 +08:00
class SnakeVM extends ViewModel<SnakeModel, SnakeView>{
binding(v: SnakeView, model: SnakeModel) {
2019-07-24 19:58:30 +08:00
}
}
@Entry
2019-07-24 20:40:25 +08:00
class SnakePanel extends VMPanel<SnakeModel, SnakeView>{
2019-07-24 19:58:30 +08:00
getVMClass() {
return SnakeVM
}
getModel() {
2019-07-24 20:40:25 +08:00
return new SnakeModel
2019-07-24 19:58:30 +08:00
}
getViewHolder() {
return new SnakeView
}
2019-07-18 16:29:24 +08:00
}