Skip to content

Commit

Permalink
doc
Browse files Browse the repository at this point in the history
  • Loading branch information
Kuangcp committed Jul 7, 2024
1 parent c885701 commit d259e35
Show file tree
Hide file tree
Showing 7 changed files with 124 additions and 68 deletions.
Binary file added gui/src/doc/tank/main.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added gui/src/doc/tank/phone.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
11 changes: 9 additions & 2 deletions gui/src/main/java/com/github/kuangcp/tank/Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,18 @@
1. build: `mvn clean package -P tank`

-javaagent:/path/to/quasar-core-0.7.4-jdk8.jar
![](/gui/src/doc/tank/main.png)

![](/gui/src/doc/tank/phone.png)

## 2024-07-07 17:30:30

- 新增:网页Ws协议控制玩家坦克,模拟手柄交互

## TODO

1. 碰撞监测
1. 对方运动算法
1. 网络联机
1. 持久化存储
1. 持久化存储

32 changes: 15 additions & 17 deletions gui/src/main/java/com/github/kuangcp/tank/domain/EnemyTank.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ public class EnemyTank extends Tank implements Runnable, RobotRate {

public final long id;
public List<Bullet> bulletList = new CopyOnWriteArrayList<>();//子弹集合
private long lastShotMs = 0;
private long shotCDMs = 168;
private long lastShotTick = 0;
private final long shotCDMs = 200;

public int INIT_MAX_LIVE_BULLET = 4;
public int maxLiveBullet = INIT_MAX_LIVE_BULLET + 1; // 活跃子弹 最大数
Expand Down Expand Up @@ -139,40 +139,32 @@ public void addLife(int delta) {
}

/**
* 发射子弹 函数
* 发射子弹
*/
public void finalShotAction() {
//判断坦克方向来 初始化子弹的起始发射位置
final long nowMs = System.currentTimeMillis();
if (lastShotMs != 0 && nowMs - lastShotMs < shotCDMs) {
if (lastShotTick != 0 && nowMs - lastShotTick < shotCDMs) {
return;
}
if (this.bulletList.size() >= maxLiveBullet || !this.isAlive()) {
return;
}
switch (this.getDirect()) {
case DirectType.UP: {
Bullet s = new Bullet(this.getX() - 1, this.getY() - 15, 0);
bulletList.add(s);
LoopEventExecutor.addLoopEvent(s);
this.shot(this.getX() - 1, this.getY() - 15, DirectType.UP);
break;
}
case DirectType.DOWN: {
Bullet s = new Bullet(this.getX() - 2, this.getY() + 15, 1);
bulletList.add(s);
LoopEventExecutor.addLoopEvent(s);
this.shot(this.getX() - 2, this.getY() + 15, DirectType.DOWN);
break;
}
case DirectType.LEFT: {
Bullet s = new Bullet(this.getX() - 15 - 2, this.getY(), 2);
bulletList.add(s);
LoopEventExecutor.addLoopEvent(s);
this.shot(this.getX() - 15 - 2, this.getY(), DirectType.LEFT);
break;
}
case DirectType.RIGHT: {
Bullet s = new Bullet(this.getX() + 15 - 2, this.getY() - 1, 3);
bulletList.add(s);
LoopEventExecutor.addLoopEvent(s);
this.shot(this.getX() + 15 - 2, this.getY() - 1, DirectType.RIGHT);
break;
}
}
Expand All @@ -184,7 +176,13 @@ public void finalShotAction() {
// 协程池
// ExecutePool.shotScheduler.getExecutor().execute(s);

lastShotMs = nowMs;
lastShotTick = nowMs;
}

private void shot(int x, int y, int direction) {
Bullet s = new Bullet(x, y, direction);
bulletList.add(s);
LoopEventExecutor.addLoopEvent(s);
}

@Override
Expand Down
7 changes: 4 additions & 3 deletions gui/src/main/resources/tank/client/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,21 @@
<meta http-equiv="cache-control" content="no-cache, must-revalidate">
<meta http-equiv="expires" content="0">

<title>phaser example</title>
<title>Tank</title>
<script src="./libs/phaser.min.js"></script>
<script src="./libs/phaser.2.3.1.virtual-joystick.min.js"></script>
<style>
#phaser-example{
margin-top: -8px;
margin-left: -10px;
margin-left: -8px;
margin-bottom: -10px;
}
</style>
</head>

<body>
<div id="phaser-example"></div>
<div id="phaser-example">
</div>
<script type="text/javascript" src="./main.js"></script>
</body>

Expand Down
138 changes: 92 additions & 46 deletions gui/src/main/resources/tank/client/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,55 +7,97 @@
* For more details please see http://phaser.io/shop/plugins/virtualjoystick
*/

var game = new Phaser.Game(1190, 590, Phaser.AUTO, 'phaser-example');
let borderW, borderH;
let directX, directY;
let aX, aY;
let bX, bY;
let cX, cY;

// console.log(screen.height);
// alert(screen.height + ' ' + screen.width)
let phone = screen.width < 900;
if (phone) {
borderW = screen.width;
borderH = screen.height;

directX = 170;
directY = 250;
directScale = 0.7;

skillScale = 0.8;
aX = 600;
aY = 270;
bX = 690;
bY = 170;
cX = 760;
cY = 270;
} else {
borderW = 1190;
borderH = 590;

directX = 150;
directY = 430;
directScale = 0.85;

skillScale = 0.9;
aX = 660;
aY = 420;
bX = 800;
bY = 320;
cX = 920;
cY = 420;
}

var game = new Phaser.Game(borderW, borderH, Phaser.AUTO, 'phaser-example');

var wsUsable =false;
function connected() {
let ws = new WebSocket("ws://192.168.1.102:7094/ws?userId=120");
ws.onopen = function () {
console.log('connected');
};
ws.onmessage = function (evt) {
console.log(evt.data)
};

ws.onclose = function (evt) {
console.log("error");
};

ws.onerror = function (evt) {
console.log("error");
};

return ws;
try {
wsUsable = false;
let ws = new WebSocket("ws://192.168.1.102:7094/ws?userId=120");
ws.onopen = function () {
console.log('connected');
// 等 open 函数回调后才可以调用 send,否则会报错 no longer object
wsUsable = true;
};
ws.onmessage = function (evt) {
console.log(evt.data)
};
ws.onclose = function (evt) {
console.log("error");
};
ws.onerror = function (evt) {
console.log("error");
};

return ws;
} catch (e) {
console.log(e);
return null;
}
}
var ws = connected();

function toggleFullScreen() {
var docElm = document.documentElement;
if (docElm.requestFullscreen) {
docElm.requestFullscreen();
docElm.requestFullscreen();
} else if (docElm.msRequestFullscreen) {
docElm.msRequestFullscreen();
docElm.msRequestFullscreen();
} else if (docElm.mozRequestFullScreen) {
docElm.mozRequestFullScreen();
docElm.mozRequestFullScreen();
} else if (docElm.webkitRequestFullScreen) {
docElm.webkitRequestFullScreen();
docElm.webkitRequestFullScreen();
}
}

// toggleFullScreen()
}

var PhaserGame = function () {

this.background = null;
this.player = null;
this.speed = 300;

this.weapon1;
this.weapon2;
this.weapon3;

this.pad;
this.speed = 300;

this.stick;

Expand All @@ -74,7 +116,6 @@ PhaserGame.prototype = {
},

preload: function () {

this.load.atlas('arcade', 'assets/arcade-joystick.png', 'assets/arcade-joystick.json');
this.load.image('background', 'assets/back.png');
this.load.image('player', 'assets/dark.png');
Expand All @@ -97,30 +138,32 @@ PhaserGame.prototype = {

this.pad = this.game.plugins.add(Phaser.VirtualJoystick);

this.stick = this.pad.addStick(150, 430, 100, 'arcade');
this.stick = this.pad.addStick(directX, directY, 100, 'arcade');
this.stick.deadZone = 0;
this.stick.scale = 0.85;
this.stick.scale = directScale;

this.buttonA = this.pad.addButton(650, 420, 'arcade', 'button1-up', 'button1-down');
this.buttonA = this.pad.addButton(aX, aY, 'arcade', 'button1-up', 'button1-down');
this.buttonA.onDown.add(this.fireBullet, this);
this.buttonA.scale = 0.9;
this.buttonA.repeatRate = 100;
this.buttonA.scale = skillScale;
this.buttonA.repeatRate = 1000;
this.buttonA.addKey(Phaser.Keyboard.CONTROL);

this.buttonB = this.pad.addButton(790, 320, 'arcade', 'button2-up', 'button2-down');
this.buttonB = this.pad.addButton(bX, bY, 'arcade', 'button2-up', 'button2-down');
this.buttonB.onDown.add(this.fireRocket, this);
this.buttonB.scale = 0.9;
this.buttonB.repeatRate = 500;
this.buttonB.scale = skillScale;
this.buttonB.repeatRate = 300;
this.buttonB.addKey(Phaser.Keyboard.Z);

this.buttonC = this.pad.addButton(920, 420, 'arcade', 'button3-up', 'button3-down');
this.buttonC = this.pad.addButton(cX, cY, 'arcade', 'button3-up', 'button3-down');
this.buttonC.onDown.add(this.fireSpreadShot, this);
this.buttonC.scale = 0.9;
this.buttonC.scale = skillScale;
this.buttonB.repeatRate = 300;
this.buttonC.addKey(Phaser.Keyboard.SPACEBAR);
},

fireBullet: function () {
// toggleFullScreen()
toggleFullScreen();

// ws = connected();
// this.weapon1.fire(this.player);

Expand All @@ -135,10 +178,12 @@ PhaserGame.prototype = {
fireSpreadShot: function () {

// this.weapon3.fire(this.player);
let x = {
s: true
}
let x = {
s: true
}
if(wsUsable){
ws.send(JSON.stringify(x))
}
},

update: function () {
Expand Down Expand Up @@ -168,8 +213,9 @@ PhaserGame.prototype = {
else {
// this.player.body.velocity.set(0);
}

if (wsUsable) {
ws.send(JSON.stringify(key))
}
}

};
Expand Down
4 changes: 4 additions & 0 deletions netty/src/main/java/netty/Readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
https://www.baeldung.com/java-netty-http-server

https://medium.com/@irunika/how-to-write-a-http-websocket-server-using-netty-f3c136adcba9

0 comments on commit d259e35

Please sign in to comment.