Skip to content

Instantly share code, notes, and snippets.

View philipshen's full-sized avatar

Philip Shen philipshen

View GitHub Profile
@ccclass
export default class NewClass extends cc.Component {
@property (cc.Node) // 1
barrel: cc.Node = null
@property (cc.Node)
body: cc.Node = null
@property angle: number = 0
onLoad() { // 1
this.hookInput()
}
hookInput() {
const cannon = this;
cc.eventManager.addListener({ // 2
event: cc.EventListener.KEYBOARD, // 3
onKeyPressed(kCode, e) { // 4
update(dt) {
const { angleSpeed } = this;
if (angleSpeed !== 0) {
const angle = this.angle = MathUtilities.clamp(this.angle + angleSpeed * dt,
this.leftMaxAngle * -1,
this.rightMaxAngle);
this.barrel.rotation = angle;
}
}
export const clamp = (val, min, max) => val < min ? min : val > max ? max : val
@ccclass
export default class NewClass extends cc.Component {
@property (cc.RigidBody)
floor: cc.RigidBody = null // 1
@property gravity: number = 0;
// LIFE-CYCLE CALLBACKS:
onLoad () {
const {ccclass, property} = cc._decorator;
@ccclass
export default class NewClass extends cc.Component {
// In milliseconds
timeToLive = 5000
timeAlive = 0
@property(cc.Prefab)
bullet: cc.Prefab = null
const { cos, sin, PI } = Math
const rad = deg => deg * PI / 180;
export const cosd = deg => cos(rad(deg));
export const sind = deg => sin(rad(deg));
// FACTORY
createBullet(position: cc.Vec2, velocity: number, angle: number) {
const newBullet = cc.instantiate(this.bullet) // 1
newBullet.setPosition(position) // 2
newBullet.rotation = angle
const body = newBullet.getComponent(cc.RigidBody) // 3
body.linearVelocity = cc.v2(MathUtilities.sind(angle) * velocity,
MathUtilities.cosd(angle) * velocity)
// Cannon.ts
import Game from './Game'
@ccclass
export default class Cannon extends cc.Component {
game: Game
onLoad() {