Skip to content
GitLab
Menu
Projects
Groups
Snippets
/
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Menu
Open sidebar
Jochen Bauer
spn-website
Commits
42e97f56
Commit
42e97f56
authored
May 04, 2018
by
Hubert Denkmair
Browse files
use ObjectPool for food items
parent
d0554396
Changes
3
Hide whitespace changes
Inline
Side-by-side
visualization/static/visualization/FoodSprite.js
View file @
42e97f56
function
FoodSprite
(
texture
,
decay_rate
,
food_id
,
pos_x
,
pos_y
,
value
)
function
FoodSprite
(
texture
)
{
PIXI
.
Sprite
.
call
(
this
,
texture
);
this
.
food_value
=
value
;
this
.
item_id
=
food_id
;
this
.
decay_rate
=
decay_rate
;
this
.
textureRadius
=
texture
.
width
/
2
;
this
.
food_value
=
0
;
this
.
decay_rate
=
0
;
this
.
item_id
=
0
;
this
.
anchor
.
set
(
0.5
);
this
.
x
=
pos_x
;
this
.
y
=
pos_y
;
this
.
textureRadius
=
32
;
this
.
tint
=
this
.
GetRandomTint
();
this
.
UpdateSize
();
}
FoodSprite
.
prototype
=
Object
.
create
(
PIXI
.
Sprite
.
prototype
);
FoodSprite
.
prototype
.
SetData
=
function
(
decay_rate
,
food_id
,
pos_x
,
pos_y
,
value
)
{
this
.
decay_rate
=
decay_rate
;
this
.
food_value
=
value
;
this
.
item_id
=
food_id
;
this
.
x
=
pos_x
;
this
.
y
=
pos_y
;
this
.
UpdateSize
();
};
FoodSprite
.
prototype
.
UpdateSize
=
function
()
{
let
size
=
Math
.
sqrt
(
this
.
food_value
)
/
this
.
textureRadius
;
...
...
visualization/static/visualization/GameVisualization.js
View file @
42e97f56
...
...
@@ -37,6 +37,10 @@ function GameVisualization(assets, snakeMoveStrategy, container)
this
.
segmentPool
=
new
ObjectPool
(
function
()
{
return
new
SnakeSegment
(
this
.
txBody
);
},
this
,
10000
);
this
.
foodItemPool
=
new
ObjectPool
(
function
()
{
return
new
FoodSprite
(
this
.
txFood
);
},
this
,
10000
);
}
GameVisualization
.
prototype
.
Run
=
function
()
...
...
@@ -80,6 +84,7 @@ GameVisualization.prototype.RemoveSnake = function(id)
if
(
id
in
this
.
snakes
)
{
this
.
mainStage
.
removeChild
(
this
.
snakes
[
id
].
Container
);
this
.
snakes
[
id
].
Destroy
();
delete
this
.
snakes
[
id
];
}
};
...
...
@@ -114,12 +119,13 @@ GameVisualization.prototype.HandleTickMessage = function(frame_id)
{
for
(
let
food_id
in
this
.
foodItems
)
{
if
(
!
this
.
foodItems
[
food_id
].
visible
)
let
item
=
this
.
foodItems
[
food_id
];
if
(
!
item
.
visible
)
{
delete
this
.
foodItems
[
food_id
];
this
.
foodItemPool
.
free
(
item
);
}
}
this
.
foodMap
.
CleanUp
();
}
...
...
@@ -155,7 +161,8 @@ GameVisualization.prototype.HandleWorldUpdateMessage = function(data)
GameVisualization
.
prototype
.
AddFood
=
function
(
food_id
,
pos_x
,
pos_y
,
value
)
{
let
sprite
=
new
FoodSprite
(
this
.
txFood
,
this
.
food_decay_rate
,
food_id
,
pos_x
,
pos_y
,
value
);
let
sprite
=
this
.
foodItemPool
.
get
();
sprite
.
SetData
(
this
.
food_decay_rate
,
food_id
,
pos_x
,
pos_y
,
value
);
this
.
foodItems
[
food_id
]
=
sprite
;
this
.
foodMap
.
AddSprite
(
sprite
);
};
...
...
@@ -180,7 +187,16 @@ GameVisualization.prototype.HandleFoodConsumedMessage = function(food_id, consum
if
(
food_id
in
this
.
foodItems
)
{
let
sprite
=
this
.
foodItems
[
food_id
];
this
.
snakes
[
consumer_id
].
Eat
(
sprite
);
if
(
consumer_id
in
this
.
snakes
)
{
this
.
snakes
[
consumer_id
].
Eat
(
sprite
);
}
else
{
this
.
foodItems
[
food_id
].
visible
=
false
;
delete
this
.
foodItems
[
food_id
];
this
.
foodItemPool
.
free
(
sprite
);
}
}
};
...
...
visualization/static/visualization/Snake.js
View file @
42e97f56
...
...
@@ -29,6 +29,27 @@ function Snake(headTexture, segmentPool, name, colorScheme, world_size_x, world_
this
.
Container
.
addChild
(
this
.
_nameSprite
);
}
Snake
.
prototype
.
Destroy
=
function
()
{
while
(
this
.
_segments
.
length
>
0
)
{
let
segment
=
this
.
_segments
.
pop
();
this
.
_segmentContainer
.
removeChild
(
segment
.
GetSprite
());
this
.
_segmentPool
.
free
(
segment
);
}
while
(
this
.
_foodContainer
.
children
.
length
>
0
)
{
let
foodItem
=
this
.
_foodContainer
.
children
[
0
];
foodItem
.
visible
=
false
;
// will be removed with decayed food
this
.
_foodContainer
.
removeChild
(
foodItem
);
}
this
.
_foodContainer
.
destroy
();
this
.
_segmentContainer
.
destroy
();
this
.
Container
.
destroy
();
};
Snake
.
prototype
.
SetData
=
function
(
data
)
{
this
.
heading
=
data
.
heading
;
...
...
@@ -163,8 +184,7 @@ Snake.prototype.AnimateEat = function()
if
(
dist
<
radius
)
{
food
.
visible
=
false
;
this
.
_foodContainer
.
removeChildAt
(
i
);
return
;
// FIXME this aborts eating for this animation cycle
this
.
_foodContainer
.
removeChild
(
food
);
}
let
factor
=
(
dist
-
food
.
speed
)
/
dist
;
...
...
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment