'use strict';
const express = require('express');
const http = require('http');
const path = require('path');
const socketIO = require('socket.io');
const app = express();
const server = http.Server(app);
const io = socketIO(server);
const yargs = require('yargs').argv;
const PORT = process.env.PORT || 7000;
const FIELD_SC = 32;
const FIELD_WIDTH = 1000*FIELD_SC, FIELD_HEIGHT = 1000*FIELD_SC;
const START_TALL = 1000 * 4;
var mFieldGridSize = 250;
var mBulletSize = 6;
var mBulletSpeed = mFieldGridSize * 1.0;
var mBulletLength = mBulletSpeed*1.0;
var mPlayerSpeed = 10;
var mPlayerTurnSpeed = 0.05;
var mPlayerTall = 100;
var mWallSize = mFieldGridSize;
var mWallThick = 25 * 1.0;
var mShootCycle = [6,30,60]; //AR,SG,S
var mMagagineSize = [30,8,1];
var mJumpHight = mWallSize *1.2;
var mJumpFrame = 40;
var mBandageSize = 20;
var mFallSpeed = 15;
function mFloorGS(v_){ //floor to grid size
return Math.floor(v_/mFieldGridSize) * mFieldGridSize;
}
class GameObject{
constructor(obj={}){
this.id = Math.floor(Math.random()*1000000000);
this.x = obj.x;
this.y = obj.y;
this.z = obj.z; //
this.width = obj.width;
this.height = obj.height;
this.tall = obj.tall;
this.angle = obj.angle;
this.angle2 = obj.angle2;
this.isMoving = 0;
this.isSquat = 0;
}
move3(distance){ //for bullet
const oldX = this.x, oldY = this.y, oldZ = this.z;
this.isMoving = 1;
var d1_ = distance * Math.sin(-this.angle2);
var d2_ = distance * Math.cos(this.angle2);
this.z += d1_;
this.x += d2_ * Math.cos(this.angle);
this.y += d2_ * Math.sin(this.angle);
let collision = false;
if(this.x < 0 || this.x + this.width >= FIELD_WIDTH || this.y < 0 || this.y + this.height >= FIELD_HEIGHT || this.z < 0 || this.z > FIELD_WIDTH){
collision = true;
}
// if(this.intersectWalls()){
// collision = true;
// }
if(collision){
this.x = oldX; this.y = oldY; this.z = oldZ;
}
return !collision;
}
move(distance){
const oldX = this.x, oldY = this.y;
this.isMoving = 1;
this.x += distance * Math.cos(this.angle);
this.y += distance * Math.sin(this.angle);
let collision = false;
if(this.x < 0 || this.x + this.width >= FIELD_WIDTH || this.y < 0 || this.y + this.height >= FIELD_HEIGHT){
collision = true;
}
// if(this.intersectWalls()){
// collision = true;
// }
if(collision){
this.x = oldX; this.y = oldY;
}
return !collision;
}
side(distance){
const oldX = this.x, oldY = this.y;
this.isMoving = 1;
this.x += distance * Math.cos(this.angle+3.1415/2);
this.y += distance * Math.sin(this.angle+3.1415/2);
let collision = false;
if(this.x < 0 || this.x + this.width >= FIELD_WIDTH || this.y < 0 || this.y + this.height >= FIELD_HEIGHT){
collision = true;
}
// if(this.intersectWalls()){
// collision = true;
// }
if(collision){
this.x = oldX; this.y = oldY;
}
return !collision;
}
intersect(obj){
return (this.x <= obj.x + obj.width) &&
(this.x + this.width >= obj.x) &&
(this.y <= obj.y + obj.height) &&
(this.y + this.height >= obj.y) &&
(this.z <= obj.z + obj.tall - obj.tall/2*obj.isSquat) &&
(this.z + this.tall - this.tall/2*this.isSquat >= obj.z) ;
}
intersectXY(obj){ //wall
return (this.x <= obj.x + obj.width) &&
(this.x >= obj.x) &&
(this.y <= obj.y + obj.height) &&
(this.y >= obj.y) &&
(this.z <=obj.z + obj.tall * 0.9 ) &&
(this.z + this.tall - this.tall/2*this.isSquat>=obj.z) ;
}
intersectZu(obj){ //wall
return (this.x <= obj.x + obj.width) &&
(this.x + this.width >= obj.x) &&
(this.y <= obj.y + obj.height) &&
(this.y + this.height >= obj.y) &&
(this.z <= obj.z + obj.tall) &&
(this.z >= obj.z + obj.tall - mWallThick*0.99) ;
}
intersectSlopeZ(obj){ //wall
return (this.x <= obj.x + mFieldGridSize) &&
(this.x >= obj.x) &&
(this.y <= obj.y + mFieldGridSize) &&
(this.y >= obj.y) &&
(this.z <=obj.z + mFieldGridSize/2 ) &&
(this.z >=obj.z-mFieldGridSize/2 -mWallThick) ;
}
intersectWalls(){
return Object.values(walls).some((wall) => this.intersect(wall));
}
intersectRocks(){
return Object.values(rocks).some((rock) => this.intersect(rock));
}
toJSON(){
return {id: this.id, x: this.x, y: this.y, z: this.z, width: this.width, height: this.height, tall:this.tall , angle: this.angle, angle2: this.angle2, isMoving: this.isMoving};
}
}
function mCreateWall(px,py,pz,pw,pa)
{
console.log('mCreateWall()');
const wall = new Wall({
x: px,
y: py,
z: pz,
width: mWallSize,
height: mWallThick,
tall: mWallSize,
});
wall.angle = pa; //this.angle; // + 3.1415/2;
wall.angle = Math.floor( (wall.angle+3.1415/4) / (3.1415/2)) * 3.1415/2;
wall.angle2 = 0;
// player geo center
var x_ = px + pw/2 * Math.cos(pa);
var y_ = py + pw/2 * Math.sin(pa);
wall.x = mFloorGS(x_); //Math.floor(x_/mFieldGridSize)*mFieldGridSize;
wall.y = mFloorGS(y_); //Math.floor(y_/mFieldGridSize)*mFieldGridSize;
wall.z = mFloorGS(pz);
wall.x += mFieldGridSize * Math.cos(wall.angle);
wall.y += mFieldGridSize * Math.sin(wall.angle);
if( (wall.angle==0) || (wall.angle==3.1415*2) ){
//console.log('pos:'+x_+','+y_+' wall:'+wall.x+','+wall.y);
if( x_+this.width/2>=wall.x ){
wall.x += mFieldGridSize;
}
wall.width = mWallThick;
wall.height = mWallSize;
//console.log('pos:'+x_+','+y_+' wall:'+wall.x+','+wall.y);
}
else if(wall.angle==3.1415){
if( x_-this.width/2>=wall.x+mFieldGridSize ){
wall.x += mFieldGridSize;
}
wall.width = mWallThick;
wall.height = mWallSize;
}
else if(wall.angle==3.1415/2){
if( y_+this.width/2>=wall.y ){
wall.y += mFieldGridSize;
}
}
else if(wall.angle==3.1415/2*3){
if( y_-this.width/2>=wall.y+mFieldGridSize ){
wall.y += mFieldGridSize;
}
}
console.log('mCreateWall:'+wall.id);
return wall;
}
class Player extends GameObject{
constructor(obj={}){
super(obj);
this.socketId = obj.socketId;
this.nickname = obj.nickname;
this.width = 80;
this.height = 80;
this.tall = mPlayerTall; //original
this.health = this.maxHealth = 100;
this.shield = this.maxShield = 100;
this.bullets = {};
this.weapon = 1;
this.point = 0;
this.movement = {};
this.shootCount = [0,0,0];
this.isShoot = 0;
this.isSquat = 0;
this.timeCount = 0;
this.shootTime = 0;
this.giveDamage = 0;
this.giveDamageTime = 0;
this.isBot = 0;
this.isGrounded = 1;
this.isJump = 0;
this.jumpTime = 0;
this.jumpStartZ = 0;
this.bandages = 0;
this.potions = 0;
this.isEmote = 0;
this.mode = 0; // 0:falling, 1:shoot, 2:build
this.buildTemp = null;
this.buildType = 0; //0:wall, 1:floor
this.inStorm = 0;
do{
this.x = Math.random() * (FIELD_WIDTH - this.width);
this.y = Math.random() * (FIELD_HEIGHT - this.height);
this.z = 0;//START_TALL;
this.angle = Math.random() * 2 * Math.PI;
this.angle2 = 0;
}while( (this.intersectWalls()) && (this.intersectRocks()) );
this.z = START_TALL;
}
shoot(){
if(this.mode != 1 ){
return;
}
const bullet = new Bullet({
x: this.x + this.width/2 * Math.cos(this.angle),
y: this.y + this.width/2 * Math.sin(this.angle),
z: this.z + (this.tall -this.tall/2*this.isSquat )*0.7,
angle: this.angle,
angle: this.angle2,
player: this,
damageV: 20,
// dx: Math.cos(this.angle2) * Math.cos(this.angle),
// dy: Math.cos(this.angle2) * Math.sin(this.angle),
// dz: Math.sin(this.angle2),
});
//console.log('bullet angle:'+bullet.angle+', '+bullet.angle2);
bullet.dx = Math.cos(-this.angle2) * Math.cos(this.angle);
bullet.dy = Math.cos(-this.angle2) * Math.sin(this.angle);
bullet.dz = Math.sin(-this.angle2);
//console.log('bullet d:'+bullet.dx+', '+bullet.dy+', '+bullet.dz);
if(this.weapon==2){
bullet.damageV = 50;
bullet.height = mBulletSize*5;
bullet.tall = mBulletSize*5;
}
else if(this.weapon==3){
bullet.damageV = 120;
}
//bullet.move(this.width/2);
//bullet.side(this.width/2*Math.cos(this.angle));
this.bullets[bullet.id] = bullet;
bullets[bullet.id] = bullet;
//io.to(this.socketId).emit('shoot', this.weapon);
//console.log('shoot from;'+this.socketId);
io.sockets.emit('shoot',[this.socketId, this.weapon]);
}
funcWall(){
const wall = new Wall({
x: this.x,
y: this.y,
z: this.z,
width: mWallSize,
height: mWallThick,
tall: mWallSize,
});
wall.angle = this.angle; // + 3.1415/2;
//console.log('wall.angle:'+wall.angle);
wall.angle = Math.floor( (wall.angle+3.1415/4) / (3.1415/2)) * 3.1415/2;
wall.angle2 = 0;
//console.log('wall.angle:'+wall.angle);
// player geo center
var x_ = this.x + this.width/2 * Math.cos(this.angle);
var y_ = this.y + this.width/2 * Math.sin(this.angle);
wall.x = mFloorGS(x_); //Math.floor(x_/mFieldGridSize)*mFieldGridSize;
wall.y = mFloorGS(y_); //Math.floor(y_/mFieldGridSize)*mFieldGridSize;
wall.z = mFloorGS(this.z);
var dz_ = this.z - mFloorGS(this.z);
//console.log('dz_:'+dz_+', angle2:'+this.angle2);
if( (dz_>mFieldGridSize/2) && (this.angle2<-3.1415/6) ){
wall.z += mFieldGridSize;
}
wall.x += mFieldGridSize * Math.cos(wall.angle);
wall.y += mFieldGridSize * Math.sin(wall.angle);
wall.p1x = 0.0;
wall.p1y = 0.0;
wall.p1z = mFieldGridSize;
wall.p2x = 0.0;
wall.p2y = mFieldGridSize;
wall.p2z = 0.0;
if( (wall.angle==0) || (wall.angle==3.1415*2) ){
//console.log('pos:'+x_+','+y_+' wall:'+wall.x+','+wall.y);
if( x_+this.width/2>=wall.x ){
wall.x += mFieldGridSize;
}
wall.width = mWallThick;
wall.height = mWallSize;
//console.log('pos:'+x_+','+y_+' wall:'+wall.x+','+wall.y);
}
else if(wall.angle==3.1415){
if( x_-this.width/2>=wall.x+mFieldGridSize ){
wall.x += mFieldGridSize;
}
wall.width = mWallThick;
wall.height = mWallSize;
}
else if(wall.angle==3.1415/2){
if( y_+this.width/2>=wall.y ){
wall.y += mFieldGridSize;
}
wall.p2x = mFieldGridSize;
wall.p2y = 0.0;
wall.p2z = 0.0;
}
else if(wall.angle==3.1415/2*3){
if( y_-this.width/2>=wall.y+mFieldGridSize ){
wall.y += mFieldGridSize;
}
wall.p2x = mFieldGridSize;
wall.p2y = 0.0;
wall.p2z = 0.0;
}
wall.p0x = wall.x;
wall.p0y = wall.y;
wall.p0z = wall.z;
return wall;
}
buildWall(){
this.mode = 2;
this.buildType = 0;
this.buildTemp = this.funcWall();
}
funcFloor(){
const wall = new Wall({
x: this.x,
y: this.y,
z: this.z,
width: mWallSize,
height: mWallSize,
tall: mWallThick,
});
wall.angle = this.angle; // + 3.1415/2;
//console.log('wall.angle:'+wall.angle);
wall.angle = Math.floor( (wall.angle+3.1415/4) / (3.1415/2)) * 3.1415/2;
wall.angle2 = 0;
//console.log('wall.angle:'+wall.angle);
// player geo center
var x_ = this.x + this.width/2 * Math.cos(this.angle);
var y_ = this.y + this.width/2 * Math.sin(this.angle);
wall.x = mFloorGS(x_); //Math.floor(x_/mFieldGridSize)*mFieldGridSize;
wall.y = mFloorGS(y_); //Math.floor(y_/mFieldGridSize)*mFieldGridSize;
wall.z = mFloorGS(this.z) - mWallThick;
wall.x += mFieldGridSize * Math.cos(wall.angle);
wall.y += mFieldGridSize * Math.sin(wall.angle);
wall.p1x = mFieldGridSize;
wall.p1y = 0.0;
wall.p1z = 0.0;
wall.p2x = 0.0;
wall.p2y = mFieldGridSize;
wall.p2z = 0.0;
if( this.angle2<-3.1415/4 ){
wall.z += mFieldGridSize;
}
if( (wall.angle==0) || (wall.angle==3.1415*2) ){
//console.log('pos:'+x_+','+y_+' wall:'+wall.x+','+wall.y);
if( x_+this.width/2>=wall.x ){
wall.x += mFieldGridSize;
}
// wall.width = mWallThick;
// wall.height = mWallSize;
//console.log('pos:'+x_+','+y_+' wall:'+wall.x+','+wall.y);
}
else if(wall.angle==3.1415){
if( x_-this.width/2>=wall.x+mFieldGridSize ){
wall.x += mFieldGridSize;
}
// wall.width = mWallThick;
// wall.height = mWallSize;
}
else if(wall.angle==3.1415/2){
if( y_+this.width/2>=wall.y ){
wall.y += mFieldGridSize;
}
}
else if(wall.angle==3.1415/2*3){
if( y_-this.width/2>=wall.y+mFieldGridSize ){
wall.y += mFieldGridSize;
}
}
if( (this.angle2<-3.1415/4) || (this.angle2>3.1415/4) ){
wall.x = mFloorGS(x_);
wall.y = mFloorGS(y_);
}
wall.p0x = wall.x;
wall.p0y = wall.y;
wall.p0z = wall.z;
return wall;
}
buildFloor(){
this.mode = 2;
this.buildType = 1;
this.buildTemp = this.funcFloor();
}
funcSlope(){
const slope = new Slope({
x: this.x,
y: this.y,
z: this.z,
width: mWallSize*1.414,
height: mWallSize*1.0,
tall: mWallThick,
});
slope.angle = this.angle; // + 3.1415/2;
slope.angle = Math.floor( (slope.angle+3.1415/4) / (3.1415/2)) * 3.1415/2;
slope.angle2 = Math.PI/4;
//console.log('wall.angle:'+wall.angle);
// player geo center
var x_ = this.x;// + this.width/2 * Math.cos(this.angle);
var y_ = this.y;// + this.width/2 * Math.sin(this.angle);
slope.x = mFloorGS(x_); //Math.floor(x_/mFieldGridSize)*mFieldGridSize;
slope.y = mFloorGS(y_); //Math.floor(y_/mFieldGridSize)*mFieldGridSize;
slope.z = mFloorGS(this.z) + mFieldGridSize/2;
var dz_ = this.z - mFloorGS(this.z);
console.log('dz_:'+dz_+', angle2:'+this.angle2);
if( (dz_>mFieldGridSize/2) && (this.angle2<-3.1415/6) ){
slope.z += mFieldGridSize;
}
slope.x += mFieldGridSize * Math.cos(slope.angle);
slope.y += mFieldGridSize * Math.sin(slope.angle);
if( (this.angle2>3.1415/4) ){
slope.x = mFloorGS(x_);
slope.y = mFloorGS(y_);
}
slope.p1x = mFieldGridSize;
slope.p1y = 0.0;
slope.p1z = mFieldGridSize;
slope.p2x = 0.0;
slope.p2y = mFieldGridSize;
slope.p2z = 0.0;
if( (slope.angle==0) || (slope.angle==3.1415*2) ){
}
else if(slope.angle==3.1415){
slope.p1x = -mFieldGridSize;
slope.p1y = 0.0;
slope.p1z = mFieldGridSize;
}
else if(slope.angle==3.1415/2){
slope.p1x = 0.0;
slope.p1y = mFieldGridSize;
slope.p1z = mFieldGridSize;
slope.p2x = mFieldGridSize;
slope.p2y = 0.0;
slope.p2z = 0.0;
}
else if(slope.angle==3.1415/2*3){
slope.p1x = 0.0;
slope.p1y = -mFieldGridSize;
slope.p1z = mFieldGridSize;
slope.p2x = mFieldGridSize;
slope.p2y = 0.0;
slope.p2z = 0.0;
}
slope.p0x = slope.x;
slope.p0y = slope.y;
slope.p0z = slope.z - mFieldGridSize/2;
return slope;
}
buildSlope(){
this.mode = 2;
this.buildType = 2;
this.buildTemp = this.funcSlope();
}
doBuild(){
if(this.buildType==0){
var wall = this.funcWall();
walls[wall.id] = wall;
}
else if(this.buildType==1){
var wall = this.funcFloor();
walls[wall.id] = wall;
}
else if(this.buildType==2){
var slope = this.funcSlope();
slopes[slope.id] = slope;
}
}
useBandage(){
if((this.bandages>0) && this.health0) && this.shield0){
this.shield -= v_;
if(this.shield<0){
v_ = -this.shield;
this.shield = 0;
}
else{
v_ = 0;
}
}
this.health -= v_;
this.health = Math.max(0,this.health);
io.to(this.socketId).emit('recievedamage',1.0);
if(this.health === 0){
this.remove();
}
}
stormDamage(v_){
this.health -= v_;
this.health = Math.max(0,this.health);
//io.to(this.socketId).emit('recievedamage',1.0);
if(this.health === 0){
this.remove();
}
}
remove(){
io.sockets.emit('deadothers');
delete players[this.id];
io.to(this.socketId).emit('dead', (Object.keys(players).length+1) );
}
toJSON(){
return Object.assign(super.toJSON(), {health: this.health, maxHealth: this.maxHealth, shield: this.shield, maxShield: this.maxShield, socketId: this.socketId, point: this.point, nickname: this.nickname, weapon: this.weapon, isShoot: this.isShoot, isSquat: this.isSquat, giveDamage: this.giveDamage, timeCount: this.timeCount, isBot: this.isBot, bandages: this.bandages, potions: this.potions, isEmote: this.isEmote, mode: this.mode, buildTemp: this.buildTemp, buildType: this.buildType, inStorm: this.inStorm});
}
}
class Bullet extends GameObject{
constructor(obj){
super(obj);
this.width = mBulletLength;// mBulletSize*20;
this.height = mBulletSize;
this.tall = mBulletSize;
this.angle = obj.player.angle;
this.angle2 = obj.player.angle2;
this.player = obj.player;
this.damageV = 20;
this.isSquat = 0;
this.dx = 0.0;
this.dy = 0.0;
this.dz = 0.0;
}
remove(){
delete this.player.bullets[this.id];
delete bullets[this.id];
}
}
class BotPlayer extends Player{
constructor(obj){
super(obj);
this.timer = setInterval(() => {
this.timeCount+=1;
this.isMoving=1;
this.mode = 1;
this.movement.forward = false;
if(Math.random()*10<5){
this.movement.forward = true;
}
if(! this.move(mPlayerSpeed*0.01)){
this.angle = Math.random() * Math.PI * 2;
}
Object.values(players).forEach((player) => {
if(player.isBot == 0){
var dx_ = player.x - this.x;
var dy_ = player.y - this.y;
var dz_ = player.z - this.z;
this.angle = Math.atan2(dy_,dx_);
var dr_ = Math.sqrt(dx_*dx_+dy_*dy_);
this.angle2 = -Math.atan2(dz_,dr_);
//console.log('angle2:'+ this.angle2/Math.PI*180.0+', ('+dz_+','+dr_);
return;
}
});
if(Math.random()<0.01){
this.shoot();
var a_ = [1, 0.2];
}
if(Math.random()<0.01){
this.squat();
}
if(Math.random()<0.01){
this.jump();
}
Object.values(players).forEach((player) => {
if(this.intersect(player)){
if(this !== player){
this.angle = Math.random() * Math.PI * 2;
return;
}
}
});
Object.values(walls).forEach((wall) => {
if(this.intersect(wall)){
this.angle = Math.random() * Math.PI * 2;
return;
}
});
Object.values(rocks).forEach((rock) => {
if(this.intersectXY(rock)){
this.angle = Math.random() * Math.PI * 2;
return;
}
});
}, 1000/30);
}
remove(){
const bandage = new Bandage({
x: this.x,
y: this.y,
z: this.z,
});
bandages[bandage.id] = bandage;
if(Math.random()*10<5){
const potion = new Potion({
x: this.x + this.width,
y: this.y + this.width,
z: this.z,
});
potions[potion.id] = potion;
}
super.remove();
clearInterval(this.timer);
//reborn
// setTimeout(() => {
// const bot = new BotPlayer({nickname: this.nickname});
// players[bot.id] = bot;
// }, 3000);
}
}
class Wall extends GameObject{
constructor(obj={}){
super(obj);
this.health = this.maxHealth = 100;
this.p0x = 0.0;
this.p0y = 0.0;
this.p0z = 0.0;
this.p1x = 0.0;
this.p1y = 0.0;
this.p1z = 0.0;
this.p2x = 0.0;
this.p2y = 0.0;
this.p2z = 0.0;
}
damage(v_){
//this.health --;
this.health -= v_;
this.health = Math.max(0,this.health);
//console.log('wall damage:'+this.id+', health:'+this.health);
if(this.health === 0){
this.remove();
}
}
remove(){
delete walls[this.id];
}
}
class Slope extends GameObject{
constructor(obj={}){
super(obj);
this.health = this.maxHealth = 100;
this.p0x = 0.0;
this.p0y = 0.0;
this.p0z = 0.0;
this.p1x = 0.0;
this.p1y = 0.0;
this.p1z = 0.0;
this.p2x = 0.0;
this.p2y = 0.0;
this.p2z = 0.0;
}
damage(v_){
//this.health --;
this.health -= v_;
this.health = Math.max(0,this.health);
console.log('wall damage:'+this.id+', v:'+v_+', health:'+this.health);
if(this.health === 0){
this.remove();
}
}
remove(){
delete slopes[this.id];
}
}
class Rock extends GameObject{
constructor(obj={}){
super(obj);
// this.p0x = 0.0;
// this.p0y = 0.0;
// this.p0z = 0.0;
// this.p1x = 0.0;
// this.p1y = 0.0;
// this.p1z = 0.0;
// this.p2x = 0.0;
// this.p2y = 0.0;
// this.p2z = 0.0;
}
remove(){
delete rocks[this.id];
}
}
class Bandage extends GameObject{
constructor(obj={}){
super(obj);
this.width = mBandageSize;
this.height = mBandageSize;
this.tall = mBandageSize*2;
}
remove(){
delete bandages[this.id];
}
}
class Potion extends GameObject{
constructor(obj={}){
super(obj);
this.width = mBandageSize;
this.height = mBandageSize;
this.tall = mBandageSize*2;
}
remove(){
delete potions[this.id];
}
}
class Storm extends GameObject{
constructor(obj={}){
super(obj);
}
}
var mStorm =
new Storm({
x: 0,
y: 0,
z: 0,
width: FIELD_WIDTH,
height: FIELD_WIDTH,
tall: FIELD_WIDTH,
angle: 0,
angle2: 0,
isSquat: 0,
});
let players = {};
let bullets = {};
let walls = {};
let slopes = {};
let rocks = {};
let bandages = {};
let potions = {};
var mGlobalTime = 0;
//function mCreateWall(n_){
// for(let i=0; i=0.0) && (s<=1.0) && (t>=0.0) && (t<=1.0) && (e>=0.0) && (e<=mBulletLength) ){
j = true;
res = [j, 0.0];
}
//console.log(' res:'+res);
return res;
}
//console.log('mBWC:'+mBulletWallCollision() );
function mBulletPlayerCollision(player_, bullet_)
{
//var plane1;
const plane1 = new Wall({
x: 0,
y: 0,
z: 0,
});
var t_ = player_.tall - player_.tall/2*player_.isSquat;
// y-z plane
plane1.p0x = player_.x;
plane1.p0y = player_.y - player_.width/2;
plane1.p0z = player_.z;
plane1.p1x = 0.0;
plane1.p1y = 0.0;
plane1.p1z = t_;
plane1.p2x = 0.0;
plane1.p2y = player_.width;
plane1.p2z = 0.0;
var res1 =mBulletWallCollision(plane1, bullet_);
if(res1[0]){
return res1;
}
// x-z plane
plane1.p0x = player_.x - player_.width/2;
plane1.p0y = player_.y;
plane1.p0z = player_.z;
plane1.p1x = 0.0;
plane1.p1y = 0.0;
plane1.p1z = t_;
plane1.p2x = player_.width;
plane1.p2y = 0.0;
plane1.p2z = 0.0;
res1 =mBulletWallCollision(plane1, bullet_);
if(res1[0]){
return res1;
}
// x-z plane
plane1.p0x = player_.x - player_.width/2;
plane1.p0y = player_.y - player_.width/2;
plane1.p0z = player_.z + t_;
plane1.p1x = player_.width;
plane1.p1y = 0.0;
plane1.p1z = 0.0;
plane1.p2x = 0.0;
plane1.p2y = player_.width;
plane1.p2z = 0.0;
res1 =mBulletWallCollision(plane1, bullet_);
return res1;
}
function mBulletRockCollision(rock_, bullet_)
{
//var plane1;
const plane1 = new Wall({
x: 0,
y: 0,
z: 0,
});
// x- plane
plane1.p0x = rock_.x;
plane1.p0y = rock_.y;
plane1.p0z = rock_.z;
plane1.p1x = 0.0;
plane1.p1y = 0.0;
plane1.p1z = rock_.tall;
plane1.p2x = 0.0;
plane1.p2y = rock_.height;
plane1.p2z = 0.0;
var res1 =mBulletWallCollision(plane1, bullet_);
if(res1[0]){
return res1;
}
// x+ plane
plane1.p0x = rock_.x + rock_.width;
plane1.p0y = rock_.y;
plane1.p0z = rock_.z;
plane1.p1x = 0.0;
plane1.p1y = 0.0;
plane1.p1z = rock_.tall;
plane1.p2x = 0.0;
plane1.p2y = rock_.height;
plane1.p2z = 0.0;
res1 =mBulletWallCollision(plane1, bullet_);
if(res1[0]){
return res1;
}
// y- plane
plane1.p0x = rock_.x;
plane1.p0y = rock_.y;
plane1.p0z = rock_.z;
plane1.p1x = 0.0;
plane1.p1y = 0.0;
plane1.p1z = rock_.tall;
plane1.p2x = rock_.width;
plane1.p2y = 0.0;
plane1.p2z = 0.0;
res1 =mBulletWallCollision(plane1, bullet_);
if(res1[0]){
return res1;
}
// y+ plane
plane1.p0x = rock_.x;
plane1.p0y = rock_.y+rock_.height;
plane1.p0z = rock_.z;
plane1.p1x = 0.0;
plane1.p1y = 0.0;
plane1.p1z = rock_.tall;
plane1.p2x = rock_.width;
plane1.p2y = 0.0;
plane1.p2z = 0.0;
res1 =mBulletWallCollision(plane1, bullet_);
if(res1[0]){
return res1;
}
// z- plane
plane1.p0x = rock_.x;
plane1.p0y = rock_.y;
plane1.p0z = rock_.z;
plane1.p1x = rock_.width;
plane1.p1y = 0.0;
plane1.p1z = 0.0;
plane1.p2x = 0.0;
plane1.p2y = rock_.height;
plane1.p2z = 0.0;
res1 =mBulletWallCollision(plane1, bullet_);
if(res1[0]){
return res1;
}
// z+ plane
plane1.p0x = rock_.x;
plane1.p0y = rock_.y;
plane1.p0z = rock_.z + rock_.tall;
plane1.p1x = rock_.width;
plane1.p1y = 0.0;
plane1.p1z = 0.0;
plane1.p2x = 0.0;
plane1.p2y = rock_.height;
plane1.p2z = 0.0;
res1 =mBulletWallCollision(plane1, bullet_);
if(res1[0]){
return res1;
}
return res1;
}
io.on('connection', function(socket) {
let player = null;
socket.on('game-start', (config) => {
player = new Player({
socketId: socket.id,
nickname: config.nickname,
});
player.mode = 0;
player.timeCount = 0;
players[player.id] = player;
io.to(player.socketId).emit('id', player.socketId);
if(Object.keys(players).length == 1){
mCreateBot(9);
mGlobalTime = 0;
mVictoryTime = 0;
mSetStorm();
}
// if(Object.keys(walls).length < 20){
// mCreateWall(20-Object.keys(walls).length);
// }
if(Object.keys(bandages).length < 20){
mCreateBandage(20-Object.keys(bandages).length);
}
if(Object.keys(potions).length < 20){
mCreatePotion(20-Object.keys(potions).length);
}
});
socket.on('movement', function(movement) {
if(!player || player.health===0){return;}
player.movement = movement;
});
socket.on('mousemoveX', function(v) {
if(!player || player.health===0){return;}
player.angle += v;
});
socket.on('mousemoveY', function(v) {
if(!player || player.health===0){return;}
player.angle2 += v;
player.angle2 = Math.max(player.angle2,-3.1415/2*0.99);
player.angle2 = Math.min(player.angle2,3.1415/2*0.99);
//console.log('angle2:'+player.angle2);
});
// socket.on('shoot', function(){
// if(!player || player.health===0){return;}
// player.shoot();
// socket.broadcast.emit('shootFromOthers',0.3);
// });
socket.on('squat', function(){
if(!player || player.health===0){return;}
player.squat();
});
socket.on('jump', function(){
if(!player || player.health===0){return;}
player.jump();
});
socket.on('buildWall', function(){
if(!player || player.health===0){return;}
if(player.mode!=0){
player.buildWall();
}
});
socket.on('buildFloor', function(){
if(!player || player.health===0){return;}
if(player.mode!=0){
player.buildFloor();
}
});
socket.on('buildSlope', function(){
if(!player || player.health===0){return;}
if(player.mode!=0){
player.buildSlope();
}
});
socket.on('doBuild', function(){
if(!player || player.health===0){return;}
if(player.mode!=0){
player.doBuild();
}
});
socket.on('jumper', function(){
if(!player || player.health===0){return;}
player.z = START_TALL;
player.mode = 0;
});
socket.on('weaponChange', function(v_){
if(!player || player.health===0){return;}
player.weaponChange(v_);
});
socket.on('reload', function(){
if(!player || player.health===0){return;}
player.shootCount[player.weapon-1]=0;
});
socket.on('useBandage', function(){
if(!player || player.health===0){return;}
player.useBandage();
});
socket.on('usePotion', function(){
if(!player || player.health===0){return;}
player.usePotion();
});
socket.on('emote', function(){
if(!player || player.health===0){return;}
player.emote();
});
socket.on('modeChanged', function(v_){
if(!player || player.health===0){return;}
player.mode = v_;
});
socket.on('disconnect', () => {
if(!player){return;}
delete players[player.id];
player = null;
});
});
function mRemoveAllPlayer()
{
Object.values(players).forEach((player) => {
player.remove();
});
}
var mVictoryTime = 0;
setInterval(() => {
mGlobalTime += 1;
//Update strom
mStorm.width -= FIELD_WIDTH/(30.0*60.0*5.0);
mStorm.width = Math.max(1,mStorm.width);
//console.log('strom w:'+mStorm.width);
if(Object.keys(players).length == 1){
Object.values(players).forEach((player) => {
if(player.isBot==0){
if(mVictoryTime == 0){
io.to(player.socketId).emit('victory');
}
else if(mVictoryTime >= 300){
mVictoryTime = 0;
player.remove();
}
mVictoryTime += 1;
}//
//player.remove();
//setTimeout(mRemoveAllPlayer, 10000);
});
}
//console.log('players:'+ Object.keys(players).length);
Object.values(players).forEach((player) => {
const movement = player.movement;
const oldX = player.x, oldY = player.y, oldZ = player.z;
player.timeCount += 1;
//player.isMoving = 0;
//Storm
var dx_ = mStorm.x - player.x;
var dy_ = mStorm.y - player.y;
var dis_ = Math.sqrt( dx_*dx_ + dy_*dy_ );
if(player.isBot==0){
//console.log('storm:'+mStorm.x+','+mStorm.y+', p:'+player.x+','+player.y+' dis_:'+dis_+', width:'+mStorm.width);
}
player.inStorm = 0;
if( dis_ > mStorm.width ){
//console.log('in storm:');
//player.health -= 1.0/30.0;
player.stormDamage(1.0/30.0);
player.inStorm = 1;
//io.to(player.socketId).emit('recievedamage',1.0);
}
var s_ = mPlayerSpeed;
if(player.isSquat==1){
s_ = mPlayerSpeed / 2;
}
if(player.mode==0){
s_ = mPlayerSpeed * 5;
}
if((player.isJump==1) && (player.mode!=0)){
player.z = player.jumpStartZ + mJumpHight * Math.sin(3.1415*(player.timeCount-player.jumpTime)/mJumpFrame);
if(player.timeCount-player.jumpTime>mJumpFrame){
player.z = player.jumpStartZ;
player.isJump = 0;
//player.z -= mFieldGridSize;
}
}
if(!movement.forward){
player.isMoving = 0;
}
if(movement.forward){
player.move(s_);
}
if(movement.back){
player.move(-s_);
}
if(movement.left){
player.angle -= mPlayerTurnSpeed;
}
if(movement.right){
player.angle += mPlayerTurnSpeed;
}
if(movement.angup){
player.angle2 -= mPlayerTurnSpeed/2;
}
if(movement.angdown){
player.angle2 += mPlayerTurnSpeed/2;
}
if(movement.sideleft){
player.side(-s_);
}
if(movement.sideright){
player.side(s_);
}
player.x = Math.floor(player.x);
player.y = Math.floor(player.y);
player.z = Math.floor(player.z);
if( player.angle > 2*3.1415 ){
player.angle = player.angle - 2*3.1415;
}
if( player.angle < 0 ){
player.angle = player.angle + 2*3.1415;
}
// build
if(player.mode == 2){
if(player.buildType==0){
player.buildWall();
}
else if(player.buildType==1){
player.buildFloor();
}
else if(player.buildType==2){
player.buildSlope();
}
}
player.isShoot = 0;
if(player.mode == 1)
{
if(movement.shoot){
var mwp = player.weapon -1;
if(player.timeCount>=player.shootTime+mShootCycle[mwp])
{
if(player.shootCount[mwp]=player.timeCount) &&
(player.giveDamage>0) )
{
io.to(player.socketId).emit('displayGiveDamage', player.timeCount-player.giveDamageTime);//1, player.giveDamage);
}
else{
player.giveDamage = 0;
io.to(player.socketId).emit('displayGiveDamage',0);
}
var contactZ =0;
player.isGrounded = 0;
Object.values(walls).forEach((wall) => {
if(player.intersectZu(wall)){
//console.log('playerz:'+ player.z);
player.z = wall.z + wall.tall; //mWallSize;
player.isJump = 0;
player.isGrounded = 1;
contactZ = 1;
if(player.mode == 0){
player.mode = 1;
}
//console.log('intersectZu wall:'+ (wall.z+mWallSize) + ', '+player.z);
return;
}
});
Object.values(rocks).forEach((rock) => {
if(player.intersectZu(rock)){
player.z = rock.z + rock.tall; //mWallSize;
player.isJump = 0;
player.isGrounded = 1;
contactZ = 1;
if(player.mode == 0){
player.mode = 1;
}
return;
}
});
// if(player.isBot==0){
// console.log('player.x:'+ player.x);
// console.log('player.z:'+ player.z);
// }
Object.values(slopes).forEach((slope) => {
// if(player.isBot==0){
// console.log('slope.x:'+ slope.x);
// console.log('slope.z:'+ slope.z);
// }
if(player.intersectSlopeZ(slope)){
//console.log(':');
//console.log('player.x-slope.x:'+ (player.x-slope.x) );
var c_ = 0;
var dz_ = 0;
if((slope.angle==0) || (slope.angle==3.1415*2)){
dz_ = slope.z - mFieldGridSize/2 + (player.x-slope.x);// + mWallThick;
}
else if(slope.angle==3.1415){
dz_ = slope.z - mFieldGridSize/2 + mFieldGridSize - (player.x-slope.x);// + mWallThick;
}
else if(slope.angle==3.1415/2){
dz_ = slope.z - mFieldGridSize/2 + (player.y-slope.y);// + mWallThick;
}
else if(slope.angle==3.1415/2*3){
dz_ = slope.z - mFieldGridSize/2 + mFieldGridSize - (player.y-slope.y);// + mWallThick;
}
// console.log('player.z:'+ player.z);
// console.log('dz:'+ dz_);
if(( player.z <= dz_ ) && (player.z >= dz_ - mWallThick ) ){
player.z = dz_;
c_ = 1;
}
else if( (player.z + player.tall >= dz_-mWallThick) && (player.z <= dz_) ){
player.x = oldX;
player.y = oldY;
return;
}
//console.log('c_:'+ c_);
if(c_==1){
player.isJump = 0;
player.isGrounded = 1;
contactZ = 1;
if(player.mode == 0){
player.mode = 1;
}
return;
}
//console.log('player:'+ player.x+','+player.y+','+player.z);
//console.log('intersectZu wall:'+ (wall.z+mWallSize) + ', '+player.z);
}
});
// if(player.isBot==0){
// console.log('player.x:'+ player.x);
// console.log('player.z:'+ player.z);
// console.log(':');
// }
if( ((player.isJump==0) && (player.isGrounded==0)) || (player.mode==0) ){
//fall
player.z -= mFallSpeed;
}
//Ground contact
if(player.z<0){
player.z = 0;
player.isJump = 0;
player.isGrounded = 1;
if(player.mode == 0){
player.mode = 1;
}
}
Object.values(walls).forEach((wall) => {
if(player.intersectXY(wall)){
player.x = oldX;
player.y = oldY;
//console.log('intersectXY wall:'+ player.x + ', '+player.y+ ', '+player.z);
return;
}
});
Object.values(rocks).forEach((rock) => {
if(player.intersectXY(rock)){
player.x = oldX;
player.y = oldY;
//console.log('intersectXY wall:'+ player.x + ', '+player.y+ ', '+player.z);
return;
}
});
Object.values(bandages).forEach((bandage) => {
if(player.intersect(bandage)){
player.bandages += 1;
io.to(player.socketId).emit('getItem',1.0);
bandage.remove();
return;
}
});
Object.values(potions).forEach((potion) => {
if(player.intersect(potion)){
player.potions += 1;
io.to(player.socketId).emit('getItem',1.0);
potion.remove();
return;
}
});
}); //player
//console.log("bullets:"+Object.values(bullets).length);
Object.values(bullets).forEach((bullet) =>{
// if(! bullet.move3(mBulletSpeed)){
// bullet.remove();
// return;
// }
Object.values(walls).forEach((wall) => {
//console.log('bullet wall collision:'+bullet.id+','+wall.id);
var res_ = mBulletWallCollision(wall, bullet);
if(res_[0]){
wall.damage(bullet.damageV);
bullet.remove();
io.sockets.emit('walldamage', 0.5);
return;
}
});
Object.values(slopes).forEach((slope) => {
//console.log('bullet wall collision:'+bullet.id+','+wall.id);
var res_ = mBulletWallCollision(slope, bullet);
if(res_[0]){
slope.damage(bullet.damageV);
bullet.remove();
io.sockets.emit('walldamage', 0.5);
return;
}
});
Object.values(rocks).forEach((rock) => {
//console.log('bullet wall collision:'+bullet.id+','+wall.id);
var res_ = mBulletRockCollision(rock, bullet);
if(res_[0]){
bullet.remove();
io.sockets.emit('walldamage', 0.5);
return;
}
});
});
Object.values(bullets).forEach((bullet) =>{
Object.values(players).forEach((player) => {
var res_ = mBulletPlayerCollision(player, bullet);
if(res_[0]){
if(player !== bullet.player){
player.damage(bullet.damageV);
bullet.remove();
bullet.player.point += 1;
//console.log('bullet intersect player:'+bullet.id+','+player.id);
io.to(bullet.player.socketId).emit('givedamage',1.0);
bullet.player.giveDamage=bullet.damageV;
bullet.player.giveDamageTime=bullet.player.timeCount;
}
return;
} //res
});
if(! bullet.move3(mBulletSpeed)){
bullet.remove();
return;
}
}); //bullet
io.sockets.emit('state', players, bullets, walls, slopes, rocks, bandages, potions, mStorm);
}, 1000/30);
app.use('/static', express.static(__dirname + '/static'));
app.get('/', (request, response) => {
response.sendFile(path.join(__dirname, '/static/index.html'));//'/static/3d.html'
});
// const port = parseInt(yargs.port) || 3000;
// server.listen(port, () => {
// console.log(`Starting server on port ${port}`);
// });
server.listen(PORT, function(){
console.log('server listening. Port:' + PORT);
});
//window.focus();
const socket = io();
const canvas2d = $('#canvas-2d')[0];
const context = canvas2d.getContext('2d');
const canvas3d = $('#canvas-3d')[0];
//const playerImage = $("#player-image")[0];
const canvasVic = $('#canvas-vic')[0];
const contextVic = canvasVic.getContext('2d');
const canvasMob = $('#canvas-mob')[0];
const contextMob = canvasMob.getContext('2d');
window.onload=function(){
console.log('------window.onload-------');
}
var mDeviceType = 'PC'; //0:PC, 1:Mobile
function mGetDeviceType() {
console.log('Device:'+navigator.userAgent);
if (navigator.userAgent.match(/iPhone|iPad|Android/)) { ///iPhone|Android.+Mobile/
mDeviceType = 'Mobile';
//$("#canvas-mob").show();
//return true;
} else {
//return false;
//$("#canvas-mob").hide();
}
//mDeviceType = navigator.userAgent;
}
mGetDeviceType();
console.log('mDeviceType:'+mDeviceType);
const FIELD_SC = 32;
const FIELD_WIDTH = 1000*FIELD_SC, FIELD_HEIGHT = 1000*FIELD_SC;
var mFieldGridSize = 250;
const renderer = new THREE.WebGLRenderer({canvas: canvas3d});
renderer.setClearColor('grey');//'skyblue'
renderer.shadowMap.enabled = true;
const scene = new THREE.Scene();
const camera = new THREE.PerspectiveCamera( 90, 2, 0.1, FIELD_WIDTH*2 );
const textureLoader = new THREE.TextureLoader();
// Floor
var mSc = 2;
const groundTexture = textureLoader.load('/static/image/p0305_m.jpeg');
const groundMaterial = new THREE.MeshLambertMaterial( { map: groundTexture } );
const floorGeometry = new THREE.PlaneGeometry(FIELD_WIDTH*2, FIELD_HEIGHT*2, 1, 1);
const floorMaterial = new THREE.MeshLambertMaterial({color : 'green'});//'lawngreen'
const floorMesh = new THREE.Mesh(floorGeometry, floorMaterial); //groundMaterial
floorMesh.position.set(FIELD_WIDTH/2, 0, FIELD_HEIGHT/2);
floorMesh.receiveShadow = true;
floorMesh.rotation.x = - Math.PI / 2;
scene.add(floorMesh);
// Side
const sideGeometry = new THREE.CylinderGeometry(FIELD_WIDTH,FIELD_WIDTH,FIELD_WIDTH*2,50,50,true); //(FIELD_WIDTH,FIELD_WIDTH,FIELD_WIDTH,50);
//const sideTexture = new THREE.Texture(playerImage);
const sideTexture = textureLoader.load('/static/image/ELFADSC08963_TP_V.jpg'); //skyimage1.png, skyimage1.png
//sideTexture.needsUpdate = true;
const sideMaterial = new THREE.MeshLambertMaterial({map: sideTexture, side: THREE.DoubleSide});//'lawngreen'
// const sideMaterial = new THREE.MeshBasicMaterial({
// map: texture, // テクスチャーを指定
// color: 0x007eff, // 色
// transparent: true, // 透明の表示許可
// blending: THREE.AdditiveBlending, // ブレンドモード
// side: THREE.DoubleSide, // 表裏の表示設定
// depthWrite: false // デプスバッファへの書き込み可否
// });
const sideMesh = new THREE.Mesh(sideGeometry, sideMaterial);
sideMesh.position.set(FIELD_WIDTH/2, FIELD_WIDTH-100, FIELD_HEIGHT/2);
sideMesh.receiveShadow = true;
//sideMesh.rotation.x = - Math.PI / 2;
scene.add(sideMesh);
// Grid
// for(var i=0;i {
const KeyToCommand = {
'ArrowUp': 'forward',
'ArrowDown': 'back',
'ArrowLeft': 'left',
'ArrowRight': 'right',
'w': 'forward',
's': 'back',
'a': 'sideleft',
'd': 'sideright',
};
const command = KeyToCommand[event.key];
if(command){
if(event.type === 'keydown'){
movement[command] = true;
}else{ /* keyup */
movement[command] = false;
}
socket.emit('movement', movement);
}
// if(event.key === ' ' && event.type === 'keydown'){
// socket.emit('shoot');
// mSoundEffect(ShotSound1,1.0);
// }
if(event.key === 'q' && event.type === 'keydown'){
socket.emit('buildWall');
//mSoundEffect(WallSetSound,1.0);
mMode = 2;
}
if(event.key === 'z' && event.type === 'keydown'){
socket.emit('buildFloor');
//mSoundEffect(WallSetSound,1.0);
mMode = 2;
}
if(event.key === 'e' && event.type === 'keydown'){
socket.emit('buildSlope');
//mSoundEffect(WallSetSound,1.0);
mMode = 2;
}
if(event.key === 'y' && event.type === 'keydown'){
socket.emit('jumper');
//mSoundEffect(WallSetSound,1.0);
//mMode = 2;
}
if(event.key === 'Control' && event.type === 'keydown'){
socket.emit('squat');
}
if(event.key === ' ' && event.type === 'keydown'){
socket.emit('jump');
}
if(event.key === '1' && event.type === 'keydown'){
socket.emit('weaponChange',1);
mWeapon = 1;
mSoundEffect(SetGunSound,1.0);
mMode = 1;
}
if(event.key === '2' && event.type === 'keydown'){
socket.emit('weaponChange',2);
mWeapon = 2;
mSoundEffect(SetGunSound,1.0);
mMode = 1;
}
if(event.key === '3' && event.type === 'keydown'){
socket.emit('weaponChange',3);
mWeapon = 3;
mSoundEffect(SetGunSound,1.0);
mMode = 1;
}
if(event.key === '4' && event.type === 'keydown'){
socket.emit('weaponChange',4);
mWeapon = 4;
mMode = 1;
}
if(event.key === '5' && event.type === 'keydown'){
socket.emit('weaponChange',5);
mWeapon = 5;
mMode = 1;
}
if(event.key === 'r' && event.type === 'keydown'){
socket.emit('reload');
if(mWeapon==1){
mSoundEffect(ReloadSound1,1.0);
}
if(mWeapon==2){
mSoundEffect(ReloadSound2,1.0);
}
if(mWeapon==3){
mSoundEffect(ReloadSound3,1.0);
}
}
if(event.key === 'f' && event.type === 'keydown'){
socket.emit('modeChanged', 1);
mMode = 1;
}
if(event.key === 'b' && event.type === 'keydown'){
socket.emit('emote');
}
if(event.key === 'p' && event.type === 'keydown'){
ElementRequestPointerLock(canvas2d);
}
if(event.key === 'h' && event.type === 'keydown'){
mShowHelp();
}
});
//--- Mouse event ---//
var mShootCount = 0;
canvas2d.addEventListener('mousedown', function(e)
{
if(mMode==1){
if(e.button==0){
if(mWeapon<=3){
movement['shoot'] = true;
socket.emit('movement', movement);
}
else if(mWeapon==4){
socket.emit('useBandage', 1);
}
else if(mWeapon==5){
socket.emit('usePotion', 1);
}
}//
if(e.button==2){
camera.zoom = 2;
if(mWeapon ==3){
camera.zoom = 10;
}
camera.updateProjectionMatrix();
//mSoundEffect(ShotSound2,1.0);
}
}//
else if(mMode==2){
socket.emit('doBuild');
mSoundEffect(WallSetSound,1.0);
//mMode = 1;
}
});
canvas2d.addEventListener('mouseup', function(e)
{
if(e.button==0){
movement['shoot'] = false;
socket.emit('movement', movement);
}
if(e.button==2){
camera.zoom = 1;
camera.updateProjectionMatrix();
}
});
var mLastMouseX = 0;
var mLastMouseY = 0;
canvas2d.addEventListener('mousemove', function(e)
{
// use pointer lock
var ang_ = (e.movementX) / 250 * 3.1415/2;
socket.emit('mousemoveX', ang_);
var ang2_ = (e.movementY) / 500 * 3.1415/2;
socket.emit('mousemoveY', ang2_);
//console.log('ang_:'+ang_+' movement:'+e.movementX);
});
canvas2d.addEventListener('wheel', function(e)
{
var v_ = e.wheelDelta;
if(v_<0){ //up
mWeapon += 1;
//mWeapon = Math.min(5,mWeapon);
if(mWeapon>=6){
mWeapon = 1;
}
}
if(v_>0){ //down
mWeapon -= 1;
//mWeapon = Math.max(1,mWeapon);
if(mWeapon<=0){
mWeapon = 5;
}
}
console.log('mWeapon:'+mWeapon);
socket.emit('weaponChange',mWeapon);
//console.log('wheel:'+v_);
});
//--- Touch event ---//
// Mobile gui
var mBtnRadius = canvasMob.height / 10;
var mBtnUp_Px = canvasMob.width / 5;
var mBtnUp_Py = canvasMob.height / 2;
var mBtnDown_Px = mBtnUp_Px;
var mBtnDown_Py = mBtnUp_Py + mBtnRadius * 3;
var mBtnRight_Px = mBtnUp_Px + mBtnRadius * 1.5;
var mBtnRight_Py = mBtnUp_Py + mBtnRadius * 1.5;
var mBtnLeft_Px = mBtnUp_Px - mBtnRadius * 1.5;
var mBtnLeft_Py = mBtnUp_Py + mBtnRadius * 1.5;
var mBtnAngUp_Px = canvasMob.width / 5 * 4;
var mBtnAngUp_Py = canvasMob.height / 3;
var mBtnAngDown_Px = mBtnAngUp_Px;
var mBtnAngDown_Py = mBtnAngUp_Py + mBtnRadius * 3;
var mBtnAngRight_Px = mBtnAngUp_Px + mBtnRadius * 1.5;
var mBtnAngRight_Py = mBtnAngUp_Py + mBtnRadius * 1.5;
var mBtnAngLeft_Px = mBtnAngUp_Px - mBtnRadius * 1.5;
var mBtnAngLeft_Py = mBtnAngUp_Py + mBtnRadius * 1.5;
var mBtnShoot_Px = canvasMob.width - mBtnRadius*1.5;
var mBtnShoot_Py = canvasMob.height / 4;
var mBtnReload_Px = mBtnShoot_Px;
var mBtnReload_Py = mBtnShoot_Py + mBtnRadius*2.5;
var mBtnJump_Px = mBtnShoot_Px - mBtnRadius*2.5;
var mBtnJump_Py = mBtnShoot_Py;
var mBtnSquat_Px = mBtnJump_Px;
var mBtnSquat_Py = mBtnReload_Py;
function mCreateMobileGUI(){
var W_ = canvasMob.width;
var H_ = canvasMob.height;
var x_,y_,w_,w2_,h_;
contextMob.clearRect(0, 0, W_, H_);
var lw_ = canvasMob.height / 100;
contextMob.globalAlpha = 0.5;
contextMob.strokeStyle = "blue";
contextMob.lineWidth = lw_;
contextMob.beginPath();
contextMob.arc(mBtnUp_Px, mBtnUp_Py, mBtnRadius, 0, Math.PI*2, 0);
contextMob.stroke();
contextMob.beginPath();
contextMob.arc(mBtnDown_Px, mBtnDown_Py, mBtnRadius, 0, Math.PI*2, 0);
contextMob.stroke();
contextMob.beginPath();
contextMob.arc(mBtnRight_Px, mBtnRight_Py, mBtnRadius, 0, Math.PI*2, 0);
contextMob.stroke();
contextMob.beginPath();
contextMob.arc(mBtnLeft_Px, mBtnLeft_Py, mBtnRadius, 0, Math.PI*2, 0);
contextMob.stroke();
contextMob.strokeStyle = "red";
contextMob.beginPath();
contextMob.arc(mBtnShoot_Px, mBtnShoot_Py, mBtnRadius, 0, Math.PI*2, 0);
contextMob.stroke();
contextMob.beginPath();
contextMob.arc(mBtnReload_Px, mBtnReload_Py, mBtnRadius, 0, Math.PI*2, 0);
contextMob.stroke();
contextMob.strokeStyle = "green";
contextMob.beginPath();
contextMob.arc(mBtnJump_Px, mBtnJump_Py, mBtnRadius, 0, Math.PI*2, 0);
contextMob.stroke();
contextMob.beginPath();
contextMob.arc(mBtnSquat_Px, mBtnSquat_Py, mBtnRadius, 0, Math.PI*2, 0);
contextMob.stroke();
contextMob.globalAlpha = 1.0;
}
function mDis(x1,y1,x2,y2){
return Math.sqrt( (x1-x2)*(x1-x2) + (y1-y2)*(y1-y2) );
}
const touches = {};
$('#canvas-mob').on('touchstart', (event)=>{
// socket.emit('shoot');
// movement.forward = true;
// socket.emit('movement', movement);
Array.from(event.changedTouches).forEach((touch) => {
touches[touch.identifier] = {pageX: touch.pageX, pageY: touch.pageY};
var x_ = touch.pageX;
var y_ = touch.pageY;
var d_ = mDis(x_,y_,mBtnUp_Px,mBtnUp_Py);
if( d_ <= mBtnRadius ){
movement.forward = true;
socket.emit('movement', movement);
}
d_ = mDis(x_,y_,mBtnDown_Px,mBtnDown_Py);
if( d_ <= mBtnRadius ){
movement.back = true;
socket.emit('movement', movement);
}
d_ = mDis(x_,y_,mBtnRight_Px,mBtnRight_Py);
if( d_ <= mBtnRadius ){
movement.sideright = true;
socket.emit('movement', movement);
}
d_ = mDis(x_,y_,mBtnLeft_Px,mBtnLeft_Py);
if( d_ <= mBtnRadius ){
movement.sideleft = true;
socket.emit('movement', movement);
}
d_ = mDis(x_,y_,mBtnShoot_Px,mBtnShoot_Py);
if( d_ <= mBtnRadius ){
movement.shoot = true;
socket.emit('movement', movement);
ShotSound1.load();
}
d_ = mDis(x_,y_,mBtnReload_Px,mBtnReload_Py);
if( d_ <= mBtnRadius ){
socket.emit('reload');
}
d_ = mDis(x_,y_,mBtnJump_Px,mBtnJump_Py);
if( d_ <= mBtnRadius ){
socket.emit('jump');
}
d_ = mDis(x_,y_,mBtnSquat_Px,mBtnSquat_Py);
if( d_ <= mBtnRadius ){
socket.emit('squat');
}
});
event.preventDefault();
});
$('#canvas-mob').on('touchmove', (event)=>{
var W_ = canvas2d.width;
var H_ = canvas2d.height;
movement.right = false;
movement.left = false;
Array.from(event.touches).forEach((touch) => {
const startTouch = touches[touch.identifier];
if( (touch.pageX>W_/2) && (startTouch.pageX>W_/2) ){
movement.right |= touch.pageX - startTouch.pageX > 30;
movement.left |= touch.pageX - startTouch.pageX < -30;
movement.angdown |= touch.pageY - startTouch.pageY > 30;
movement.angup |= touch.pageY - startTouch.pageY < -30;
}
});
socket.emit('movement', movement);
event.preventDefault();
});
$('#canvas-mob').on('touchend', (event)=>{
Array.from(event.changedTouches).forEach((touch) => {
delete touches[touch.identifier];
});
if(Object.keys(touches).length === 0){
movement = {};
socket.emit('movement', movement);
}
event.preventDefault();
});
function gameStart(){
const nickname = $("#nickname").val();
socket.emit('game-start', {nickname: nickname});
$("#start-screen").hide();
$("#canvas-vic").hide();
$("#help-screen").hide();
if(mDeviceType=='Mobile'){
mCreateMobileGUI();
$("#canvas-mob").show();
mSoundEffect(ShotSound1,vol_);
}
ElementRequestPointerLock(canvas2d);
}
$("#start-button").on('click', gameStart);
var mFontSize1 = Math.floor(canvas2d.width/100);
var mFontSize2 = mFontSize1*2;
var mDisplayDamage = 0;
const Meshes = [];
socket.on('state', (players, bullets, walls, slopes, rocks, bandages, potions, mStorm) => {
stats.update();
Object.values(Meshes).forEach((mesh) => {mesh.used = false;});
var W_ = canvas2d.width;
var H_ = canvas2d.height;
context.clearRect(-W_/2, -H_/2, W_*2, H_*2);
//Device
context.font = mFontSize1+'px Bold Arial'; //'20px Bold Arial';
context.fillStyle="black";
context.fillText( mDeviceType+':'+screen.availWidth, W_/4, mFontSize1*1.5);
var p_ = 0;
// Players
Object.values(players).forEach((player) => {
let playerMesh = Meshes[player.id];
if(!playerMesh){
//console.log('create player mesh');
playerMesh = new THREE.Group();
playerMesh.castShadow = true;
Meshes[player.id] = playerMesh;
scene.add(playerMesh);
}
playerMesh.used = true;
var pz = player.tall;
if(player.isSquat==1){
pz = player.tall / 2;
}
var px = player.x;// + player.width/2;
var py = player.z + pz/2*0;
var pz = player.y;// + player.height/2;
playerMesh.position.set(px, py, pz);
playerMesh.rotation.y = - player.angle;
var coA_ =-3.1415/180.0*10.0;
var pL_ = player.tall/3;
var pW_ = player.tall/2;
var pW2_ = pL_/2;
var A_ = 3.1415/10;
var pA_ = A_;
var ofz_ = -pW_/2;// * 2;
var pa2_ = player.angle2;
var ofx_ = 200 * Math.sin(-pa2_) * 0;
var sq_ = player.isSquat;
if(player.isMoving==1){
pA_ = A_ * Math.sin( player.timeCount/10.0*3.1415*2.0);
// //console.log('timeCount:'+player.timeCount);
}
if(player.socketId !== socket.id){
ofx_ = 0;
ofz_ = 0;
}
let mesh1 = playerMesh.getObjectByName('bodyL1');
if(mesh1){
playerMesh.remove(mesh1);
mesh1.geometry.dispose();
mesh1 = null;
}
if(player.isBot==0){
mesh = new THREE.Mesh(new THREE.BoxGeometry(pL_, pL_, pW2_), playerMaterialtx1);//playerMaterial1);
}
else{
mesh = new THREE.Mesh(new THREE.BoxGeometry(pL_, pL_, pW2_), botMaterial1);
}
mesh.position.set(ofx_,pL_/2,pW2_/2+ofz_);
mesh.rotation.z = - pA_;
mesh.castShadow = true;
mesh.name = 'bodyL1';
playerMesh.add(mesh);
let mesh2 = playerMesh.getObjectByName('bodyL2');
if(mesh2){
playerMesh.remove(mesh2);
mesh2.geometry.dispose();
mesh2 = null;
}
if(player.isBot==0){
mesh = new THREE.Mesh(new THREE.BoxGeometry(pL_, pL_, pW2_), playerMaterialtx1);//playerMaterial1);
}
else{
mesh = new THREE.Mesh(new THREE.BoxGeometry(pL_, pL_, pW2_), botMaterial1);
}
mesh.position.set(ofx_,pL_/2,-pW2_/2+ofz_);
mesh.rotation.z = pA_;
mesh.castShadow = true;
mesh.name = 'bodyL2';
playerMesh.add(mesh);
if(!playerMesh.getObjectByName('body')){
//console.log('create body mesh');
// mesh = new THREE.Mesh(new THREE.BoxGeometry(player.width, pz, player.height), playerMaterial);
// mesh.castShadow = true;
// mesh.name = 'body';
// playerMesh.add(mesh);
}
let meshB = playerMesh.getObjectByName('body');
if(meshB){
playerMesh.remove(meshB);
meshB.geometry.dispose();
meshB = null;
}
if(player.isBot==0){
//mesh = new THREE.Mesh(new THREE.BoxGeometry(pL_, pL_, pW_), playerMaterial2);
mesh = new THREE.Mesh(new THREE.BoxGeometry(pL_, pL_, pW_), playerMaterialtx1);
}
else{
mesh = new THREE.Mesh(new THREE.BoxGeometry(pL_, pL_, pW_), botMaterial1);
}
mesh.position.set(ofx_,pL_/2+pL_-pL_/2*sq_,0+ofz_);
mesh.rotation.y = -3.1415/4;
mesh.rotation.z = -3.1415/3*sq_;
mesh.castShadow = true;
mesh.name = 'body';
playerMesh.add(mesh);
let meshA1 = playerMesh.getObjectByName('bodyA1');
if(meshA1){
playerMesh.remove(meshA1);
meshA1.geometry.dispose();
meshA1 = null;
}
if(player.isBot==0){
//mesh = new THREE.Mesh(new THREE.BoxGeometry(pL_, pL_/2, pL_/2), playerMaterial2);
mesh = new THREE.Mesh(new THREE.BoxGeometry(pL_, pL_/2, pL_/2), playerMaterialtx1);
}
else{
mesh = new THREE.Mesh(new THREE.BoxGeometry(pL_, pL_/2, pL_/2), botMaterial1);
}
mesh.position.set(ofx_,pL_*2-pL_/4-pL_*sq_,pW_/2+ofz_);
mesh.rotation.y = 0;
mesh.rotation.z = -player.angle2;
mesh.castShadow = true;
mesh.name = 'bodyA1';
playerMesh.add(mesh);
let meshA2 = playerMesh.getObjectByName('bodyA2');
if(meshA2){
playerMesh.remove(meshA2);
meshA2.geometry.dispose();
meshA2 = null;
}
if(player.isBot==0){
//mesh = new THREE.Mesh(new THREE.BoxGeometry(pL_, pL_/2, pL_/2), playerMaterial2);
mesh = new THREE.Mesh(new THREE.BoxGeometry(pL_, pL_/2, pL_/2), playerMaterialtx1);
}
else{
mesh = new THREE.Mesh(new THREE.BoxGeometry(pL_, pL_/2, pL_/2), botMaterial1);
}
mesh.position.set(ofx_+pL_/2,pL_*2-pL_/4-pL_*sq_, -pW_/2+pL_/4+ofz_);
mesh.rotation.y = -3.1415/4;;
mesh.castShadow = true;
mesh.name = 'bodyA2';
playerMesh.add(mesh);
let meshH = playerMesh.getObjectByName('bodyH');
if(meshH){
playerMesh.remove(meshH);
meshH.geometry.dispose();
meshH = null;
}
if(player.isBot==0){
mesh = new THREE.Mesh(new THREE.BoxGeometry(pL_, pL_, pL_), playerMaterial3);
}
else{
mesh = new THREE.Mesh(new THREE.BoxGeometry(pL_, pL_, pL_), playerMaterial3b);
}
mesh.position.set(ofx_+pL_/2*sq_, pL_/2+pL_*2 -pL_*sq_ ,+ofz_);
mesh.rotation.y = 0;
if(player.isEmote == 1){
mesh.rotation.y = 3.1415 - coA_;
}
mesh.castShadow = true;
mesh.name = 'bodyH';
playerMesh.add(mesh);
if(player.isBot==0){
let meshFace = playerMesh.getObjectByName('bodyFace');
if(meshFace){
playerMesh.remove(meshFace);
meshFace.geometry.dispose();
meshFace = null;
}
meshFace = new THREE.Mesh(new THREE.PlaneGeometry(pL_, pL_, 1, 1), faceMaterial1); //playerMaterial3); //
meshFace.position.set(ofx_+pL_/2*sq_+pL_/2, pL_/2+pL_*2 -pL_*sq_ ,+ofz_);
meshFace.rotation.y = 3.1415/2;
if(player.isEmote == 1){
meshFace.position.set(ofx_+pL_/2*sq_+pL_/2*1.01*Math.cos(3.1415+coA_), pL_/2+pL_*2 -pL_*sq_ ,+ofz_+pL_/2*1.01*Math.sin(-coA_));
meshFace.rotation.y = 3.1415/2 - coA_;
}
//meshFace.castShadow = true;
meshFace.name = 'bodyFace';
playerMesh.add(meshFace);
}
let meshG = playerMesh.getObjectByName('bodyG');
if(meshG){
playerMesh.remove(meshG);
meshG.geometry.dispose();
meshG = null;
}
var gL_ = pL_*2;
var gW_ = pL_/2;
if(player.weapon==2){
gW_ = pL_/2*2;
}
else if(player.weapon==3){
gL_ = pL_*4;
}
if(player.weapon<=3){
mesh = new THREE.Mesh(new THREE.BoxGeometry(gL_, pL_/2, gW_), playerMaterialg);
mesh.position.set(ofx_+gL_/2*Math.cos(pa2_) ,pL_*2 -pL_*sq_ +gL_/2*Math.sin(-pa2_), pW_/2+ ofz_);
mesh.rotation.y = 0;
mesh.rotation.z = -pa2_;
mesh.castShadow = true;
mesh.name = 'bodyG';
playerMesh.add(mesh);
}
if((player.weapon==4) && (player.bandages>0) ){
mesh = new THREE.Mesh(new THREE.CylinderGeometry(gW_,gW_,gL_,10,10,false), bandageMaterial);
mesh.position.set(ofx_+gL_/2*Math.cos(pa2_) ,pL_*2 -pL_*sq_ +gL_/2*Math.sin(-pa2_), pW_/2+ ofz_);
mesh.rotation.y = 0;
mesh.rotation.z = -pa2_;
mesh.castShadow = true;
mesh.name = 'bodyG';
playerMesh.add(mesh);
}
else if( (player.weapon==5) && (player.potions>0) ){
mesh = new THREE.Mesh(new THREE.CylinderGeometry(gW_,gW_,gL_,10,10,false), potionMaterial);
mesh.position.set(ofx_+gL_/2*Math.cos(pa2_) ,pL_*2 -pL_*sq_ +gL_/2*Math.sin(-pa2_), pW_/2+ ofz_);
mesh.rotation.y = 0;
mesh.rotation.z = -pa2_;
mesh.castShadow = true;
mesh.name = 'bodyG';
playerMesh.add(mesh);
}
let meshF = playerMesh.getObjectByName('bodyF');
if(meshF){
playerMesh.remove(meshF);
meshF.geometry.dispose();
meshF = null;
}
if(player.isShoot == 1){
mesh = new THREE.Mesh(new THREE.BoxGeometry(2, gW_*3, gW_*3), playerMaterialf);
mesh.position.set(ofx_+gL_*Math.cos(pa2_) ,pL_*2 -pL_*sq_ +gL_*Math.sin(-pa2_), pW_/2+ ofz_);
mesh.rotation.y = 0;
mesh.rotation.z = -pa2_;
mesh.rotation.x = Math.random()* 3.1415 / 2;
mesh.castShadow = true;
mesh.name = 'bodyF';
playerMesh.add(mesh);
}
let meshP = playerMesh.getObjectByName('bodyP');
if(meshP){
playerMesh.remove(meshP);
meshP.geometry.dispose();
meshP = null;
}
if(player.mode==0){
//mesh = new THREE.Mesh(new THREE.PlaneGeometry(pL_*2, pL_*8, 1,1), paraMaterial1);
mesh = new THREE.Mesh(new THREE.BoxGeometry(pL_*2, pL_/4, pL_*8), paraMaterial1);
mesh.position.set(ofx_, pL_*4, ofz_);
//mesh.rotation.x = Math.PI/2;
mesh.castShadow = true;
mesh.name = 'bodyP';
playerMesh.add(mesh);
}
// buildTemp
if(player.socketId === socket.id){
if(player.buildTemp)
{
let wallt = player.buildTemp;
let walltMesh = Meshes[wallt.id];
if(!walltMesh){
console.log('walltMesh');
walltMesh = new THREE.Mesh(new THREE.BoxGeometry(wallt.width, wallt.tall, wallt.height), wallMaterial_t);
var wx = wallt.x + wallt.width/2 -px*0;
var wy = wallt.z + wallt.tall/2 -py*0;
var wz = wallt.y + wallt.height/2 -pz*0;
if(player.buildType==2){
wx = wallt.x + mFieldGridSize/2;
wz = wallt.y + mFieldGridSize/2;
wy = wallt.z - wallt.tall;
}
walltMesh.position.set(wx, wy, wz);
if(player.buildType==2){
walltMesh.rotation.y = wallt.angle;
walltMesh.rotation.z = wallt.angle2;
if((wallt.angle == 3.1415/2)||(wallt.angle == 3.1415/2*3) ){
walltMesh.rotation.z = -wallt.angle2;
}
}
//walltMesh.castShadow = true;
//Meshes.push(walltMesh);
Meshes[wallt.id] = walltMesh;
scene.add(walltMesh);
}
if(player.mode == 2){
walltMesh.used = true;
console.log('walltMesh used');
}
}
}
if(font){
if(!playerMesh.getObjectByName('nickname'))
{
//console.log('create nickname mesh');
if(player.socketId !== socket.id){
let meshN = playerMesh.getObjectByName('nickname');
if(!meshN){
meshN = new THREE.Mesh(
new THREE.TextGeometry(player.nickname,
{font: font, size: 10, height: 1}),
nicknameMaterial,
);
meshN.name = 'nickname';
playerMesh.add(meshN);
}
meshN.position.set(0, player.tall+20, 0);
meshN.rotation.y = -Math.PI/2;
}
}
{
// if(player.socketId !== socket.id)
// {
// let mesh = playerMesh.getObjectByName('health');
// if(mesh && mesh.health !== player.health){
// playerMesh.remove(mesh);
// mesh.geometry.dispose();
// mesh = null;
// }
// if(!mesh){
// //console.log('create health mesh');
// mesh = new THREE.Mesh(
// new THREE.TextGeometry('*'.repeat(player.health),
// {font: font, size: 10, height: 1}),
// textMaterial,
// );
// mesh.name = 'health';
// mesh.health = player.health;
// playerMesh.add(mesh);
// }
// mesh.position.set(0, player.tall, 0);
// mesh.rotation.y = -Math.PI/2;
// }//
}
}
if(player.socketId === socket.id){
// Your player
var d_ = 120;//170;
// Camera
camera.position.set(
player.x - d_ * Math.cos(player.angle +coA_) * Math.cos(pa2_),
player.z + (player.tall -player.tall/2*sq_)*0.8 + d_*Math.sin(pa2_),
player.y - d_ * Math.sin(player.angle+coA_) * Math.cos(pa2_)
);
//camera.rotation.order = "XYZ";
camera.rotation.order = "YXZ";
camera.rotation.y = - player.angle - Math.PI/2;
camera.rotation.x = -player.angle2;
// Write to 2D canvas
var x_,y_,w_,w2_,h_;
// Scope
if( (mWeapon==3) && (camera.zoom != 1) )
{
context.strokeStyle="black";
var r_ = W_/2*0.52;
context.lineWidth = 1;
context.beginPath();
context.arc(W_/2, H_/2, r_, 0, Math.PI*2, false);
context.closePath();
context.stroke();
r_ = W_/2*0.6;
context.lineWidth = 50;
context.beginPath();
context.arc(W_/2, H_/2, r_, 0, Math.PI*2, false);
context.closePath();
context.stroke();
context.lineWidth = 1;
context.beginPath();
context.moveTo(W_/2, H_/2-r_);
context.lineTo(W_/2, H_/2+r_);
context.closePath();
context.stroke();
}
//else{
var s_ = W_/30;
context.strokeStyle="black";
context.lineWidth = 2;
context.beginPath();
context.setLineDash([5,10]);
context.moveTo(W_/2, H_/2-s_);
context.lineTo(W_/2, H_/2+s_);
context.closePath();
context.stroke();
context.beginPath();
context.moveTo(W_/2-s_, H_/2);
context.lineTo(W_/2+s_, H_/2);
context.closePath();
context.stroke();
context.setLineDash([]);
//}
// Player angle
x_ = canvas2d.width / 2;
y_ = mFontSize2;
context.font = mFontSize2 + 'px Bold Arial';
context.fillStyle="black";
context.fillText( Math.floor(player.angle/3.1415*180), x_, y_);
y_ = mFontSize2*2;
context.fillText( Math.floor(-player.angle2/3.1415*180), x_, y_);
// life gauge
context.strokeStyle = 'black';
context.fillStyle='green';
if(player.inStorm==1){
context.fillStyle='red';
}
w_ = canvas2d.width / 4;
w2_ = canvas2d.width / 4 * player.health / player.maxHealth;
h_ = canvas2d.height / 20;
x_ = canvas2d.width / 20;
y_ = canvas2d.height - h_*2;
//console.log('life:'+[x_,y_,w_,w2_,h_]);
context.fillRect(x_,y_,w2_,h_);
context.strokeRect(x_,y_,w_,h_);
// potion gauge
context.fillStyle='skyblue';
w_ = canvas2d.width / 4;
w2_ = canvas2d.width / 4 * player.shield / player.maxShield;
h_ = canvas2d.height / 20;
x_ = canvas2d.width / 20;
y_ = canvas2d.height - h_*3;
context.fillRect(x_,y_,w2_,h_);
context.strokeRect(x_,y_,w_,h_);
//weapons
w_ = canvas2d.width / 5;
w2_ = w_ / 5;
h_ = canvas2d.height / 10;
x_ = canvas2d.width / 4 * 3;
y_ = canvas2d.height - h_*2;
context.lineWidth = 10;
for(var i=0;i<5;i++){
context.strokeStyle = 'white';
context.strokeRect(x_+w2_*i,y_,w2_,h_);
}
context.strokeStyle = 'red';
context.strokeRect(x_+w2_*(player.weapon-1),y_,w2_,h_);
context.font = mFontSize2+ 'px Bold Arial';
context.fillStyle="black";
context.fillText( 'AR', x_+w2_*0+10,y_+h_*0.7);
context.fillText( 'SG', x_+w2_*1+10,y_+h_*0.7);
context.fillText( 'SR', x_+w2_*2+10,y_+h_*0.7);
context.fillText( 'B'+player.bandages, x_+w2_*3+10,y_+h_*0.7);
context.fillText( 'P'+player.potions, x_+w2_*4+10,y_+h_*0.7);
// give damage
if(mDisplayDamage>0){
x_ = W_/2;
y_ = H_/2;
var t_ = mDisplayDamage * 3;
context.font = (mFontSize1*5)+'px Bold Arial';
context.fillStyle="yellow";
context.fillText( ' +'+player.giveDamage*1, x_+t_,y_-t_);
}
}// your player
// Player list
if(1){
var x_,y_,w_,w2_,h_;
w_ = canvas2d.width / 8;
h_ = canvas2d.height / 60;
x_ = canvas2d.width / 50;
y_ = 100 + h_*4*p_;
context.lineWidth = 1;
context.font = mFontSize1+'px Bold Arial';
context.fillStyle="white";
context.fillText(player.nickname, x_, y_-3);
context.strokeStyle = 'black';
context.fillStyle='skyblue';
w2_ = w_ * player.shield / player.maxShield;
context.fillRect(x_,y_,w2_,h_);
context.strokeRect(x_,y_,w_,h_);
context.fillStyle='green';
if(player.inStorm==1){
context.fillStyle='red';
}
y_ += h_;
w2_ = w_ * player.health / player.maxHealth;
context.fillRect(x_,y_,w2_,h_);
context.strokeRect(x_,y_,w_,h_);
}
p_ += 1;
}); //players
// Bullets
Object.values(bullets).forEach((bullet) => {
let mesh = Meshes[bullet.id];
if(!mesh){
mesh = new THREE.Mesh(new THREE.BoxGeometry(bullet.width, bullet.height, bullet.height), bulletMaterial);
mesh.castShadow = true;
Meshes[bullet.id] = mesh;
// Meshes.push(mesh);
scene.add(mesh);
}
mesh.used = true;
var bl = bullet.width/2;
var ba = bullet.angle;
var ba2 = bullet.angle2;
mesh.position.set(bullet.x + bl*Math.cos(-ba2)*Math.cos(ba)*0, bullet.z + bl*Math.sin(-ba2)*0, bullet.y + bl*Math.cos(-ba2)*Math.sin(ba)*0);
mesh.rotation.y = - ba;
mesh.rotation.z = - ba2;
});
// Walls
Object.values(walls).forEach((wall) => {
let mesh = Meshes[wall.id];
if(!mesh){
mesh = new THREE.Mesh(new THREE.BoxGeometry(wall.width, wall.tall, wall.height), wallMaterial);
mesh.castShadow = true;
//Meshes.push(mesh);
Meshes[wall.id] = mesh;
scene.add(mesh);
}
mesh.used = true;
mesh.position.set(wall.x + wall.width/2, wall.z+wall.tall/2, wall.y + wall.height/2);
//mesh.rotation.y = - wall.angle;
});
// Rocks
Object.values(rocks).forEach((rock) => {
let mesh = Meshes[rock.id];
if(!mesh){
mesh = new THREE.Mesh(new THREE.BoxGeometry(rock.width, rock.tall, rock.height), rockMaterial);
//Meshes.push(mesh);
Meshes[rock.id] = mesh;
scene.add(mesh);
}
mesh.used = true;
mesh.position.set(rock.x + rock.width/2, rock.z+rock.tall/2, rock.y + rock.height/2);
//mesh.rotation.y = - wall.angle;
});
// Slopes
Object.values(slopes).forEach((slope) => {
let mesh = Meshes[slope.id];
if(!mesh){
mesh = new THREE.Mesh(new THREE.BoxGeometry(slope.width, slope.tall, slope.height), wallMaterial);
//Meshes.push(mesh);
Meshes[slope.id] = mesh;
scene.add(mesh);
}
mesh.used = true;
mesh.position.set(slope.x + mFieldGridSize/2, slope.z - slope.tall, slope.y + mFieldGridSize/2);
mesh.rotation.y = slope.angle;
mesh.rotation.z = slope.angle2;
if((slope.angle == 3.1415/2)||(slope.angle == 3.1415/2*3) ){
mesh.rotation.z = -slope.angle2;
}
});
// Bandages
Object.values(bandages).forEach((bandage) => {
let mesh = Meshes[bandage.id];
if(!mesh){
mesh = new THREE.Mesh(new THREE.CylinderGeometry(bandage.width,bandage.width,bandage.tall,10,10,false), bandageMaterial);
mesh.position.set(bandage.x + bandage.width/2, bandage.z+bandage.tall/2, bandage.y + bandage.height/2);
mesh.castShadow = true;
Meshes.push(mesh);
Meshes[bandage.id] = mesh;
scene.add(mesh);
}
mesh.used = true;
});
// Potions
Object.values(potions).forEach((potion) => {
let mesh = Meshes[potion.id];
if(!mesh){
mesh = new THREE.Mesh(new THREE.CylinderGeometry(potion.width,potion.width,potion.tall,10,10,false), potionMaterial);
mesh.castShadow = true;
Meshes.push(mesh);
Meshes[potion.id] = mesh;
scene.add(mesh);
}
mesh.used = true;
mesh.position.set(potion.x + potion.width/2, potion.z+potion.tall/2, potion.y + potion.height/2);
});
let meshStorm = Meshes[mStorm.id];
if(!meshStorm){
meshStorm = new THREE.Mesh(new THREE.CylinderGeometry(mStorm.width, mStorm.width, FIELD_WIDTH, 50,50,true), stormMaterial);
Meshes.push(meshStorm);
Meshes[mStorm.id] = meshStorm;
scene.add(meshStorm);
}
meshStorm.used = true;
meshStorm.position.set(mStorm.x, -10, mStorm.y);
var storm_scale = mStorm.width / meshStorm.geometry.parameters.radiusTop;
//console.log('storm:'+mStorm.width+', '+meshStorm.geometry.parameters.radiusTop);
console.log('storm:'+storm_scale);
meshStorm.scale.x = storm_scale;
meshStorm.scale.z = storm_scale;
// Clear unused Meshes
Object.keys(Meshes).forEach((key) => {
const mesh = Meshes[key];
if(!mesh.used){
//console.log('removing mesh', key);
scene.remove(mesh);
mesh.traverse((mesh2) => {
if(mesh2.geometry){
mesh2.geometry.dispose();
}
});
delete Meshes[key];
}
});
});
// Victory
function mDrawVictroy(){
var W_ = canvasVic.width;
var H_ = canvasVic.height;
var x_,y_,w_,w2_,h_;
contextVic.clearRect(0, 0, W_, H_);
var s_ = (mFontSize1*10);
x_ = W_ / 4;
y_ = H_ / 10;
w_ = W_/2;
h_ = s_ * 1.3;
contextVic.fillStyle="blue";
contextVic.fillRect(x_, y_, w_, h_);
x_ = W_ / 2 - s_*2;
y_ += s_;
contextVic.font = s_+'px Bold Arial';
contextVic.fillStyle="white";
contextVic.fillText( '#1 優勝!!', x_, y_);
s_ = (mFontSize1*2);
y_ += s_;
contextVic.font = s_+'px Bold Arial';
contextVic.fillStyle="white";
x_ = W_ / 2 - s_*3;
contextVic.fillText( '夕飯はお寿司だ', x_, y_);
}
function mDrawResults(n_){
var W_ = canvasVic.width;
var H_ = canvasVic.height;
var x_,y_,w_,w2_,h_;
contextVic.clearRect(0, 0, W_, H_);
var s_ = (mFontSize1*10);
x_ = W_ / 2 - s_;
y_ = H_ / 5;
contextVic.font = s_+'px Bold Arial';
contextVic.fillStyle="black";
contextVic.fillText( '#'+n_, x_,y_);
contextVic.font = '40px Bold Arial';
}
socket.on('id', id_ => {
mID = id_;
console.log('mID:'+mID);
});
socket.on('shoot', function(a_) {
var id_ = a_[0];
var wp_ = a_[1];
var vol_ = 0.3;
if(id_==mID){
vol_=1.0;
if(wp_==1){
mSoundEffect(ShotSound1,vol_);
}
else if(wp_==2){
mSoundEffect(ShotSound2,vol_);
}
else if(wp_==3){
mSoundEffect(ShotSound3,vol_);
}
}
else{
if(wp_==1){
mSoundEffect(ShotSound1.cloneNode(),vol_);
}
else if(wp_==2){
mSoundEffect(ShotSound2.cloneNode(),vol_);
}
else if(wp_==3){
mSoundEffect(ShotSound3.cloneNode(),vol_);
}
}
});
socket.on('empty', () => {
mSoundEffect(EmptySound,1.0);
});
socket.on('shootFromOthers', function(a_)
{
//console.log('shootFromOthers:'+a_[0]);
if(a_[0]==1){
mSoundEffect(ShotSound1,a_[1]);
}
else if(a_[0]==2){
mSoundEffect(ShotSound2,a_[1]);
}
else if(a_[0]==3){
mSoundEffect(ShotSound3,a_[1]);
}
});
socket.on('shootFromBot', vol_ => {
mSoundEffect(ShotSoundOthers1,vol_);
});
socket.on('displayGiveDamage', v_ => {
mDisplayDamage = v_;
});
socket.on('givedamage', vol_ => {
mSoundEffect(GiveDamageSound,vol_);
});
socket.on('recievedamage', vol_ => {
mSoundEffect(RecieveDamageSound,vol_);
});
socket.on('walldamage', vol_ => {
mSoundEffect(WallDamageSound,vol_);
});
socket.on('deadothers', () => {
mSoundEffect(RemoveSound,1.0);
});
socket.on('getItem', () => {
mSoundEffect(GetItemSound,1.0);
});
socket.on('usedBandage', () => {
mSoundEffect(UsedBandageSound,1.0);
});
socket.on('cannotUseBandage', () => {
mSoundEffect(CannotUseBandageSound,1.0);
});
socket.on('usedPotion', () => {
mSoundEffect(UsedBandageSound,1.0);
});
socket.on('cannotUsePotion', () => {
mSoundEffect(CannotUseBandageSound,1.0);
});
socket.on('victory', () => {
DocumentExitPointerLock(canvas2d);
mDrawVictroy();
//$("#help-screen").show();
$("#canvas-vic").show();
//$("#start-screen").show();
//mSoundEffect(ChurchSound,1.0);
ChurchSound.play();
});
socket.on('dead', n_ => {
if(n_>1){
mDrawResults(n_);
$("#canvas-vic").show();
}
$("#help-screen").show();
$("#start-screen").show();
});[メールボックス]
- MOBI-PAGE -