bevy-learning

A learning project of mine.
git clone git://archive.git.mtrnord.blog/MTRNord/bevy-learning.git
Log | Files | Refs

commit c20a67816e7114cc32bccdf45c6b7555cf915888
parent c8039eee329058609229ae7ca6e7ed885e77ecb4
Author: MTRNord <mtrnord1@gmail.com>
Date:   Tue, 16 Mar 2021 14:09:10 +0100

Optimize collisions

Took 57 minutes

Diffstat:
MCargo.lock | 1+
MCargo.toml | 1+
Massets/map.ldtk | 281++++++++++++++++++++++++++++++++++++++++++++-----------------------------------
Msrc/main.rs | 6+++---
Msrc/plugins/player.rs | 113+++++++++++++++++++++----------------------------------------------------------
Msrc/plugins/world.rs | 65+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++--
6 files changed, 254 insertions(+), 213 deletions(-)

diff --git a/Cargo.lock b/Cargo.lock @@ -4448,6 +4448,7 @@ dependencies = [ "bevy_ldtk", "bevy_prototype_inline_assets", "bevy_rapier2d", + "rayon", ] [[package]] diff --git a/Cargo.toml b/Cargo.toml @@ -11,6 +11,7 @@ bevy_prototype_inline_assets = "0.1.1" bevy_discovery = "0.1.0" #kurinji = "1.0.5" bevy_ldtk = {version = "0.4.2", features = ["bevy-unstable"]} +rayon = "1.5" [dependencies.bevy] git = "https://github.com/bevyengine/bevy" diff --git a/assets/map.ldtk b/assets/map.ldtk @@ -964,7 +964,7 @@ "limitScope": "PerWorld", "limitBehavior": "PreventAdding", "pivotX": 0.5, - "pivotY": 1, + "pivotY": 0.5, "fieldDefs": [ { "identifier": "items", @@ -1056,7 +1056,7 @@ "limitScope": "PerLevel", "limitBehavior": "DiscardOldOnes", "pivotX": 0.5, - "pivotY": 1, + "pivotY": 0.5, "fieldDefs": [ { "identifier": "loot", @@ -1326,40 +1326,48 @@ "gridTiles": [], "entityInstances": [ { - "__identifier": "Chest", - "__grid": [37,20], + "__identifier": "Door", + "__grid": [41,10], "__pivot": [0.5,1], - "__tile": { "tilesetUid": 95, "srcRect": [304,288,16,16] }, - "width": 32, - "height": 24, - "defUid": 52, - "px": [600,336], + "__tile": null, + "width": 8, + "height": 48, + "defUid": 86, + "px": [664,176], + "fieldInstances": [{ "__identifier": "locked", "__value": false, "__type": "Bool", "defUid": 87, "realEditorValues": [] }] + }, + { + "__identifier": "Player", + "__grid": [18,15], + "__pivot": [0.5,0.5], + "__tile": { "tilesetUid": 95, "srcRect": [160,176,16,16] }, + "width": 16, + "height": 16, + "defUid": 46, + "px": [296,248], "fieldInstances": [{ - "__identifier": "content", - "__value": [ "Healing_Plant", "Water", "Meat" ], + "__identifier": "items", + "__value": [ "Knife", "Boots" ], "__type": "Array<LocalEnum.Item>", - "defUid": 53, + "defUid": 51, "realEditorValues": [ { "id": "V_String", - "params": ["Healing_Plant"] - }, { - "id": "V_String", - "params": ["Water"] + "params": ["Knife"] }, { "id": "V_String", - "params": ["Meat"] + "params": ["Boots"] } ] }] }, { "__identifier": "Mob", "__grid": [29,12], - "__pivot": [0.5,1], + "__pivot": [0.5,0.5], "__tile": { "tilesetUid": 91, "srcRect": [176,1328,16,16] }, "width": 20, "height": 20, "defUid": 54, - "px": [472,208], + "px": [472,200], "fieldInstances": [ { "__identifier": "patrol", @@ -1386,12 +1394,12 @@ { "__identifier": "Mob", "__grid": [23,20], - "__pivot": [0.5,1], + "__pivot": [0.5,0.5], "__tile": { "tilesetUid": 91, "srcRect": [176,1328,16,16] }, "width": 20, "height": 20, "defUid": 54, - "px": [376,336], + "px": [376,328], "fieldInstances": [ { "__identifier": "patrol", @@ -1415,45 +1423,31 @@ "height": 32, "defUid": 86, "px": [536,336], - "fieldInstances": [{ - "__identifier": "locked", - "__value": false, - "__type": "Bool", - "defUid": 87, - "realEditorValues": [null] - }] - }, - { - "__identifier": "Door", - "__grid": [41,10], - "__pivot": [0.5,1], - "__tile": null, - "width": 8, - "height": 48, - "defUid": 86, - "px": [664,176], "fieldInstances": [{ "__identifier": "locked", "__value": false, "__type": "Bool", "defUid": 87, "realEditorValues": [] }] }, { - "__identifier": "Player", - "__grid": [18,15], + "__identifier": "Chest", + "__grid": [37,20], "__pivot": [0.5,1], - "__tile": { "tilesetUid": 95, "srcRect": [160,176,16,16] }, - "width": 16, - "height": 16, - "defUid": 46, - "px": [296,256], + "__tile": { "tilesetUid": 95, "srcRect": [304,288,16,16] }, + "width": 24, + "height": 24, + "defUid": 52, + "px": [600,336], "fieldInstances": [{ - "__identifier": "items", - "__value": [ "Knife", "Boots" ], + "__identifier": "content", + "__value": [ "Healing_Plant", "Water", "Meat" ], "__type": "Array<LocalEnum.Item>", - "defUid": 51, + "defUid": 53, "realEditorValues": [ { "id": "V_String", - "params": ["Knife"] + "params": ["Healing_Plant"] }, { "id": "V_String", - "params": ["Boots"] + "params": ["Water"] + }, { + "id": "V_String", + "params": ["Meat"] } ] }] } @@ -2640,6 +2634,7 @@ { "px": [176,96], "src": [320,272], "f": 0, "t": 411, "d": [26,431] }, { "px": [192,96], "src": [320,272], "f": 0, "t": 411, "d": [26,432] }, { "px": [208,96], "src": [320,272], "f": 0, "t": 411, "d": [26,433] }, + { "px": [432,96], "src": [320,272], "f": 0, "t": 411, "d": [26,447] }, { "px": [736,96], "src": [320,272], "f": 0, "t": 411, "d": [26,466] }, { "px": [960,96], "src": [320,272], "f": 0, "t": 411, "d": [26,480] }, { "px": [976,96], "src": [320,272], "f": 0, "t": 411, "d": [26,481] }, @@ -2670,6 +2665,7 @@ { "px": [496,112], "src": [320,272], "f": 0, "t": 411, "d": [26,521] }, { "px": [512,112], "src": [320,272], "f": 0, "t": 411, "d": [26,522] }, { "px": [528,112], "src": [320,272], "f": 0, "t": 411, "d": [26,523] }, + { "px": [576,112], "src": [320,272], "f": 0, "t": 411, "d": [26,526] }, { "px": [592,112], "src": [320,272], "f": 0, "t": 411, "d": [26,527] }, { "px": [608,112], "src": [320,272], "f": 0, "t": 411, "d": [26,528] }, { "px": [736,112], "src": [320,272], "f": 0, "t": 411, "d": [26,536] }, @@ -2874,6 +2870,7 @@ { "px": [208,192], "src": [320,272], "f": 0, "t": 411, "d": [26,853] }, { "px": [256,192], "src": [320,272], "f": 0, "t": 411, "d": [26,856] }, { "px": [272,192], "src": [320,272], "f": 0, "t": 411, "d": [26,857] }, + { "px": [288,192], "src": [320,272], "f": 0, "t": 411, "d": [26,858] }, { "px": [336,192], "src": [320,272], "f": 0, "t": 411, "d": [26,861] }, { "px": [352,192], "src": [320,272], "f": 0, "t": 411, "d": [26,862] }, { "px": [368,192], "src": [320,272], "f": 0, "t": 411, "d": [26,863] }, @@ -2883,6 +2880,7 @@ { "px": [528,192], "src": [320,272], "f": 0, "t": 411, "d": [26,873] }, { "px": [544,192], "src": [320,272], "f": 0, "t": 411, "d": [26,874] }, { "px": [560,192], "src": [320,272], "f": 0, "t": 411, "d": [26,875] }, + { "px": [576,192], "src": [320,272], "f": 0, "t": 411, "d": [26,876] }, { "px": [592,192], "src": [320,272], "f": 0, "t": 411, "d": [26,877] }, { "px": [608,192], "src": [320,272], "f": 0, "t": 411, "d": [26,878] }, { "px": [960,192], "src": [320,272], "f": 0, "t": 411, "d": [26,900] }, @@ -2977,6 +2975,7 @@ { "px": [208,240], "src": [320,272], "f": 0, "t": 411, "d": [26,1063] }, { "px": [224,240], "src": [320,272], "f": 0, "t": 411, "d": [26,1064] }, { "px": [256,240], "src": [320,272], "f": 0, "t": 411, "d": [26,1066] }, + { "px": [272,240], "src": [320,272], "f": 0, "t": 411, "d": [26,1067] }, { "px": [304,240], "src": [320,272], "f": 0, "t": 411, "d": [26,1069] }, { "px": [320,240], "src": [320,272], "f": 0, "t": 411, "d": [26,1070] }, { "px": [336,240], "src": [320,272], "f": 0, "t": 411, "d": [26,1071] }, @@ -3256,6 +3255,7 @@ { "px": [864,0], "src": [288,240], "f": 0, "t": 363, "d": [37,54] }, { "px": [944,0], "src": [288,240], "f": 0, "t": 363, "d": [37,59] }, { "px": [224,16], "src": [288,240], "f": 1, "t": 363, "d": [37,84] }, + { "px": [864,16], "src": [288,240], "f": 0, "t": 363, "d": [37,124] }, { "px": [944,16], "src": [288,240], "f": 0, "t": 363, "d": [37,129] }, { "px": [224,32], "src": [288,240], "f": 1, "t": 363, "d": [37,154] }, { "px": [944,32], "src": [288,240], "f": 0, "t": 363, "d": [37,199] }, @@ -3266,7 +3266,9 @@ { "px": [224,80], "src": [288,240], "f": 1, "t": 363, "d": [37,364] }, { "px": [944,80], "src": [288,240], "f": 0, "t": 363, "d": [37,409] }, { "px": [224,96], "src": [288,240], "f": 1, "t": 363, "d": [37,434] }, + { "px": [416,96], "src": [288,240], "f": 0, "t": 363, "d": [37,446] }, { "px": [608,96], "src": [288,240], "f": 0, "t": 363, "d": [37,458] }, + { "px": [720,96], "src": [288,240], "f": 0, "t": 363, "d": [37,465] }, { "px": [944,96], "src": [288,240], "f": 0, "t": 363, "d": [37,479] }, { "px": [224,112], "src": [288,240], "f": 1, "t": 363, "d": [37,504] }, { "px": [416,112], "src": [288,240], "f": 0, "t": 363, "d": [37,516] }, @@ -3280,17 +3282,20 @@ { "px": [784,128], "src": [288,240], "f": 1, "t": 363, "d": [37,609] }, { "px": [816,128], "src": [288,240], "f": 0, "t": 363, "d": [37,611] }, { "px": [944,128], "src": [288,240], "f": 0, "t": 363, "d": [37,619] }, + { "px": [224,144], "src": [288,240], "f": 1, "t": 363, "d": [37,644] }, { "px": [384,144], "src": [288,240], "f": 0, "t": 363, "d": [37,654] }, { "px": [784,144], "src": [288,240], "f": 1, "t": 363, "d": [37,679] }, { "px": [816,144], "src": [288,240], "f": 0, "t": 363, "d": [37,681] }, { "px": [944,144], "src": [288,240], "f": 0, "t": 363, "d": [37,689] }, { "px": [192,160], "src": [288,240], "f": 1, "t": 363, "d": [37,712] }, + { "px": [352,160], "src": [288,240], "f": 0, "t": 363, "d": [37,722] }, { "px": [784,160], "src": [288,240], "f": 1, "t": 363, "d": [37,749] }, { "px": [816,160], "src": [288,240], "f": 0, "t": 363, "d": [37,751] }, { "px": [944,160], "src": [288,240], "f": 0, "t": 363, "d": [37,759] }, { "px": [192,176], "src": [288,240], "f": 1, "t": 363, "d": [37,782] }, { "px": [352,176], "src": [288,240], "f": 0, "t": 363, "d": [37,792] }, { "px": [624,176], "src": [288,240], "f": 1, "t": 363, "d": [37,809] }, + { "px": [720,176], "src": [288,240], "f": 0, "t": 363, "d": [37,815] }, { "px": [784,176], "src": [288,240], "f": 1, "t": 363, "d": [37,819] }, { "px": [944,176], "src": [288,240], "f": 0, "t": 363, "d": [37,829] }, { "px": [480,192], "src": [288,240], "f": 1, "t": 363, "d": [37,870] }, @@ -3309,6 +3314,7 @@ { "px": [352,272], "src": [288,240], "f": 0, "t": 363, "d": [37,1212] }, { "px": [448,272], "src": [288,240], "f": 1, "t": 363, "d": [37,1218] }, { "px": [944,272], "src": [288,240], "f": 0, "t": 363, "d": [37,1249] }, + { "px": [224,288], "src": [288,240], "f": 1, "t": 363, "d": [37,1274] }, { "px": [352,288], "src": [288,240], "f": 0, "t": 363, "d": [37,1282] }, { "px": [944,288], "src": [288,240], "f": 0, "t": 363, "d": [37,1319] }, { "px": [224,304], "src": [288,240], "f": 1, "t": 363, "d": [37,1344] }, @@ -3318,7 +3324,10 @@ { "px": [608,304], "src": [288,240], "f": 1, "t": 363, "d": [37,1368] }, { "px": [944,304], "src": [288,240], "f": 0, "t": 363, "d": [37,1389] }, { "px": [224,320], "src": [288,240], "f": 1, "t": 363, "d": [37,1414] }, + { "px": [352,320], "src": [288,240], "f": 0, "t": 363, "d": [37,1422] }, { "px": [400,320], "src": [288,240], "f": 1, "t": 363, "d": [37,1425] }, + { "px": [432,320], "src": [288,240], "f": 0, "t": 363, "d": [37,1427] }, + { "px": [608,320], "src": [288,240], "f": 1, "t": 363, "d": [37,1438] }, { "px": [944,320], "src": [288,240], "f": 0, "t": 363, "d": [37,1459] }, { "px": [224,336], "src": [288,240], "f": 1, "t": 363, "d": [37,1484] }, { "px": [944,336], "src": [288,240], "f": 0, "t": 363, "d": [37,1529] }, @@ -3330,33 +3339,46 @@ { "px": [944,384], "src": [288,240], "f": 0, "t": 363, "d": [37,1739] }, { "px": [224,400], "src": [288,240], "f": 1, "t": 363, "d": [37,1764] }, { "px": [944,400], "src": [288,240], "f": 0, "t": 363, "d": [37,1809] }, + { "px": [784,16], "src": [288,272], "f": 0, "t": 409, "d": [38,119] }, { "px": [800,16], "src": [288,272], "f": 0, "t": 409, "d": [38,120] }, { "px": [816,16], "src": [288,272], "f": 0, "t": 409, "d": [38,121] }, + { "px": [832,16], "src": [288,272], "f": 0, "t": 409, "d": [38,122] }, { "px": [448,96], "src": [288,272], "f": 2, "t": 409, "d": [38,448] }, { "px": [464,96], "src": [288,272], "f": 2, "t": 409, "d": [38,449] }, { "px": [480,96], "src": [288,272], "f": 2, "t": 409, "d": [38,450] }, { "px": [496,96], "src": [288,272], "f": 2, "t": 409, "d": [38,451] }, { "px": [512,96], "src": [288,272], "f": 2, "t": 409, "d": [38,452] }, + { "px": [624,96], "src": [288,272], "f": 2, "t": 409, "d": [38,459] }, { "px": [752,96], "src": [288,272], "f": 2, "t": 409, "d": [38,467] }, { "px": [768,96], "src": [288,272], "f": 2, "t": 409, "d": [38,468] }, { "px": [784,96], "src": [288,272], "f": 2, "t": 409, "d": [38,469] }, + { "px": [816,96], "src": [288,272], "f": 2, "t": 409, "d": [38,471] }, + { "px": [544,112], "src": [288,272], "f": 2, "t": 409, "d": [38,524] }, + { "px": [560,112], "src": [288,272], "f": 2, "t": 409, "d": [38,525] }, { "px": [656,128], "src": [288,272], "f": 2, "t": 409, "d": [38,601] }, { "px": [672,128], "src": [288,272], "f": 2, "t": 409, "d": [38,602] }, + { "px": [816,176], "src": [288,272], "f": 0, "t": 409, "d": [38,821] }, { "px": [224,192], "src": [288,272], "f": 2, "t": 409, "d": [38,854] }, { "px": [240,192], "src": [288,272], "f": 2, "t": 409, "d": [38,855] }, { "px": [304,192], "src": [288,272], "f": 2, "t": 409, "d": [38,859] }, { "px": [320,192], "src": [288,272], "f": 2, "t": 409, "d": [38,860] }, + { "px": [416,192], "src": [288,272], "f": 0, "t": 409, "d": [38,866] }, { "px": [432,192], "src": [288,272], "f": 0, "t": 409, "d": [38,867] }, { "px": [448,192], "src": [288,272], "f": 0, "t": 409, "d": [38,868] }, { "px": [624,192], "src": [288,272], "f": 0, "t": 409, "d": [38,879] }, { "px": [416,224], "src": [288,272], "f": 2, "t": 409, "d": [38,1006] }, + { "px": [432,224], "src": [288,272], "f": 2, "t": 409, "d": [38,1007] }, { "px": [288,240], "src": [288,272], "f": 0, "t": 409, "d": [38,1068] }, { "px": [480,288], "src": [288,272], "f": 2, "t": 409, "d": [38,1290] }, + { "px": [512,288], "src": [288,272], "f": 2, "t": 409, "d": [38,1292] }, + { "px": [560,288], "src": [288,272], "f": 2, "t": 409, "d": [38,1295] }, { "px": [576,288], "src": [288,272], "f": 2, "t": 409, "d": [38,1296] }, { "px": [592,288], "src": [288,272], "f": 2, "t": 409, "d": [38,1297] }, + { "px": [608,288], "src": [288,272], "f": 2, "t": 409, "d": [38,1298] }, { "px": [448,320], "src": [288,272], "f": 0, "t": 409, "d": [38,1428] }, { "px": [464,320], "src": [288,272], "f": 0, "t": 409, "d": [38,1429] }, { "px": [480,320], "src": [288,272], "f": 0, "t": 409, "d": [38,1430] }, + { "px": [512,320], "src": [288,272], "f": 0, "t": 409, "d": [38,1432] }, { "px": [528,320], "src": [288,272], "f": 0, "t": 409, "d": [38,1433] }, { "px": [544,320], "src": [288,272], "f": 0, "t": 409, "d": [38,1434] }, { "px": [560,320], "src": [288,272], "f": 0, "t": 409, "d": [38,1435] }, @@ -3476,6 +3498,12 @@ { "px": [1024,352], "src": [256,240], "f": 0, "t": 361, "d": [77,1605] }, { "px": [1040,352], "src": [272,240], "f": 0, "t": 362, "d": [77,1605] }, { "px": [1056,352], "src": [288,240], "f": 0, "t": 363, "d": [77,1605] }, + { "px": [384,304], "src": [128,304], "f": 1, "t": 445, "d": [71,1422] }, + { "px": [384,320], "src": [128,320], "f": 1, "t": 468, "d": [71,1422] }, + { "px": [368,304], "src": [144,304], "f": 1, "t": 446, "d": [71,1422] }, + { "px": [368,320], "src": [144,320], "f": 1, "t": 469, "d": [71,1422] }, + { "px": [352,304], "src": [160,304], "f": 1, "t": 447, "d": [71,1422] }, + { "px": [352,320], "src": [160,320], "f": 1, "t": 470, "d": [71,1422] }, { "px": [416,96], "src": [176,272], "f": 2, "t": 402, "d": [39,446] }, { "px": [432,96], "src": [160,304], "f": 2, "t": 447, "d": [39,447] }, { "px": [544,112], "src": [160,304], "f": 2, "t": 447, "d": [39,524] }, @@ -3485,12 +3513,17 @@ { "px": [416,192], "src": [160,304], "f": 0, "t": 447, "d": [39,866] }, { "px": [576,192], "src": [160,304], "f": 0, "t": 447, "d": [39,876] }, { "px": [272,240], "src": [192,304], "f": 0, "t": 449, "d": [39,1067] }, + { "px": [352,320], "src": [144,272], "f": 0, "t": 400, "d": [39,1422] }, { "px": [432,320], "src": [144,272], "f": 0, "t": 400, "d": [39,1427] }, { "px": [512,320], "src": [144,272], "f": 0, "t": 400, "d": [39,1432] }, { "px": [832,16], "src": [144,272], "f": 0, "t": 400, "d": [65,122] }, + { "px": [864,16], "src": [128,320], "f": 0, "t": 468, "d": [65,124] }, { "px": [624,96], "src": [128,320], "f": 2, "t": 468, "d": [65,459] }, + { "px": [224,144], "src": [144,272], "f": 0, "t": 400, "d": [65,644] }, { "px": [352,160], "src": [144,304], "f": 3, "t": 446, "d": [65,722] }, + { "px": [720,176], "src": [128,320], "f": 1, "t": 468, "d": [65,815] }, { "px": [432,224], "src": [144,304], "f": 2, "t": 446, "d": [65,1007] }, + { "px": [224,288], "src": [128,320], "f": 2, "t": 468, "d": [65,1274] }, { "px": [512,288], "src": [144,304], "f": 2, "t": 446, "d": [65,1292] }, { "px": [352,320], "src": [144,272], "f": 1, "t": 400, "d": [65,1422] }, { "px": [784,16], "src": [208,304], "f": 0, "t": 450, "d": [80,119] }, @@ -3568,78 +3601,6 @@ "gridTiles": [], "entityInstances": [ { - "__identifier": "Mob", - "__grid": [28,14], - "__pivot": [0.5,1], - "__tile": { "tilesetUid": 91, "srcRect": [176,1328,16,16] }, - "width": 20, - "height": 20, - "defUid": 54, - "px": [456,240], - "fieldInstances": [ - { - "__identifier": "patrol", - "__value": [{ "cx": 22, "cy": 14 }], - "__type": "Array<Point>", - "defUid": 55, - "realEditorValues": [{ - "id": "V_String", - "params": ["22,14"] - }] - }, - { "__identifier": "loot", "__value": [], "__type": "Array<LocalEnum.Item>", "defUid": 56, "realEditorValues": [] } - ] - }, - { - "__identifier": "Mob", - "__grid": [26,20], - "__pivot": [0.5,1], - "__tile": { "tilesetUid": 91, "srcRect": [176,1328,16,16] }, - "width": 20, - "height": 20, - "defUid": 54, - "px": [424,336], - "fieldInstances": [ - { - "__identifier": "patrol", - "__value": [{ "cx": 23, "cy": 20 }], - "__type": "Array<Point>", - "defUid": 55, - "realEditorValues": [{ - "id": "V_String", - "params": ["23,20"] - }] - }, - { "__identifier": "loot", "__value": [], "__type": "Array<LocalEnum.Item>", "defUid": 56, "realEditorValues": [] } - ] - }, - { - "__identifier": "Mob", - "__grid": [12,13], - "__pivot": [0.5,1], - "__tile": { "tilesetUid": 91, "srcRect": [176,1328,16,16] }, - "width": 48, - "height": 48, - "defUid": 54, - "px": [200,224], - "fieldInstances": [ - { "__identifier": "patrol", "__value": [], "__type": "Array<Point>", "defUid": 55, "realEditorValues": [] }, - { - "__identifier": "loot", - "__value": [ "Knife", "Healing_Plant" ], - "__type": "Array<LocalEnum.Item>", - "defUid": 56, - "realEditorValues": [ { - "id": "V_String", - "params": ["Knife"] - }, { - "id": "V_String", - "params": ["Healing_Plant"] - } ] - } - ] - }, - { "__identifier": "Door", "__grid": [20,13], "__pivot": [0.5,1], @@ -3692,6 +3653,78 @@ "params": [ true ] }] }] + }, + { + "__identifier": "Mob", + "__grid": [12,12], + "__pivot": [0.5,0.5], + "__tile": { "tilesetUid": 91, "srcRect": [176,1328,16,16] }, + "width": 48, + "height": 48, + "defUid": 54, + "px": [200,200], + "fieldInstances": [ + { "__identifier": "patrol", "__value": [], "__type": "Array<Point>", "defUid": 55, "realEditorValues": [] }, + { + "__identifier": "loot", + "__value": [ "Knife", "Healing_Plant" ], + "__type": "Array<LocalEnum.Item>", + "defUid": 56, + "realEditorValues": [ { + "id": "V_String", + "params": ["Knife"] + }, { + "id": "V_String", + "params": ["Healing_Plant"] + } ] + } + ] + }, + { + "__identifier": "Mob", + "__grid": [28,14], + "__pivot": [0.5,0.5], + "__tile": { "tilesetUid": 91, "srcRect": [176,1328,16,16] }, + "width": 20, + "height": 20, + "defUid": 54, + "px": [456,232], + "fieldInstances": [ + { + "__identifier": "patrol", + "__value": [{ "cx": 22, "cy": 14 }], + "__type": "Array<Point>", + "defUid": 55, + "realEditorValues": [{ + "id": "V_String", + "params": ["22,14"] + }] + }, + { "__identifier": "loot", "__value": [], "__type": "Array<LocalEnum.Item>", "defUid": 56, "realEditorValues": [] } + ] + }, + { + "__identifier": "Mob", + "__grid": [26,20], + "__pivot": [0.5,0.5], + "__tile": { "tilesetUid": 91, "srcRect": [176,1328,16,16] }, + "width": 20, + "height": 20, + "defUid": 54, + "px": [424,328], + "fieldInstances": [ + { + "__identifier": "patrol", + "__value": [{ "cx": 23, "cy": 20 }], + "__type": "Array<Point>", + "defUid": 55, + "realEditorValues": [{ + "id": "V_String", + "params": ["23,20"] + }] + }, + { "__identifier": "loot", "__value": [], "__type": "Array<LocalEnum.Item>", "defUid": 56, "realEditorValues": [] } + ] } ] }, @@ -4798,12 +4831,12 @@ { "__identifier": "Mob", "__grid": [19,10], - "__pivot": [0.5,1], + "__pivot": [0.5,0.5], "__tile": { "tilesetUid": 91, "srcRect": [176,1328,16,16] }, "width": 20, "height": 20, "defUid": 54, - "px": [312,176], + "px": [312,168], "fieldInstances": [ { "__identifier": "patrol", diff --git a/src/main.rs b/src/main.rs @@ -2,6 +2,7 @@ use crate::plugins::{ world::{MainCamera, WorldState}, PlayerPlugin, WorldPlugin, }; +use bevy::diagnostic::{FrameTimeDiagnosticsPlugin, LogDiagnosticsPlugin}; use bevy::render::camera::{OrthographicProjection, ScalingMode}; use bevy::{prelude::*, window::WindowMode}; use bevy_ldtk::*; @@ -29,9 +30,8 @@ fn main() { .init_resource::<GameState>() .add_plugins(DefaultPlugins) .add_plugin(LdtkPlugin) - //.add_plugin(PrintDiagnosticsPlugin::default()) - //.add_plugin(FrameTimeDiagnosticsPlugin::default()) - //.add_system(PrintDiagnosticsPlugin::print_diagnostics_system.system()) + .add_plugin(FrameTimeDiagnosticsPlugin::default()) + .add_plugin(LogDiagnosticsPlugin::default()) //.add_plugin(KurinjiPlugin::default()) .add_startup_system(setup.system()) .add_plugin(PlayerPlugin) diff --git a/src/plugins/player.rs b/src/plugins/player.rs @@ -6,6 +6,7 @@ use crate::GameState; use bevy::math::Vec3Swizzles; use bevy::prelude::*; use bevy_ldtk::LdtkMap; +use rayon::prelude::*; use std::convert::TryInto; pub struct PlayerPlugin; @@ -22,7 +23,6 @@ fn spawn_player( commands: &mut Commands, map_query: Query<&Handle<LdtkMap>>, map_assets: Res<Assets<LdtkMap>>, - mut materials: ResMut<Assets<ColorMaterial>>, mut camera: Query<&mut Transform, With<MainCamera>>, mut texture_atlases: ResMut<Assets<TextureAtlas>>, ) { @@ -68,7 +68,7 @@ fn spawn_player( player_start.px[0] as f32, // The player y position is the entity's y position from the map data, but // multiplied by negative one because in the LDtk map +y means down and not up. - -(player_start.px[1] as f32 - (player_start.height as f32 / 2.0)), + -(player_start.px[1]) as f32, // Spawn the player with the z value we determined earlier player_z, ); @@ -135,8 +135,6 @@ fn spawn_player( fn player_movement( time: Res<Time>, game_state: ResMut<GameState>, - map_query: Query<&Handle<LdtkMap>>, - map_assets: Res<Assets<LdtkMap>>, input: Res<Input<KeyCode>>, mut player: Query<(&PlayerBundle, &mut Transform)>, mut camera: Query<&mut Transform, With<MainCamera>>, @@ -178,27 +176,21 @@ fn player_movement( // assuming there is exactly one player entity, so this is OK if let Some((_, mut player_transform)) = player.iter_mut().next() { - if can_move_to_requested_coordinate( - &map_query, - &map_assets, - &game_state, - player_transform.translation + translation, - 16, - 16, - ) { + let new_coords = player_transform.translation + translation; + let rounded_x = new_coords.x.round(); + let rounded_y = new_coords.y.round(); + let rounded_cords = Vec3::new(rounded_x, rounded_y, new_coords.z); + if can_move_to_requested_coordinate(&game_state, rounded_cords, 16, 16) { player_transform.translation += translation; } } // assuming there is exactly one camera entity, so this is OK if let Some(mut camera_transform) = camera.iter_mut().next() { - if can_move_to_requested_coordinate( - &map_query, - &map_assets, - &game_state, - camera_transform.translation + translation, - 16, - 16, - ) { + let new_coords = camera_transform.translation + translation; + let rounded_x = new_coords.x.round(); + let rounded_y = new_coords.y.round(); + let rounded_cords = Vec3::new(rounded_x, rounded_y, new_coords.z); + if can_move_to_requested_coordinate(&game_state, rounded_cords, 16, 16) { camera_transform.translation += translation; } } @@ -206,75 +198,28 @@ fn player_movement( } fn can_move_to_requested_coordinate( - map_query: &Query<&Handle<LdtkMap>>, - map_assets: &Res<Assets<LdtkMap>>, game_state: &ResMut<GameState>, coordinate: Vec3, width: i32, height: i32, ) -> bool { - // Loop through all of the maps - for map_handle in map_query.iter() { - // We have to `if let` here because asset server may not have finished loading - // the map yet. - if let Some(map) = map_assets.get(map_handle) { - let level_idx = game_state.world_state.level; - - // Get the level from the project - let level = &map.project.levels[level_idx]; - - // Find the collision layer - let collision_layer = level - .layer_instances - .as_ref() // get a reference to the layer instances - .unwrap() // Unwrap the option ( this could be None, if there are no layers ) - .iter() // Iterate over the layers - .find(|&x| x.__identifier == "Collisions") // Filter on the one we want - .unwrap(); // Unwrap it ( would be None if it could not find a layer "Collisions" ) - - // Do a bounding box check. Bounding boxes are 16*16 from center of the object - for (i, object) in collision_layer.int_grid_csv.iter().enumerate() { - // Skip ladders - if *object == 2 || *object == 0 { - continue; - } - - // Check moves - let object_coordinate = - one_d_to_two_d_coordinate(i as f32, collision_layer.__c_wid as f32, 16.0, 16.0); - - if check_intersection( - coordinate.xy(), - object_coordinate, - Vec2::new(width as f32, height as f32), - Vec2::new(16.0, 16.0), - ) { - // We found an collision - return false; - } - } - } - } - true -} - -fn check_intersection(a: Vec2, b: Vec2, a_size: Vec2, b_size: Vec2) -> bool { - ((a.x - b.x).abs() * 2.0 < (a_size.x + b_size.x)) - && ((a.y - b.y).abs() * 2.0 < (a_size.y + b_size.y)) -} - -fn two_d_to_one_d_coordinate(coordinate: Vec3, row_length: f32) -> f32 { - (coordinate.y * row_length) + (-coordinate.x) + // Do a bounding box check. Bounding boxes are 16*16 from center of the object + return !game_state + .world_state + .collisions + .clone() + .par_iter() + .any(|object_coordinate| { + check_intersection( + coordinate.xy(), + object_coordinate, + Vec2::new(width as f32, height as f32), + Vec2::new(16.0, 16.0), + ) + }); } -fn one_d_to_two_d_coordinate( - coordinate: f32, - row_length: f32, - tile_width: f32, - tile_height: f32, -) -> Vec2 { - Vec2::new( - (((coordinate % row_length) * tile_width) + (tile_width / 2.0)).round() as f32, - ((-(coordinate / row_length * tile_height)) - (tile_height / 2.0)).round() as f32, - ) +fn check_intersection(a: Vec2, b: &Vec2, a_size: Vec2, b_size: Vec2) -> bool { + ((a.x - b.x).abs() * 2.0 <= (a_size.x + b_size.x)) + && ((a.y - b.y).abs() * 2.0 <= (a_size.y + b_size.y)) } diff --git a/src/plugins/world.rs b/src/plugins/world.rs @@ -1,6 +1,7 @@ use crate::GameState; use bevy::prelude::*; -use bevy_ldtk::{LdtkMapBundle, LdtkMapConfig}; +use bevy_ldtk::{LdtkMap, LdtkMapBundle, LdtkMapConfig}; +use rayon::prelude::*; pub struct MainCamera; @@ -11,16 +12,76 @@ pub struct WorldState { pub level: usize, pub requested_level: usize, pub world: Option<Entity>, + pub collisions: Vec<Vec2>, } pub struct WorldPlugin; impl Plugin for WorldPlugin { fn build(&self, app: &mut AppBuilder) { - app.add_system(draw.system()); + app.add_system(draw.system()) + .add_system(setup_collisions.system()); } } +pub fn setup_collisions( + map_query: Query<&Handle<LdtkMap>>, + map_assets: Res<Assets<LdtkMap>>, + mut game_state: ResMut<GameState>, +) { + if game_state.world_state.collisions_loaded + && game_state.world_state.level == game_state.world_state.requested_level + { + return; + } + + // Loop through all of the maps + for map_handle in map_query.iter() { + // We have to `if let` here because asset server may not have finished loading + // the map yet. + if let Some(map) = map_assets.get(map_handle) { + let level_idx = game_state.world_state.level; + + // Get the level from the project + let level = &map.project.levels[level_idx]; + + // Find the collision layer + let collision_layer = level + .layer_instances + .as_ref() // get a reference to the layer instances + .unwrap() // Unwrap the option ( this could be None, if there are no layers ) + .iter() // Iterate over the layers + .find(|&x| x.__identifier == "Collisions") // Filter on the one we want + .unwrap(); // Unwrap it ( would be None if it could not find a layer "Collisions" ) + + // Calculate collider center coordinates + game_state.world_state.collisions = collision_layer + .int_grid_csv + .clone() + .into_par_iter() + .enumerate() + .filter(|(_, object)| *object != 2 && *object != 0) + .map(|(i, _)| { + one_d_to_two_d_coordinate(i as f32, collision_layer.__c_wid as f32, 16.0, 16.0) + }) + .collect(); + game_state.world_state.collisions_loaded = true; + } + } +} + +fn one_d_to_two_d_coordinate( + coordinate: f32, + row_length: f32, + tile_width: f32, + tile_height: f32, +) -> Vec2 { + Vec2::new( + (((coordinate % row_length) * tile_width) + (tile_width / 2.0)).round() as f32, + ((-(coordinate / row_length * tile_height)) - (tile_height / 2.0)).round() as f32, + ) +} + fn draw( commands: &mut Commands, asset_server: Res<AssetServer>,