Skip to content

Commit

Permalink
Add AutoGun and SubObjects
Browse files Browse the repository at this point in the history
  • Loading branch information
ybs1164 committed Jul 6, 2020
1 parent ef8f700 commit cead002
Show file tree
Hide file tree
Showing 13 changed files with 385 additions and 133 deletions.
1 change: 1 addition & 0 deletions config/setting.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
"maxPlayer": 50,
"maxShape": 1000,
"chatting": false,
"getExp": 1,
"mapSize": {
"x": 1500,
"y": 1500
Expand Down
8 changes: 4 additions & 4 deletions dist/js/core.js

Large diffs are not rendered by default.

4 changes: 4 additions & 0 deletions lib/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,10 @@ func Ccw(p1, p2, p3 Pos) int {
}
}

func DirDis(d1, d2 float64) float64 {
return math.Pi - math.Abs(math.Abs(d1-d2)-math.Pi)
}

func Now() int64 {
return time.Now().UnixNano() / 1000000
}
24 changes: 16 additions & 8 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,16 +80,16 @@ func moveloop(ticker time.Ticker) { // manages the motion of all objects.

t := time.Now()

obj.AddShape()
obj.AddShape() // ๋„ํ˜• ์Šคํฐ

for _, u := range obj.Users {
for _, u := range obj.Users { // ์œ ์ € ์„ธํŒ…
u.PlayerSet()
u.CameraSet()
}

obj.Qt.Clear()
obj.Qt.Clear() // ์ฟผ๋“œํŠธ๋ฆฌ ์ดˆ๊ธฐํ™”

for _, o := range obj.Objects {
for _, o := range obj.Objects { // ์ฟผ๋“œํŠธ๋ฆฌ ์ถฉ๋Œ์ฒ˜๋ฆฌ
if !o.IsDead {
obj.Qt.Insert(o)

Expand All @@ -98,27 +98,33 @@ func moveloop(ticker time.Ticker) { // manages the motion of all objects.
Y: o.Y - o.R,
W: o.R * 2,
H: o.R * 2,
}) {
}) { // ๋˜‘๊ฐ™์€ ์˜ค๋ธŒ์ ํŠธ๊ฐ€ ์•„๋‹ˆ๊ณ , ์ฃฝ์ง€ ์•Š์•˜์œผ๋ฉฐ, ์ด๋ฏธ ์ถฉ๋Œ๊ฐ์ง€๊ฐ€ ์ฒ˜๋ฆฌ๋˜์ง€ ์•Š์•˜๊ณ , ์ฃผ์ธ์ด ๊ฐ™์„ ๋•Œ ๋ฐ˜๋™๊ฐ’์„ ์ฃผ๋Š”๊ฐ€, ํƒ€ ์˜ค๋ธŒ์ ํŠธ์˜ ์ฃผ์ธ์ด ์ž์‹ ์˜ ์˜ค๋ธŒ์ ํŠธ๊ฐ€ ์•„๋‹Œ๊ฐ€
if o != obj2 && !obj2.IsDead && !(o.IsCollision || obj2.IsCollision) && (obj2.Owner != o.Owner || obj2.IsOwnCol && o.IsOwnCol) && o != obj2.Owner && obj2 != o.Owner {
if (o.X-obj2.X)*(o.X-obj2.X)+(o.Y-obj2.Y)*(o.Y-obj2.Y) < (o.R+obj2.R)*(o.R+obj2.R) {
o.Collision(o, obj2)
obj2.Collision(obj2, o)
if o.Collision != nil {
o.Collision(o, obj2)
}
if obj2.Collision != nil {
obj2.Collision(obj2, o)
}
}
}
}
}
}

for i, o := range obj.Objects {
o.Index = i
if o.Tick != nil {
o.Tick(o)
}

o.ObjectTick(i)
o.ObjectTick()
}

for i := 0; i < len(obj.Objects); i++ {
var o *obj.Object = obj.Objects[i]
o.Index = i
if o.IsDead {
if o.DeadTime == -1 {
o.DeadTime = 300
Expand All @@ -130,6 +136,8 @@ func moveloop(ticker time.Ticker) { // manages the motion of all objects.
obj.Objects = append(obj.Objects[:i], obj.Objects[i+1:]...)
i--
} else {
o.Opacity = math.Max(o.Opacity-0.1, 0)
o.R += o.R * 0.02
o.DeadTime = math.Max(o.DeadTime-1000./60., 0.)
}
}
Expand Down
12 changes: 9 additions & 3 deletions obj/gun.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,9 +103,15 @@ func (obj *Object) Shot(objIndex int) {
obj.Dy -= math.Sin(obj.Dir+g.Dir) * 0.1 * g.GunBound
g.DelayTime = (0.6 - 0.04*float64(GunOwner.Stats[6])) / g.Reload * 1000
g.IsShot = true
Objects[objIndex] = &bullet
objIndex = len(Objects)
Objects = append(Objects, obj)
if objIndex == -1 { // as subObject
Objects[GunOwner.Index] = &bullet
GunOwner.Index = len(Objects)
Objects = append(Objects, GunOwner)
} else {
Objects[objIndex] = &bullet
objIndex = len(Objects)
Objects = append(Objects, obj)
}
}
} else {
g.ShotTime = 0
Expand Down
40 changes: 31 additions & 9 deletions obj/object.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ type Object struct {
Type string `json:"type"`
Team string `json:"team"`
Name string `json:"name"`
Index int

X float64 `json:"x"`
Y float64 `json:"y"`
Expand Down Expand Up @@ -58,16 +59,17 @@ type Object struct {
IsShowName bool `json:"isShowName"`
IsBack bool

HitObject *Object
Target *Object
Tick func(*Object)
Collision func(*Object, *Object)
KillEvent func(*Object, *Object)
DeadEvent func(*Object, *Object)
SubObjects []*Object
HitObject *Object
Target *Object
Tick func(*Object)
Collision func(*Object, *Object)
KillEvent func(*Object, *Object)
DeadEvent func(*Object, *Object)
}

//
func (obj *Object) ObjectTick(objIndex int) {
func (obj *Object) ObjectTick() {
obj.X += obj.Dx //* lib.GameSetting.GameSpeed // ์ขŒํ‘œ๊ฐ’์— ์†๋„ ์ ์šฉ
obj.Y += obj.Dy //* lib.GameSetting.GameSpeed

Expand All @@ -89,15 +91,27 @@ func (obj *Object) ObjectTick(objIndex int) {
}
}

if obj.H <= 0 {
if obj.H <= 0 && obj.Mh != 0 {
obj.IsDead = true
obj.H = 0
}
for _, o := range obj.SubObjects {
if o == nil {
continue
}
o.Index = -1
if o.Tick != nil {
o.Tick(o)
}

o.ObjectTick()
}

if obj.IsDead {
return
}

obj.Shot(objIndex)
obj.Shot(obj.Index)

if obj.Controller != nil && lib.Now()-int64(30000.*lib.GameSetting.GameSpeed) > obj.HitTime {
obj.H += obj.Mh / 60 / 10 * lib.GameSetting.GameSpeed
Expand Down Expand Up @@ -182,6 +196,14 @@ func (o Object) ObjBinaryData() []byte {
data = append(data, byte(0))
}
}
data = append(data, byte(len(o.SubObjects)))
for _, obj := range o.SubObjects {
if obj == nil {
data = append(data, make([]byte, 4)...)
} else {
data = append(data, obj.ObjBinaryData()...)
}
}
if o.HitTime+100-lib.Now() > 0 {
data = append(data, byte(uint8(o.HitTime+100-lib.Now())))
} else {
Expand Down
5 changes: 4 additions & 1 deletion obj/player.go
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ func Event(p *Player, message []byte) {
value /= 2
if value%2 == 1 {
if obj := u.ControlObject; obj != nil {
//obj.H = 0
obj.H = 0
}
}
}
Expand Down Expand Up @@ -213,6 +213,9 @@ func Event(p *Player, message []byte) {
"Predator",
"Trapper",
"TriTrapper",
"Auto5",
"Auto3",
"AutoTrapper",
"Battleship",
"Spike",
"Skimmer",
Expand Down
2 changes: 1 addition & 1 deletion obj/quadtree.go
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ func NearObject(o *Object, ran, dir, rdir float64) *Object {
var nearObj *Object = nil

for _, obj := range objList {
if math.Abs(dir-math.Atan2(o.Y-obj.Y, o.X-obj.X)) < rdir && lib.Distance(o.X, o.Y, obj.X, obj.Y) < ran && o != obj && o.Owner != obj && o.Team != obj.Team && !obj.IsDead && obj.IsTargeted {
if lib.DirDis(dir, math.Atan2(obj.Y-o.Y, obj.X-o.X)) < rdir && lib.Distance(o.X, o.Y, obj.X, obj.Y) < ran && o != obj && o.Owner != obj && o.Team != obj.Team && !obj.IsDead && obj.IsTargeted {
if nearObj == nil || lib.Distance(o.X, o.Y, obj.X, obj.Y) < lib.Distance(o.X, o.Y, nearObj.X, nearObj.Y) {
nearObj = obj
}
Expand Down
125 changes: 122 additions & 3 deletions obj/tank.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ import (
)

func TankTick(obj *Object) {
if obj.IsDead {
return
}
obj.R = 12.9 * math.Pow(1.01, float64(obj.Level)-1)
obj.Damage = 20 + float64(obj.Stats[2])*4
obj.Speed = (0.07 + 0.007*float64(obj.Stats[7])) * math.Pow(0.985, float64(obj.Level)-1)
Expand Down Expand Up @@ -265,7 +268,7 @@ func NewTank(t string) *Object {
}, nil, nil, nil, nil)}
case "TriTrapper":
obj.Sight = 1.11
var dir float64 = -math.Pi / 3 * 2
var dir float64 = -math.Pi / 3. * 2.
obj.Guns = []Gun{}
for i := 0; i < 3; i++ {
obj.Guns = append(obj.Guns, NewGun(obj, map[string]interface{}{
Expand All @@ -277,8 +280,53 @@ func NewTank(t string) *Object {
"lifetime": 10,
"sy": 1.2,
}, nil, nil, nil, nil))
dir += math.Pi / 3 * 2
dir += math.Pi / 3. * 2.
}
case "Auto5":
obj.Sight = 1.11
for i := 0.; i < 5.; i++ {
obj.SubObjects = append(obj.SubObjects, AutoGun(obj, map[string]interface{}{
"speed": 1.2,
"damage": 0.4,
"radius": 1.2,
"bound": 0.333,
}, math.Pi*2./5.*i, math.Pi/2.))
}
case "Auto3":
obj.Sight = 1.11
for i := 0.; i < 3.; i++ {
obj.SubObjects = append(obj.SubObjects, AutoGun(obj, map[string]interface{}{
"speed": 1.2,
"damage": 0.4,
"radius": 1.2,
"bound": 0.333,
}, math.Pi*2./3.*i, math.Pi/2.))
}
case "Auto1":
obj.Sight = 1.11
obj.SubObjects = append(obj.SubObjects, AutoGun(obj, map[string]interface{}{
"speed": 1.2,
"damage": 0.4,
"radius": 1.2,
"bound": 0.333,
}, 0, math.Pi/2.))
case "AutoTrapper":
obj.Sight = 1.11
obj.Guns = []Gun{NewGun(obj, map[string]interface{}{
"type": "Trap",
"health": 2,
"reload": 0.667,
"radius": 0.8,
"lifetime": 24,
"sy": 1.2,
}, nil, nil, nil, nil)}
obj.SubObjects = append(obj.SubObjects, nil)
obj.SubObjects = append(obj.SubObjects, AutoGun(obj, map[string]interface{}{
"speed": 1.2,
"damage": 0.3,
"radius": 1.2,
"bound": 0.125,
}, 0, 0))
case "Battleship":
obj.Sight = 1.11
obj.Stats = [8]int{0, 0, 0, 7, 7, 7, 7, 5}
Expand Down Expand Up @@ -403,8 +451,79 @@ func NewTank(t string) *Object {
return obj
}

func AutoGun() {
func AutoGun(owner *Object, gunValue map[string]interface{}, dir, rdir float64) *Object {
var o *Object = NewObject(map[string]interface{}{
"team": "nil",
"type": "AutoGun",
"speed": rdir,
"sight": dir,
"mh": 0,
"r": 1,
"damage": 0,
"isBorder": false,
"isOwnCol": false,
}, func(obj *Object) {
obj.R = obj.Owner.R * 0.5
obj.Owner.Dx += obj.Dx
obj.Owner.Dy += obj.Dy
obj.Dx = 0
obj.Dy = 0

if obj.Owner.H == 0 || obj.Owner.IsDead == true {
obj.IsDead = true
}

if obj.Speed == 0. {
obj.X = obj.Owner.X
obj.Y = obj.Owner.Y
obj.Dir += 0.005

var target *Object = NearObject(obj, 400, 0, math.Pi)

if obj.Target == nil {
obj.Target = target
} 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 {
obj.Target = nil
}
}

} else {
obj.X = obj.Owner.X + math.Cos(obj.Sight)*obj.Owner.R*0.8
obj.Y = obj.Owner.Y + math.Sin(obj.Sight)*obj.Owner.R*0.8
if obj.Target == nil {
obj.Dir = obj.Sight
}
obj.Sight = obj.Sight + 0.005
if obj.Sight > math.Pi*2 {
obj.Sight -= math.Pi * 2
}

var target *Object = NearObject(obj, 400, obj.Sight, obj.Speed)

if obj.Target == nil {
obj.Target = target
} 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 {
obj.Target = nil
}
}
}

if obj.Target != nil {
obj.Guns[0].AutoShot = true
obj.Dir = math.Atan2(obj.Target.Y-obj.Y, obj.Target.X-obj.X)
} else {
obj.Guns[0].AutoShot = false
}
}, nil, DefaultKillEvent, nil)
o.Owner = owner
o.Guns = []Gun{NewGun(o, gunValue, nil, nil, nil, nil)}
return o
}

func Invisible(o *Object, t float64) {
Expand Down
3 changes: 1 addition & 2 deletions src/js/data/gun.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { colorList } from './console';
import { drawC } from '../lib/draw';

export const Gun = function (paths, dir, color, isMoveDir, isFront, isStatic) {
export const Gun = function (paths, dir, color, isMoveDir, isStatic) {
'use strict';

/*
Expand All @@ -14,7 +14,6 @@ export const Gun = function (paths, dir, color, isMoveDir, isFront, isStatic) {
this.color = color;
if (this.color == undefined) this.color = 1; // default gun color
this.isMoveDir = isMoveDir || 0; // is Cannot Control Gun? (and Turn like bolt?) (using Smasher or Spike)
this.isFront = isFront || false; // ์˜ค๋ธŒ์ ํŠธ์˜ ๋ชธ์ฒด๋ณด๋‹ค ๋ ˆ์ด์–ด๊ฐ€ ์œ„์ชฝ์— ์œ„์น˜ํ–ˆ๋Š”๊ฐ€?
this.isStatic = isStatic || false; // ์ด๊ตฌ๊ฐ€ ๊ฐ€๋กœ๋กœ ๋Š˜์–ด๋‚˜์ง€ ์•Š๋Š”๊ฐ€? (using Skimmer or Rocketeer 's Gun)
this.shotTime = []; // using gun's animation
this.back = 0;
Expand Down
Loading

0 comments on commit cead002

Please sign in to comment.