Skip to content

Commit

Permalink
Error Fix
Browse files Browse the repository at this point in the history
  • Loading branch information
ybs1164 committed Aug 26, 2020
1 parent f25cc55 commit 02b967c
Show file tree
Hide file tree
Showing 10 changed files with 94 additions and 53 deletions.
4 changes: 2 additions & 2 deletions dist/js/core.js

Large diffs are not rendered by default.

14 changes: 14 additions & 0 deletions lib/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package lib
import (
"math"
"math/rand"
"reflect"
"time"
)

Expand Down Expand Up @@ -78,3 +79,16 @@ func DirDis(d1, d2 float64) float64 {
func Now() int64 {
return time.Now().UnixNano() / 1000000
}

func Contains(s interface{}, e interface{}) bool {
arrV := reflect.ValueOf(s)

if arrV.Kind() == reflect.Slice {
for i := 0; i < arrV.Len(); i++ {
if arrV.Index(i).Interface() == e {
return true
}
}
}
return false
}
12 changes: 6 additions & 6 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ func moveloop(ticker time.Ticker) { // manages the motion of all objects.
W: o.R * 2,
H: o.R * 2,
}) {
if o != e && !e.IsDead && !(o.IsCollision || e.IsCollision) && (e.Owner != o.Owner || e.IsOwnCol && o.IsOwnCol) && o != e.Owner && e != o.Owner && o != e.HitObject {
if o != e && !o.IsDead && !e.IsDead && !e.IsCollision && (e.Owner != o.Owner || e.IsOwnCol && o.IsOwnCol) && o != e.Owner && e != o.Owner && !lib.Contains(e.HitObjects, o) {
if (o.X-e.X)*(o.X-e.X)+(o.Y-e.Y)*(o.Y-e.Y) < (o.R+e.R)*(o.R+e.R) {
if o.Collision != nil {
o.Collision(o, e)
Expand All @@ -129,15 +129,15 @@ func moveloop(ticker time.Ticker) { // manages the motion of all objects.
for i := 0; i < len(obj.Objects); i++ {
var o *obj.Object = obj.Objects[i]
o.Index = i
if len(o.HitObjects) > 0 {
o.LastHitObject = o.HitObjects[0]
}
if o.IsDead {
if o.Controller != nil && o.HitObject != nil {
o.Controller.KillObject = o.HitObject
}
if o.DeadTime == -1 {
o.DeadTime = 300
} else if o.DeadTime <= 0 {
if o.DeadEvent != nil {
o.DeadEvent(o, o.HitObject)
o.DeadEvent(o, o.LastHitObject)
}
obj.ObjIDList = append(obj.ObjIDList, o.ID)
obj.Objects = append(obj.Objects[:o.Index], obj.Objects[o.Index+1:]...)
Expand All @@ -148,7 +148,7 @@ func moveloop(ticker time.Ticker) { // manages the motion of all objects.
o.DeadTime = math.Max(o.DeadTime-1000./60., 0.)
}
}
o.HitObject = nil
o.HitObjects = nil
}

obj.ObjMutex.Unlock()
Expand Down
18 changes: 12 additions & 6 deletions obj/bullet.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,16 +34,22 @@ func DefaultTrapTick(obj *Object) {
}

func DefaultBulletTick(obj *Object) {
if obj.IsDead {
if !obj.IsOwnCol {
obj.DeadTime = -1
obj.IsOwnCol = true
}
return
}

obj.IsOwnCol = false
obj.Dx += math.Cos(obj.Dir) * obj.Speed
obj.Dy += math.Sin(obj.Dir) * obj.Speed

if !obj.IsDead {
obj.DeadTime -= 1000. / 60.
if obj.DeadTime <= 0 {
obj.DeadTime = -1
obj.H = 0
}
obj.DeadTime -= 1000. / 60.
if obj.DeadTime <= 0 {
obj.DeadTime = -1
obj.H = 0
}
}

Expand Down
20 changes: 12 additions & 8 deletions obj/object.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,13 +61,15 @@ type Object struct {
IsBack bool // Should drone object go back to Owner object?

// SubObjects do not enable HitBox and Health.
SubObjects []*Object
HitObject *Object
Target *Object
Tick func(*Object)
Collision func(*Object, *Object)
KillEvent func(*Object, *Object)
DeadEvent func(*Object, *Object)
SubObjects []*Object
HitObjects []*Object
LastHitObject *Object
Target *Object

Tick func(*Object)
Collision func(*Object, *Object)
KillEvent func(*Object, *Object)
DeadEvent func(*Object, *Object)
}

//
Expand Down Expand Up @@ -96,6 +98,7 @@ func (obj *Object) ObjectTick() {
if obj.H <= 0 && obj.Mh != 0 {
obj.IsDead = true
obj.H = 0
obj.IsCollision = true
}
for _, o := range obj.SubObjects {
if o == nil {
Expand Down Expand Up @@ -149,12 +152,13 @@ func DefaultCollision(a *Object, b *Object) {

if a.H <= 0 {
a.H = 0
a.IsDead = true
if b.KillEvent != nil {
b.KillEvent(b, a)
}
}

a.HitObject = b
a.HitObjects = append(a.HitObjects, b)
a.IsCollision = true
}

Expand Down
5 changes: 3 additions & 2 deletions obj/player.go
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ func Event(p *Player, message []byte) {
}
log.Println(name)
var list []string = []string{
/*"Basic",
"Basic",
"Twin",
"Triplet",
"TripleShot",
Expand Down Expand Up @@ -226,12 +226,13 @@ func Event(p *Player, message []byte) {
"Deception",
"Dropper",
"Follower",
"Lifesteal",*/
"Lifesteal",
"Shielder",
"Cure",
"Mechanic",
}
t := p.StartTime % int64(len(list))
p.StartTime++
var s string = list[t]

p.ControlObject = NewTank(s)
Expand Down
2 changes: 2 additions & 0 deletions obj/shape.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ func AddShape() {
"type": "Square",
"name": "Square",
"team": "shape",
"mh": 10,
"h": 10,
"x": lib.RandomRange(-lib.GameSetting.MapSize.X, lib.GameSetting.MapSize.X),
"y": lib.RandomRange(-lib.GameSetting.MapSize.Y, lib.GameSetting.MapSize.Y),
"dir": lib.RandomRange(-math.Pi, math.Pi),
Expand Down
23 changes: 14 additions & 9 deletions obj/tank.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ func TankTick(obj *Object) {
obj.H *= obj.Mh / im
}

if obj.Controller != nil {
if obj.Controller != nil && obj.H > 0 {
obj.H += obj.Mh / 60 / 30 * (0.03 + 0.12*float64(obj.Stats[0]))

for i := 0; i < 8; i++ {
Expand Down Expand Up @@ -659,6 +659,10 @@ func NewTank(t string) *Object {
"isborder": true,
"limit": 8,
}, func(o *Object) {
if o.Owner.DeadTime == 0 {
o.H = 0
return
}
o.Dir += 0.05
if o.IsBack {
if o.Controller != nil && o.Controller.Mr {
Expand Down Expand Up @@ -702,6 +706,7 @@ func NewTank(t string) *Object {
o.X = o.Owner.X
o.Y = o.Owner.Y
o.Dir = o.Owner.Dir

if o.Owner.H == 0 || o.Owner.IsDead == true {
o.IsDead = true
}
Expand Down Expand Up @@ -748,7 +753,7 @@ func AutoGun(owner *Object, gunValue map[string]interface{}, dir, rdir float64)
} else {
if (obj.Target.IsDead || obj.Owner == obj.Target || obj.Target.Team == obj.Team || !obj.Target.IsTargeted) && lib.Distance(obj.Target.X, obj.Target.Y, obj.X, obj.Y) > 500 {
obj.Target = target
} else {
} else if obj.Target != nil && lib.Distance(obj.Target.X, obj.Target.Y, obj.X, obj.Y) > 550 {
obj.Target = nil
}
}
Expand Down Expand Up @@ -784,7 +789,7 @@ func AutoGun(owner *Object, gunValue map[string]interface{}, dir, rdir float64)
} else {
if (obj.Target.IsDead || obj.Owner == obj.Target || obj.Target.Team == obj.Team || !obj.Target.IsTargeted) && lib.Distance(obj.Target.X, obj.Target.Y, obj.X, obj.Y) > 500 {
obj.Target = target
} else {
} else if obj.Target != nil && lib.Distance(obj.Target.X, obj.Target.Y, obj.X, obj.Y) > 550 {
obj.Target = nil
}
}
Expand Down Expand Up @@ -813,6 +818,7 @@ func DefaultShieldTick(o *Object) {
o.Damage = o.Owner.Damage
o.Mh = o.Owner.Mh * (0.3 + 0.1*float64(o.Owner.Stats[4]))
o.Team = o.Owner.Team
o.Dir = o.Owner.Dir
o.SetController(o.Owner.Controller)

if o.Owner.H == 0 || o.Owner.IsDead == true {
Expand All @@ -838,7 +844,6 @@ func DefaultShieldTick(o *Object) {
return
}

o.Dir = o.Owner.Dir
o.R = o.Owner.R + (float64(o.Level) * 5.)
o.Opacity = 0.5

Expand All @@ -848,23 +853,23 @@ func DefaultShieldTick(o *Object) {
W: o.R * 2,
H: o.R * 2,
}) {
if o != e && !e.IsDead && !(o.IsCollision || e.IsCollision) && (e.Owner != o.Owner || e.IsOwnCol && o.IsOwnCol) && o != e.Owner && e != o.Owner && o != e.HitObject {
if o != e && !o.IsDead && !e.IsDead && !e.IsCollision && (e.Owner != o.Owner || e.IsOwnCol && o.IsOwnCol) && o != e.Owner && e != o.Owner && !lib.Contains(e.HitObjects, o) {
if (o.X-e.X)*(o.X-e.X)+(o.Y-e.Y)*(o.Y-e.Y) < (o.R+e.R)*(o.R+e.R) {
if o.Collision != nil {
o.Collision(o, e)
}
if e.Collision != nil {
e.Collision(e, o.Owner)
e.Collision(e, o)
}
}
}
}
o.Owner.HitObject = o.HitObject
o.HitObject = nil
o.Owner.HitObjects = append(o.Owner.HitObjects, o.HitObjects...)
o.HitObjects = nil
}

func Invisible(o *Object, t float64) {
if o.IsShot || o.Controller != nil && o.Controller.IsMove {
if !o.IsDead && (o.IsShot || o.Controller != nil) && o.Controller.IsMove {
o.Opacity = math.Min(o.Opacity+0.1, 1)
} else {
o.Opacity = math.Max(o.Opacity-1./60./t, 0)
Expand Down
4 changes: 2 additions & 2 deletions src/js/data/gun.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,9 @@ export const Gun = function (paths, dir, color, isMoveDir, isStatic) {
const {z, ddir, b} = this.DrawSet(camera, r);

this.paths.forEach((p) => {
const x = Math.floor(Math.cos(dir - Math.PI/2 + ddir) * p[0] * z * ((this.isStatic)?1:r)
const x = /*Math.floor*/(Math.cos(dir - Math.PI/2 + ddir) * p[0] * z * ((this.isStatic)?1:r)
+ Math.cos(dir + ddir) * (p[1] * z * r + b) + pos.x);
const y = Math.floor(Math.sin(dir - Math.PI/2 + ddir) * p[0] * z * ((this.isStatic)?1:r)
const y = /*Math.floor*/(Math.sin(dir - Math.PI/2 + ddir) * p[0] * z * ((this.isStatic)?1:r)
+ Math.sin(dir + ddir) * (p[1] * z * r + b) + pos.y);

if (x < 0) {
Expand Down
45 changes: 27 additions & 18 deletions src/js/data/object.js
Original file line number Diff line number Diff line change
Expand Up @@ -162,12 +162,12 @@ export const Obj = function(id) {
objs.forEach((o) => {
if (o != null) {
let {px, py, w, h} = o.SetCanvasSize(camera);
let ax = -(x + camera.x - o.x) * z + pos.x + 2 * camera.z + 2;
let ay = -(y + camera.y - o.y) * z + pos.y + 2 * camera.z + 2;
let x1 = Math.floor(ax - px);
let x2 = Math.floor(ax - px + w);
let y1 = Math.floor(ay - py);
let y2 = Math.floor(ay - py + h);
let ax = -(x + camera.x - o.x) * z + pos.x + 2 * z + 2;
let ay = -(y + camera.y - o.y) * z + pos.y + 2 * z + 2;
let x1 = /*Math.floor*/(ax - px);
let x2 = /*Math.floor*/(ax - px + w);
let y1 = /*Math.floor*/(ay - py);
let y2 = /*Math.floor*/(ay - py + h);

if (x1 < 0) {
size.x -= x1;
Expand Down Expand Up @@ -202,8 +202,8 @@ export const Obj = function(id) {
});
};
setSubobjCanvas(this.subobjs);
this.cv.width = size.x + 4 * camera.z + 4;
this.cv.height = size.y + 4 * camera.z + 4;
this.cv.width = Math.floor(size.x + 4 * camera.z) + 4;
this.cv.height = Math.floor(size.y + 4 * camera.z) + 4;
pos.x += 2 * camera.z + 2;
pos.y += 2 * camera.z + 2;
this.ctx.lineWidth = 2 * camera.z;
Expand All @@ -220,30 +220,39 @@ export const Obj = function(id) {
this.Draw = function (ctx, camera) {
if ((this.guns.length > 0 || this.subobjs.length > 0) && this.opacity < 1){
var {ctxx, px, py} = this.SetCanvasSize(camera);

let {x, y, z, o} = this.DrawSet(camera);
let lx = (x * z - px) - Math.floor(x * z - px) - x;
let ly = (y * z - py) - Math.floor(y * z - py) - y;

{
let {x, y, z, t, c, r, dir} = this.DrawSet(camera);
let drawO = function (obj) {
let {x, y, z, t, c, r, dir} = obj.DrawSet(camera);
let i = 0;
for (; i < this.subobjs.length; i++) {
if (this.subobjs[i] === null) {
for (; i < obj.subobjs.length; i++) {
if (obj.subobjs[i] === null) {
break;
}
this.subobjs[i].Draw(ctx, camera);
if (obj.subobjs[i].opacity < 1) {
obj.subobjs[i].Draw(ctx,camera);
} else {
drawO(obj.subobjs[i]);
}
}
this.guns.forEach((g) => {
g.Draw(ctxx, camera, px / z + lx + x, py / z + ly + y, r, c, dir, this.hitTime);
obj.guns.forEach((g) => {
g.Draw(ctxx, camera, px / z + lx + x, py / z + ly + y, r, c, dir, obj.hitTime);
});
if (c) {
drawObj(ctxx, px / z + lx + x, py / z + ly + y, z, r, dir, t, 1, c);
}
for (i++; i < this.subobjs.length; i++) {
this.subobjs[i].Draw(ctx, camera);
for (i++; i < obj.subobjs.length; i++) {
if (obj.subobjs[i].opacity < 1) {
obj.subobjs[i].Draw(ctx,camera);
} else {
drawO(obj.subobjs[i]);
}
}
}
drawO(this);

ctx.save();
ctx.globalAlpha = o;
Expand Down

0 comments on commit 02b967c

Please sign in to comment.