mirror of
https://github.com/clusterio/clusterio.git
synced 2026-11-24 00:23:12 +09:00
#61 Render entities with rotations
This code didn't even error once, it just worked. Which is nice.
This commit is contained in:
1 parent
51bb9f4d71
commit
0c365c0090
3 files changed
+32
-8
No files matched your search
@@ -69,7 +69,11 @@ function on_some_entity_created(event)
|
|||||||
complain("wtf, on_some_entity_created has nil entity")
|
complain("wtf, on_some_entity_created has nil entity")
|
||||||
return
|
return
|
||||||
end
|
end
|
||||||
write_file(ent.name.." "..ent.position.x.." "..ent.position.y)
|
local entityData = ent.name..","..ent.position.x..","..ent.position.y
|
||||||
|
if ent.supports_direction then
|
||||||
|
entityData = entityData .. ",rot="..ent.direction
|
||||||
|
end
|
||||||
|
write_file(entityData)
|
||||||
--writeout_objects(ent.surface, {left_top={x=math.floor(ent.position.x), y=math.floor(ent.position.y)}, right_bottom={x=math.floor(ent.position.x)+1, y=math.floor(ent.position.y)+1}})
|
--writeout_objects(ent.surface, {left_top={x=math.floor(ent.position.x), y=math.floor(ent.position.y)}, right_bottom={x=math.floor(ent.position.x)+1, y=math.floor(ent.position.y)+1}})
|
||||||
|
|
||||||
complain("on_some_entity_created: "..ent.name.." at "..ent.position.x..","..ent.position.y)
|
complain("on_some_entity_created: "..ent.name.." at "..ent.position.x..","..ent.position.y)
|
||||||
@@ -83,12 +87,12 @@ function on_some_entity_deleted(event)
|
|||||||
|
|
||||||
-- we can't do this now, because the entity still exists at this point. instead, we schedule the writeout for the next tick
|
-- we can't do this now, because the entity still exists at this point. instead, we schedule the writeout for the next tick
|
||||||
|
|
||||||
local surface = ent.surface
|
--local surface = ent.surface
|
||||||
local area = {left_top={x=math.floor(ent.position.x), y=math.floor(ent.position.y)}, right_bottom={x=math.floor(ent.position.x)+1, y=math.floor(ent.position.y)+1}}
|
--local area = {left_top={x=math.floor(ent.position.x), y=math.floor(ent.position.y)}, right_bottom={x=math.floor(ent.position.x)+1, y=math.floor(ent.position.y)+1}}
|
||||||
|
|
||||||
--table.insert(todo_next_tick, function () writeout_objects(surface, area ) end)
|
--table.insert(todo_next_tick, function () writeout_objects(surface, area ) end)
|
||||||
write_file("deleted "..ent.position.x.." "..ent.position.y)
|
write_file("deleted "..ent.position.x.." "..ent.position.y)
|
||||||
complain("on_some_entity_deleted: "..ent.name.." at "..ent.position.x..","..ent.position.y)
|
--complain("on_some_entity_deleted: "..ent.name.." at "..ent.position.x..","..ent.position.y)
|
||||||
end
|
end
|
||||||
|
|
||||||
script.on_event(defines.events.on_tick, on_tick)
|
script.on_event(defines.events.on_tick, on_tick)
|
||||||
|
|||||||
@@ -57,8 +57,8 @@ class remoteMap {
|
|||||||
scriptOutput(data){
|
scriptOutput(data){
|
||||||
if(data){
|
if(data){
|
||||||
this.messageInterface(data);
|
this.messageInterface(data);
|
||||||
// express-transport-belt -35.5 -14.5
|
// express-transport-belt,-35.5,-14.5,[...]
|
||||||
data = data.split(" ");
|
data = data.split(",");
|
||||||
if(data && data[0] == undefined){
|
if(data && data[0] == undefined){
|
||||||
console.log("empty: ");
|
console.log("empty: ");
|
||||||
console.log(data);
|
console.log(data);
|
||||||
@@ -74,8 +74,15 @@ class remoteMap {
|
|||||||
this.messageInterface("Deleted data "+xPos+", "+yPos);
|
this.messageInterface("Deleted data "+xPos+", "+yPos);
|
||||||
}).catch((err)=>this.messageInterface(err));
|
}).catch((err)=>this.messageInterface(err));
|
||||||
} else {
|
} else {
|
||||||
|
let entity = {name};
|
||||||
|
for(let i = 3; i < data.length; i++){
|
||||||
|
if(data[i].includes("=")){
|
||||||
|
let kv = data[i].split("=");
|
||||||
|
entity[kv[0]] = kv[1];
|
||||||
|
}
|
||||||
|
}
|
||||||
// save this entity in the database
|
// save this entity in the database
|
||||||
this.chunkMap.setEntity(xPos, yPos, {name}).then(data => {
|
this.chunkMap.setEntity(xPos, yPos, entity).then(data => {
|
||||||
this.messageInterface("Added "+name+" to data "+xPos+", "+yPos);
|
this.messageInterface("Added "+name+" to data "+xPos+", "+yPos);
|
||||||
}).catch((err)=>this.messageInterface(err));
|
}).catch((err)=>this.messageInterface(err));
|
||||||
}
|
}
|
||||||
|
|||||||
+14
-1
@@ -68,6 +68,14 @@ function requestMapDraw(){
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
function drawImage(ctx, image, x, y, w, h, degrees){
|
||||||
|
ctx.save();
|
||||||
|
ctx.translate(x+w/2, y+h/2);
|
||||||
|
ctx.rotate(degrees*Math.PI/180.0);
|
||||||
|
ctx.translate(-x-w/2, -y-h/2);
|
||||||
|
ctx.drawImage(image, x, y, w, h);
|
||||||
|
ctx.restore();
|
||||||
|
}
|
||||||
function drawEntity(entity){
|
function drawEntity(entity){
|
||||||
if(entity.x && entity.y){
|
if(entity.x && entity.y){
|
||||||
if(entity.entity && entity.entity.name && typeof entity.entity.name == "string"){
|
if(entity.entity && entity.entity.name && typeof entity.entity.name == "string"){
|
||||||
@@ -93,7 +101,12 @@ function drawEntity(entity){
|
|||||||
let name = entity.entity.name;
|
let name = entity.entity.name;
|
||||||
let xPos = (entity.x * 16) - playerPosition.x;
|
let xPos = (entity.x * 16) - playerPosition.x;
|
||||||
let yPos = (entity.y * 16) - playerPosition.y;
|
let yPos = (entity.y * 16) - playerPosition.y;
|
||||||
ctx.drawImage(entityImages[name].img, xPos, yPos, 16, 16);
|
let rotation = 0;
|
||||||
|
if(entity.entity.rot && !isNaN(Number(entity.entity.rot))){
|
||||||
|
rotation = entity.entity.rot * 45;
|
||||||
|
}
|
||||||
|
|
||||||
|
drawImage(ctx, entityImages[name].img, xPos, yPos, 16, 16, rotation);
|
||||||
console.log("Drawing "+name+" at X: "+xPos+", Y: "+yPos);
|
console.log("Drawing "+name+" at X: "+xPos+", Y: "+yPos);
|
||||||
} else {
|
} else {
|
||||||
this.queue.push(entity);
|
this.queue.push(entity);
|
||||||
|
|||||||
Reference in new issue
Block a user