package com.hit.sz.view;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.media.MediaPlayer;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import androidx.annotation.NonNull;
import com.hit.sz.R;
import com.hit.sz.activity.BoardActivity;
import com.hit.sz.aircraft.AbstractAircraft;
import com.hit.sz.aircraft.BossEnemy;
import com.hit.sz.aircraft.EliteEnemy;
import com.hit.sz.aircraft.HeroAircraft;
import com.hit.sz.aircraft.MobEnemy;
import com.hit.sz.application.ImageManager;
import com.hit.sz.activity.DifficultyActivity;
import com.hit.sz.basic.AbstractFlyingObject;
import com.hit.sz.bullet.BaseBullet;
import com.hit.sz.dao.Rank;
import com.hit.sz.factory.BossFactory;
import com.hit.sz.factory.EliteFactory;
import com.hit.sz.factory.EnemyFactory;
import com.hit.sz.factory.MobFactory;
import com.hit.sz.factory.PropFactory;
import com.hit.sz.factory.RandomPropFactory;
import com.hit.sz.internet.SocketConnector;
import com.hit.sz.prop.AbstractProp;
import com.hit.sz.prop.BombProp;
import com.hit.sz.strategy.ShootScatter;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
public abstract class AbstractGameView extends SurfaceView implements SurfaceHolder.Callback, Runnable {
private int backGroundTop = 0;
//屏幕宽高
public static int screenWidth;
public static int screenHeight;
public static String userName;
/**
* 时间间隔(ms),控制刷新频率
*/
private int timeInterval = 10;
/**
* 飞机,子弹,道具
*/
private final HeroAircraft heroAircraft;
private final List<AbstractAircraft> enemyAircrafts;
private final List<BaseBullet> heroBullets;
private final List<BaseBullet> enemyBullets;
private final List<AbstractProp> props;
private AbstractProp prop;
/**
* 工厂
*/
private final EnemyFactory mobFactory;
private final EnemyFactory eliteFactory;
private final EnemyFactory bossFactory;
private final PropFactory randomPropFactory;
/**
* enemyMaxNumber 普通机和精英机最大数量
* curBossNum 当前boss数量
* bossAppearCnt 记录boss出现的次数
* timeCnt 时间计数器,每一个周期+1,用于控制精英机出现概率
*/
private int enemyMaxNumber = 8;
private int curBossNum = 0;
private int bossAppearCnt = 0;
private int timeCnt = 0;
/**
* 游戏配置
*/
private boolean gameOverFlag = false;
public static int score = 0;
public int scoreThreshold = setBossScoreThreshold();
public int lastScoreThreshold = 0;
private int time = 0;
/**
* 周期(ms)
* 指示子弹的发射、敌机的产生频率
*/
private int cycleDuration = 100;
private int cycleTime = 0;
private final SurfaceHolder mSurfaceHolder;
private Paint mPaint;
private Canvas canvas;
private Context context;
//网络对战
private SocketConnector scoreSocketConnector;
public String matchMessage = "";
//网络排行榜
private SocketConnector onlineRank;
public static String rankMessage = "";
//音乐播放
private MediaPlayer bgmPlayer;
private MediaPlayer bgm_bossPlayer;
private MediaPlayer bomb_explosionPlayer;
private MediaPlayer bulletPlayer;
private MediaPlayer bullet_hitPlayer;
private MediaPlayer game_overPlayer;
private MediaPlayer get_supplyPlayer;
public AbstractGameView(Context context) {
super(context);
this.context = context;
loadImage(); // 加载图片资源
screenWidth = DifficultyActivity.screenWidth;
screenHeight = DifficultyActivity.screenHeight;
heroAircraft = HeroAircraft.getHeroInstance();
enemyAircrafts = new LinkedList<>();
heroBullets = new LinkedList<>();
enemyBullets = new LinkedList<>();
props = new LinkedList<>();
mobFactory = new MobFactory();
eliteFactory = new EliteFactory();
bossFactory = new BossFactory();
randomPropFactory = new RandomPropFactory();
//绘制相关
mSurfaceHolder = this.getHolder();
mSurfaceHolder.addCallback(this);
mPaint = new Paint();
setFocusable(true);
setKeepScreenOn(true);
setFocusableInTouchMode(true);
//链接分数服务器
scoreSocketConnector = new SocketConnector(9999);
scoreSocketConnector.connectSever();
onlineRank = new SocketConnector(9996);
onlineRank.connectSever();
//播放背景音乐
bgmPlayer = MediaPlayer.create(getContext(), R.raw.bgm);
bgmPlayer.setLooping(true);
bgm_bossPlayer = MediaPlayer.create(getContext(), R.raw.bgm_boss);
bgm_bossPlayer.setLooping(true);
bomb_explosionPlayer = MediaPlayer.create(getContext(), R.raw.bomb_explosion);
bomb_explosionPlayer.setLooping(false);
bulletPlayer = MediaPlayer.create(getContext(), R.raw.bullet);
bulletPlayer.setLooping(false);
bullet_hitPlayer = MediaPlayer.create(getContext(), R.raw.bullet_hit);
bullet_hitPlayer.setLooping(false);
game_overPlayer = MediaPlayer.create(getContext(), R.raw.game_over);
game_overPlayer.setLooping(false);
get_supplyPlayer = MediaPlayer.create(getContext(), R.raw.get_supply);
get_supplyPlayer.setLooping(false);
if (DifficultyActivity.IsMusicOn) {
bgmPlayer.start();
}
}
/**
* 游戏启动入口,执行游戏逻辑
*/
public final void action() {
time += timeInterval;
// 周期性执行(控制频率)
if (timeCountAndNewCycleJudge()) {
System.out.println(time);
timeCnt++;
if (enemyAircrafts.size() < enemyMaxNumber) {
AbstractAircraft mobEnemy = mobFactory.getEnemy();
mobEnemy.setHp(setInitEnemyHp());
enemyAircrafts.add(mobEnemy);
}
if (ifSpawnElite()) {
AbstractAircraft eliteEnemy = eliteFactory.getEnemy();
eliteEnemy.setHp(setInitEnemyHp());
enemyAircrafts.add(eliteEnemy);
}
if (score > scoreThreshold && isBoss()) {
if (curBossNum == 0) {
AbstractAircraft bossEnemy = bossFactory.getEnemy();
bossEnemy.setShootStrategy(new ShootScatter());
if (isBossHPIncreasing()) {
bossEnemy.setHp(bossEnemy.getHp() + bossAppearCnt * 200);
}
enemyAircrafts.add(bossEnemy);
curBossNum = 1;
bossAppearCnt++;
if (DifficultyActivity.IsMusicOn) {
// TODO
bgm_bossPlayer.start();
}
}
lastScoreThreshold = scoreThreshold;
if (bossAppearCnt <= 5) {
scoreThreshold += (1000 - bossAppearCnt * 50);
} else {
scoreThreshold = 500;
}
}
// 飞机射出子弹
shootAction();
}
// 子弹移动
bulletsMoveAction();
// 飞机移动
aircraftsMoveAction();
// 道具移动
propsMoveAction();
// 撞击检测
crashCheckAction();
// 后处理
postProcessAction();
//每个时刻重绘界面
repaint();
//网络对战得分
scoreSocketConnector.SendMessage(userName + "," + score);
if (scoreSocketConnector.messageFromServer != null) {
Nello._BTW
- 粉丝: 5
- 资源: 5
最新资源
- AllSort(直接插入排序,希尔排序,选择排序,堆排序,冒泡排序,快速排序,归并排序)
- 模拟qsort,改造冒泡排序使其能排序任意数据类型,即日常练习
- 数组经典习题之顺序排序和二分查找和冒泡排序
- 基于 Oops Framework 提供的游戏项目开发模板,项目中提供了最新版本 Cocos Creator 3.x 插件与游戏资源初始化通用逻辑
- live-ai这是一个深度学习的资料
- FeiQ.rar 局域网内通信服务软件
- 172.16.100.195
- 光储并网simulink仿真模型,直流微电网 光伏系统采用扰动观察法是实现mppt控制,储能可由单独蓄电池构成,也可由蓄电池和超级电容构成的混合储能系统,并采用lpf进行功率分配 并网采用pq控制
- python编写微信读取smart200plc的数据发送给微信联系人
- 光储并网VSG系统Matlab simulink仿真模型,附参考文献 系统前级直流部分包括光伏阵列、变器、储能系统和双向dcdc变器,后级交流子系统包括逆变器LC滤波器,交流负载 光储并网VSG系
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈