-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpiggy_main.ts
61 lines (50 loc) · 1.82 KB
/
piggy_main.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
import { Callable, Node, Node2D, NodePath, PackedScene, ResourceLoader, ShaderMaterial, Sprite2D, Vector2 } from "godot";
import { onready } from "godot.annotations";
import Arrow from "./arrow";
import Shooter from "./shooter";
export default class PiggyMain extends Node {
@onready("scene")
scene!: Node2D;
@onready("scene/shooter")
shooter!: Shooter;
@onready("scene/rope")
rope!: Sprite2D;
private instantiate_asset(path: string) {
console.log("loading asset", path);
const scene = <PackedScene>ResourceLoader.load(path);
if (scene) {
const node = scene.instantiate();
this.scene.add_child(node);
console.log("instantiate asset", path);
return node;
}
console.error("no such scene", path);
}
_ready() {
console.log("scene:", this.scene);
console.log("shooter:", this.shooter);
console.log("rope:", this.rope);
this.shooter.shot.connect(Callable.create(this, this.on_shooter_shot));
this.shooter.moved.connect(Callable.create(this, this.on_shooter_moved));
}
private on_shooter_moved(y: number) {
let rope_y = this.rope.position.y;
let scale = new Vector2(1, (y - rope_y) / 64);
this.rope.scale = scale;
let mat = <ShaderMaterial>this.rope.material;
try {
mat.set_shader_parameter("tiling", scale.y);
} catch (e) {
console.error(e);
}
}
private on_shooter_shot(pos: Vector2) {
console.log("shot at", pos);
let arrow = <Arrow>this.instantiate_asset("res://piggy/arrow.tscn");
arrow.init_position(pos);
arrow.die.connect(Callable.create(this, this.on_arrow_die));
}
private on_arrow_die() {
console.log("arrow fly out of screen");
}
}