文章目录
前言敌机脚本1. 初始变量2. 子弹发射逻辑3. 初始化敌机状态和子弹状态
子弹脚本1.创建初始变量与飞机子弹类型判断逻辑2. 状态初始化
游戏管理脚本完整代码GameManager.tsBullet.tsEnemyPlane.ts
前言
敌机子弹需要考虑
敌机子弹与玩家子弹方向不同敌机是有可能不产生子弹的 产生子弹要有发射周期
敌机脚本
EnemyPlane.ts
1. 初始变量
首先子弹发射要有子弹周期createBulletTime,然后要有判断当前子弹发射的状态_needBullet(因为有两种飞机,发射子弹和不发射子弹的),还有创建子弹的时间_currCreateBulletTime和_gameManager(创建子弹会写在游戏管理里) ![在这里插入图片描述](https://img-blog.csdnimg.cn/c3660d661cb9428b8057f05bd5b03b7c.png?x-oss-process=image/watermark,type_d3F5LXplbmhlaQ,shadow_50,text_Q1NETiBAeWtsOTcwNzE5,size_20,color_FFFFFF,t_70,g_se,x_16)
/** 子弹发射周期 */
@property
public createBulletTime = 0.5
// 敌机速度
private _enemySpeed = 0;
private _needBullet = false//当前是否发射子弹
private _gameManager: GameManager = null
private _currCreateBulletTime = 0
2. 子弹发射逻辑
需要判断敌机是否需要发射子弹,如果需要发射根据子弹发射周期创建子弹 ![在这里插入图片描述](https://img-blog.csdnimg.cn/dc06399dbaca4c0ca322602a70a3e4dd.png?x-oss-process=image/watermark,type_d3F5LXplbmhlaQ,shadow_50,text_Q1NETiBAeWtsOTcwNzE5,size_20,color_FFFFFF,t_70,g_se,x_16)
if (this._needBullet) {//发射子弹逻辑
this._currCreateBulletTime += deltaTime
if (this._currCreateBulletTime > this.createBulletTime) {
this._gameManager.createEnemyBullet(this.node.position)
this._currCreateBulletTime = 0
}
}
3. 初始化敌机状态和子弹状态
![在这里插入图片描述](https://img-blog.csdnimg.cn/f4c8f64d87504621a421ea4ff248f5ee.png?x-oss-process=image/watermark,type_d3F5LXplbmhlaQ,shadow_50,text_Q1NETiBAeWtsOTcwNzE5,size_20,color_FFFFFF,t_70,g_se,x_16)
/**
* 设置飞机移动速度
* @param speed 移动速度
*/
show(gameManager: GameManager, speed: number, needBullet: boolean) {
this._gameManager = gameManager
this._enemySpeed = speed
this._needBullet = needBullet
}
子弹脚本
Bullet.ts 最开始只有玩家子弹,但是现在也有敌机子弹了,所以需要有判断子弹是敌机还是玩家的变量,然后边界也要分玩家子弹边界与敌机子弹边界,并根据玩家与敌机子弹进行处理
1.创建初始变量与飞机子弹类型判断逻辑
![在这里插入图片描述](https://img-blog.csdnimg.cn/b71a3b5de6eb4bb68ad849595be9f77f.png?x-oss-process=image/watermark,type_d3F5LXplbmhlaQ,shadow_50,text_Q1NETiBAeWtsOTcwNzE5,size_20,color_FFFFFF,t_70,g_se,x_16)
private _isEnemyBullet = false;//判断是否是敌机子弹
update(deltaTime: number) {
// 子弹移动
const pos = this.node.position;
let moveLength = 0
// 敌机子弹
if (this._isEnemyBullet) {
moveLength = pos.z + this._bulletSpeed;
this.node.setPosition(pos.x, pos.y, moveLength);
// 超出屏幕销毁
if (moveLength > 50) {//敌机子弹移动的最大位置范围为50
this.node.destroy();
}
} else {//玩家子弹
moveLength = pos.z - this._bulletSpeed;
this.node.setPosition(pos.x, pos.y, moveLength);
// 超出屏幕销毁
if (moveLength
this._bulletSpeed = speed
this._isEnemyBullet = isEnemyBullet
}
游戏管理脚本
GameManager.ts 这里主要就是写敌机子弹的创建以及通知敌机是否生成带子弹的飞机
/**
* 创建敌机子弹
* @param targetPos 敌机子弹位置
*/
public createEnemyBullet(targetPos: Vec3) {
// 子弹实例化
const bullet = instantiate(this.bullet01);
// 将子弹放在子弹管理节点下面
bullet.setParent(this.bulletRoot);
// 设置子弹位置
bullet.setPosition(targetPos.x, targetPos.y, targetPos.z + 6);
// 设置子弹速度
const bulletComp = bullet.getComponent(Bullet);
bulletComp.show(1, true)
}
/**
* 创建敌机
*
*/
public createEnemyPlane() {
// 两架飞机随机选一(1,2)
const whichEnemy = math.randomRangeInt(1, 3)
let prefab: Prefab = null
let speed = 0
// 创建敌机1或2
if (whichEnemy == Constant.EnemyType.TYPE1) {
prefab = this.enemy01
speed = this.enemy1Speed
} else {
prefab = this.enemy02
speed = this.enemy2Speed
}
// 预制实例化
const enemy = instantiate(prefab)
console.log(enemy);
enemy.setParent(this.node)
const enemyComp = enemy.getComponent(EnemyPlane)
enemyComp.show(this, speed, true)//单架敌机需要发射子弹
// 设置飞机位置
const randomPos = math.randomRangeInt(-25, 26)
enemy.setPosition(randomPos, 0, -50)
}
完整代码
GameManager.ts
import { _decorator, Component, Node, Prefab, instantiate, math, Vec3 } from 'cc';
import { Bullet } from '../bullet/Bullet';
import { EnemyPlane } from '../plane/EnemyPlane';
import { Constant } from './Constant';
const { ccclass, property } = _decorator;
@ccclass('GameManager')
export class GameManager extends Component {
// 关联玩家飞机
@property(Node)
public playerPlane: Node = null;
// 关联所有子弹
@property(Prefab)
public bullet01: Prefab = null;
@property(Prefab)
public bullet02: Prefab = null;
@property(Prefab)
public bullet03: Prefab = null;
@property(Prefab)
public bullet04: Prefab = null;
@property(Prefab)
public bullet05: Prefab = null;
// 射击周期
@property
public shootTime = 0.3;
// 子弹移动速度
@property
public bulletSpeed = 1;
/** 关联敌机 */
@property(Prefab)
public enemy01: Prefab = null;
@property(Prefab)
public enemy02: Prefab = null;
@property
public createEnemtTime = 1;//创建敌机时间
@property
public enemy1Speed = 0.5;//敌机1速度
@property
public enemy2Speed = 0.7;//敌机2速度
//子弹管理节点
@property(Node)
public bulletRoot: Node = null;
private _currShootTime = 0;
// 是否触摸屏幕
private _isShooting = false;
private _currCreateEnemyTime = 0//当前创建的敌机时间
private _combinationInterval = Constant.Combination.PLAN1//组合间隔状态
start() {
this._init();
}
update(deltaTime: number) {
// 这步加时间是为了发射子弹
this._currShootTime += deltaTime;
// 判断是触摸状态 并且射击时间大于我们的周期 发射子弹
if (this._isShooting && this._currShootTime > this.shootTime) {
this.createPlayerBullet();
this._currShootTime = 0;
}
this._currCreateEnemyTime += deltaTime
//判断组合方式
if (this._combinationInterval == Constant.Combination.PLAN1) {
//只创建单一的飞机 0-10秒飞机创建
if (this._currCreateEnemyTime > this.createEnemtTime) {//普通飞机创建
this.createEnemyPlane()
this._currCreateEnemyTime = 0
}
} else if (this._combinationInterval == Constant.Combination.PLAN2) {
// 10-20秒飞机创建
if (this._currCreateEnemyTime > this.createEnemtTime * 0.9) {//0.9飞机组合间隔
const randomCombination = math.randomRangeInt(1, 3)//随机1,2飞机
if (randomCombination === Constant.Combination.PLAN2) {
this.createCombination1()
} else {
this.createEnemyPlane()
}
this._currCreateEnemyTime = 0
}
} else {
//20+ 飞机创建
if (this._currCreateEnemyTime > this.createEnemtTime * 0.8) {//0.8飞机组合间隔
const randomCombination = math.randomRangeInt(1, 4)//随机1,2,3飞机
if (randomCombination === Constant.Combination.PLAN2) {
this.createCombination1()
} else if (randomCombination === Constant.Combination.PLAN3) {
this.createCombination2()
} else {
this.createEnemyPlane()
}
this._currCreateEnemyTime = 0
}
}
}
/**
* 创建子弹
*/
public createPlayerBullet() {
// 子弹实例化
const bullet = instantiate(this.bullet01);
// 将子弹放在子弹管理节点下面
bullet.setParent(this.bulletRoot);
// 获取飞机位置
const pos = this.playerPlane.position;
// 设置子弹位置
bullet.setPosition(pos.x, pos.y, pos.z - 7);
// 设置子弹速度
const bulletComp = bullet.getComponent(Bullet);
bulletComp.show(this.bulletSpeed, false)
}
/**
* 创建敌机子弹
* @param targetPos 敌机子弹位置
*/
public createEnemyBullet(targetPos: Vec3) {
// 子弹实例化
const bullet = instantiate(this.bullet01);
// 将子弹放在子弹管理节点下面
bullet.setParent(this.bulletRoot);
// 设置子弹位置
bullet.setPosition(targetPos.x, targetPos.y, targetPos.z + 6);
// 设置子弹速度
const bulletComp = bullet.getComponent(Bullet);
bulletComp.show(1, true)
}
/**
* 创建敌机
*
*/
public createEnemyPlane() {
// 两架飞机随机选一(1,2)
const whichEnemy = math.randomRangeInt(1, 3)
let prefab: Prefab = null
let speed = 0
// 创建敌机1或2
if (whichEnemy == Constant.EnemyType.TYPE1) {
prefab = this.enemy01
speed = this.enemy1Speed
} else {
prefab = this.enemy02
speed = this.enemy2Speed
}
// 预制实例化
const enemy = instantiate(prefab)
console.log(enemy);
enemy.setParent(this.node)
const enemyComp = enemy.getComponent(EnemyPlane)
enemyComp.show(this, speed, true)//单架敌机需要发射子弹
// 设置飞机位置
const randomPos = math.randomRangeInt(-25, 26)
enemy.setPosition(randomPos, 0, -50)
}
/**
* 组合1创建 横向排列 z轴50,x轴从-20开始
*
*/
public createCombination1() {
const enemyArray = new Array(5)//飞机数组
for (let i = 0; i
const enemyArray = new Array(7)//飞机数组
// 位置数组
const combinationPos = [
-21, 0, -60,
-14, 0, -55,
-7, 0, -50,
0, 0, -45,
7, 0, -50,
14, 0, -55,
21, 0, -60
]
for (let i = 0; i
this._isShooting = value;
}
/**
* 默认发射子弹
*/
private _init() {
this._currShootTime = this.shootTime;
this._changePlanMode();
}
/**
* 设计定时器
*/
private _changePlanMode() {
/**
* 每10秒改变一次状态,执行三次,根据状态就能决定采用什么组合
*/
this.schedule(this._modeChanged, 10, 3);//组件自带定时器(回调函数,间隔时间,重复次数,延迟时间)
}
private _modeChanged() {
this._combinationInterval++
}
}
Bullet.ts
import { _decorator, Component, Node } from 'cc';
const { ccclass, property } = _decorator;
@ccclass('Bullet')
export class Bullet extends Component {
// 子弹速度
private _bulletSpeed = 0;
private _isEnemyBullet = false;//判断是否是敌机子弹
start() {
// [3]
}
update(deltaTime: number) {
// 子弹移动
const pos = this.node.position;
let moveLength = 0
// 敌机子弹
if (this._isEnemyBullet) {
moveLength = pos.z + this._bulletSpeed;
this.node.setPosition(pos.x, pos.y, moveLength);
// 超出屏幕销毁
if (moveLength > 50) {//敌机子弹移动的最大位置范围为50
this.node.destroy();
}
} else {//玩家子弹
moveLength = pos.z - this._bulletSpeed;
this.node.setPosition(pos.x, pos.y, moveLength);
// 超出屏幕销毁
if (moveLength
this._bulletSpeed = speed
this._isEnemyBullet = isEnemyBullet
}
}
EnemyPlane.ts
import { _decorator, Component, Node } from 'cc';
import { GameManager } from '../framework/GameManager';
const { ccclass, property } = _decorator;
// 敌机销毁位置
const OUTOFBOUNCE = 50
@ccclass('EnemyPlane')
export class EnemyPlane extends Component {
/** 子弹发射周期 */
@property
public createBulletTime = 0.5
// 敌机速度
private _enemySpeed = 0;
private _needBullet = false//当前是否发射子弹
private _gameManager: GameManager = null
private _currCreateBulletTime = 0
// // 敌机类型
// public enemyType = Constant.EnemyType.TYPE1
start() {
// [3]
}
update(deltaTime: number) {
const pos = this.node.position
const movePos = pos.z + this._enemySpeed
// 因为敌机向西飞所以为正向
this.node.setPosition(pos.x, pos.y, movePos)
if (this._needBullet) {//发射子弹逻辑
this._currCreateBulletTime += deltaTime
if (this._currCreateBulletTime > this.createBulletTime) {
this._gameManager.createEnemyBullet(this.node.position)
this._currCreateBulletTime = 0
}
}
if (movePos > OUTOFBOUNCE) {//超出边界销毁
this.node.destroy()
}
}
/**
* 设置飞机移动速度
* @param speed 移动速度
*/
show(gameManager: GameManager, speed: number, needBullet: boolean) {
this._gameManager = gameManager
this._enemySpeed = speed
this._needBullet = needBullet
}
}
|