This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
onLoad() { // 1 | |
this.hookInput() | |
} | |
hookInput() { | |
const cannon = this; | |
cc.eventManager.addListener({ // 2 | |
event: cc.EventListener.KEYBOARD, // 3 | |
onKeyPressed(kCode, e) { // 4 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export const clamp = (val, min, max) => val < min ? min : val > max ? max : val |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@ccclass | |
export default class NewClass extends cc.Component { | |
@property (cc.RigidBody) | |
floor: cc.RigidBody = null // 1 | |
@property gravity: number = 0; | |
// LIFE-CYCLE CALLBACKS: | |
onLoad () { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const {ccclass, property} = cc._decorator; | |
@ccclass | |
export default class NewClass extends cc.Component { | |
// In milliseconds | |
timeToLive = 5000 | |
timeAlive = 0 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@property(cc.Prefab) | |
bullet: cc.Prefab = null |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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) | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Cannon.ts | |
import Game from './Game' | |
@ccclass | |
export default class Cannon extends cc.Component { | |
game: Game | |
onLoad() { |
OlderNewer