-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathEntityDrawer.cpp
More file actions
936 lines (833 loc) · 30.8 KB
/
Copy pathEntityDrawer.cpp
File metadata and controls
936 lines (833 loc) · 30.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
#include "EntityDrawer.h"
#include <algorithm>
#include "TextureCatcher.h"
#ifdef GD_EXTENSION_GODOCTOPUS
#include <godot_cpp/variant/utility_functions.hpp>
#include <godot_cpp/classes/rendering_server.hpp>
#else
#include "servers/rendering_server.h"
#endif
#define ENTITY_DRAWER_EPSILON 0.000000001
namespace godot
{
Vector3 color_from_idx(int idx_p)
{
// Compute the color based on the idx
int r = idx_p % 256;
int g = (idx_p/ 256 ) % 256;
int b = (idx_p/ (256*256) ) % 256;
return Vector3(r/255.,g/255.,b/255.);
}
int idx_from_color(Color const &color_p)
{
int r = int(color_p.r * 255);
int g = int(color_p.g * 255);
int b = int(color_p.b * 255);
if(r != 255 || g != 255 || b != 255)
{
return r + g *256 + b *256*256;
}
return -1;
}
Color safe_color(int x, int y, Ref<Image> const &image_p)
{
if(x >= 0 && x < image_p->get_width()
&& y >= 0 && y < image_p->get_height())
{
return image_p->get_pixel(x, y);
}
return Color(1.f,1.f,1.f,1.f);
}
void init_animation(DirectionalAnimation &anim_p, StringName const &base_anim_p)
{
anim_p.base_name = base_anim_p;
anim_p.names[DirectionHandler::UP] = "up_"+base_anim_p;
anim_p.names[DirectionHandler::DOWN] = "down_"+base_anim_p;
anim_p.names[DirectionHandler::LEFT] = "left_"+base_anim_p;
anim_p.names[DirectionHandler::RIGHT] = "right_"+base_anim_p;
}
int get_direction(Vector2 const &dir_p, bool has_up_down_p)
{
int type_l = DirectionHandler::NONE;
if(std::abs(dir_p.x) > ENTITY_DRAWER_EPSILON || std::abs(dir_p.y) > ENTITY_DRAWER_EPSILON)
{
if(std::abs(dir_p.x) > std::abs(dir_p.y) || !has_up_down_p)
{
if(dir_p.x > 0)
{
type_l = DirectionHandler::RIGHT;
}
else
{
type_l = DirectionHandler::LEFT;
}
}
else
{
if(dir_p.y > 0)
{
type_l = DirectionHandler::DOWN;
}
else
{
type_l = DirectionHandler::UP;
}
}
}
return type_l;
}
EntityDrawer::~EntityDrawer()
{
_instances.for_each([&](EntityInstance &, size_t idx_p) {
free_instance(idx_p);
});
delete _payload_handler;
}
// helper for animation
void set_up_animation(smart_list_handle<AnimationInstance> &handle_p,
double elapsed_time_p, Ref<Shader> const &shader_p, RID const &parent_p,
Vector2 const &offset_p, Ref<SpriteFrames> const & animation_p,
StringName const ¤t_animation_p, StringName const &next_animation_p, bool one_shot_p)
{
AnimationInstance &animation_l = handle_p.get();
animation_l.offset = offset_p;
animation_l.animation = animation_p;
animation_l.start = elapsed_time_p;
animation_l.frame_idx = 0;
animation_l.current_animation = current_animation_p;
animation_l.next_animation = next_animation_p;
animation_l.one_shot = one_shot_p;
// if fresh new animation we set it up
if(handle_p.revision() == 0)
{
// set up resources
animation_l.info.rid = RenderingServer::get_singleton()->canvas_item_create();
animation_l.info.material = Ref<ShaderMaterial>(memnew(ShaderMaterial));
animation_l.info.material->set_shader(shader_p);
RenderingServer::get_singleton()->canvas_item_set_parent(animation_l.info.rid, parent_p);
RenderingServer::get_singleton()->canvas_item_set_default_texture_filter(animation_l.info.rid, RenderingServer::CANVAS_ITEM_TEXTURE_FILTER_NEAREST);
RenderingServer::get_singleton()->canvas_item_set_material(animation_l.info.rid, animation_l.info.material->get_rid());
}
}
int EntityDrawer::add_instance(Vector2 const &pos_p, Vector2 const &offset_p, Ref<SpriteFrames> const & animation_p,
StringName const ¤t_animation_p, StringName const &next_animation_p, bool one_shot_p, bool in_front_p)
{
std::lock_guard<std::mutex> lock_l(_internal_mutex);
EntityInstance entity_l;
// animation
entity_l.animation = animations.recycle_instance();
set_up_animation(entity_l.animation, _elapsedAllTime, _shader, get_canvas_item(), offset_p, animation_p, current_animation_p, next_animation_p, one_shot_p);
// reset z_index in case we reuse an instance for a sub instance
RenderingServer::get_singleton()->canvas_item_set_z_index(entity_l.animation.get().info.rid, in_front_p? 1 : 0);
// register instance
smart_list_handle<EntityInstance> handle_l = _instances.new_instance(entity_l);
// add payload
_payload_handler->add_payload();
// position
handle_l.get().pos_idx = pos_indexes.recycle_instance();
if(handle_l.get().pos_idx.get().idx < _newPos.size()
&& handle_l.get().pos_idx.get().idx < _oldPos.size())
{
_newPos[handle_l.get().pos_idx.get().idx] = pos_p;
_oldPos[handle_l.get().pos_idx.get().idx] = pos_p;
}
else
{
// update index
handle_l.get().pos_idx.get().idx = _newPos.size();
_newPos.push_back(pos_p);
_oldPos.push_back(pos_p);
}
return int(handle_l.handle());
}
int EntityDrawer::add_sub_instance(int idx_ref_p, Vector2 const &offset_p, Ref<SpriteFrames> const & animation_p,
StringName const ¤t_animation_p, StringName const &next_animation_p,
bool one_shot_p, bool in_front_p, bool use_directions_p)
{
std::lock_guard<std::mutex> lock_l(_internal_mutex);
if(!_instances.is_valid(idx_ref_p))
{
return -1;
}
EntityInstance entity_l;
// animation
entity_l.animation = animations.recycle_instance();
set_up_animation(entity_l.animation, _elapsedAllTime, _shader, get_canvas_item(), offset_p, animation_p, current_animation_p, next_animation_p, one_shot_p);
RenderingServer::get_singleton()->canvas_item_set_z_index(entity_l.animation.get().info.rid, in_front_p ? 2 : -1);
// copy reference for position and dir_handler
entity_l.pos_idx = _instances.get(idx_ref_p).pos_idx;
entity_l.main_instance = _instances.get_handle(idx_ref_p);
if(use_directions_p)
{
entity_l.dir_handler = _instances.get(idx_ref_p).dir_handler;
if(entity_l.dir_handler.is_valid())
{
DirectionalAnimation anim_l;
init_animation(anim_l, entity_l.animation.get().current_animation);
entity_l.dir_animation = dir_animations.new_instance(anim_l);
}
}
// register instance
smart_list_handle<EntityInstance> handle_l = _instances.new_instance(entity_l);
// add payload
_payload_handler->add_payload();
// set up relation for main instance
entity_l.main_instance.get().sub_instances.push_back(handle_l);
return int(handle_l.handle());
}
void EntityDrawer::free_instance(int idx_p, bool skip_main_free_p)
{
std::lock_guard<std::mutex> *lock_l = nullptr;
if(!skip_main_free_p)
{
lock_l = new std::lock_guard<std::mutex>(_internal_mutex);
};
EntityInstance &instance_l = _instances.get(idx_p);
// free all components that cannot be inherited
if(instance_l.animation.is_valid())
{
if(instance_l.animation.get().info.rid.is_valid())
RenderingServer::get_singleton()->canvas_item_clear(instance_l.animation.get().info.rid);
animations.free_instance(instance_l.animation);
}
if(instance_l.dir_animation.is_valid())
{
dir_animations.free_instance(instance_l.dir_animation);
}
if(instance_l.dyn_animation.is_valid())
{
dyn_animations.free_instance(instance_l.dyn_animation);
}
if(instance_l.alt_info.is_valid())
{
if(instance_l.alt_info.get().rid.is_valid())
RenderingServer::get_singleton()->canvas_item_clear(instance_l.alt_info.get().rid);
alt_infos.free_instance(instance_l.alt_info);
}
for(smart_list_handle<EntityInstance> subs_l : instance_l.sub_instances)
{
if(subs_l.is_valid())
{
free_instance(subs_l.handle(), true);
}
}
// if we are a sub instance we release ourself from our main
if(instance_l.main_instance.is_valid())
{
if(!skip_main_free_p)
{
std::list<smart_list_handle<EntityInstance> > & sub_instances_l = instance_l.main_instance.get().sub_instances;
// remove itself
for(auto it_l = sub_instances_l.begin() ; it_l != sub_instances_l.end() ; ++it_l )
{
if(it_l->handle() == idx_p)
{
sub_instances_l.erase(it_l);
break;
}
}
}
}
// else we can clear the direction handler
else
{
pos_indexes.free_instance(instance_l.pos_idx);
if(instance_l.dir_handler.is_valid())
{
dir_handlers.free_instance(instance_l.dir_handler);
}
}
// free payload
_payload_handler->free_payload(idx_p);
_instances.free_instance(idx_p);
delete lock_l;
}
void EntityDrawer::update_sprite_frames(int idx_p, Vector2 const &offset_p, Ref<SpriteFrames> const & animation_p)
{
std::lock_guard<std::mutex> lock_l(_internal_mutex);
EntityInstance &entity_l = _instances.get(idx_p);
AnimationInstance &animation_l = entity_l.animation.get();
animation_l.offset = offset_p;
animation_l.animation = animation_p;
}
void EntityDrawer::set_direction(int idx_p, Vector2 const &direction_p, bool just_looking_p)
{
std::lock_guard<std::mutex> lock_l(_internal_mutex);
EntityInstance &instance_l = _instances.get(idx_p);
if(!instance_l.dir_handler.is_valid())
{
return;
}
DirectionHandler &handler_l = instance_l.dir_handler.get();
if(just_looking_p)
{
handler_l.direction = Vector2();
}
else
{
handler_l.direction = direction_p;
}
int new_type = get_direction(direction_p, handler_l.has_up_down);
if(new_type != DirectionHandler::NONE)
{
handler_l.type = new_type;
handler_l.count = 0;
}
}
void EntityDrawer::add_direction_handler(int idx_p, bool has_up_down_p)
{
std::lock_guard<std::mutex> lock_l(_internal_mutex);
EntityInstance &instance_l = _instances.get(idx_p);
if(instance_l.dir_handler.is_valid()
|| !instance_l.animation.is_valid())
{
return;
}
DirectionHandler handler_l;
handler_l.has_up_down = has_up_down_p;
handler_l.pos_idx = instance_l.pos_idx.get().idx;
instance_l.dir_handler = dir_handlers.new_instance(handler_l);
DirectionalAnimation anim_l;
init_animation(anim_l, instance_l.animation.get().current_animation);
instance_l.dir_animation = dir_animations.new_instance(anim_l);
}
void EntityDrawer::remove_direction_handler(int idx_p)
{
std::lock_guard<std::mutex> lock_l(_internal_mutex);
EntityInstance &instance_l = _instances.get(idx_p);
if(instance_l.dir_handler.is_valid())
{
dir_handlers.free_instance(instance_l.dir_handler);
}
if(instance_l.dir_animation.is_valid())
{
dir_animations.free_instance(instance_l.dir_animation);
}
}
void EntityDrawer::add_dynamic_animation(int idx_p, StringName const &idle_animation_p, StringName const &moving_animation_p)
{
std::lock_guard<std::mutex> lock_l(_internal_mutex);
EntityInstance &instance_l = _instances.get(idx_p);
if(instance_l.dyn_animation.is_valid())
{
return;
}
DynamicAnimation dyn_l;
init_animation(dyn_l.idle, idle_animation_p);
init_animation(dyn_l.moving, moving_animation_p);
instance_l.dyn_animation = dyn_animations.new_instance(dyn_l);
}
void EntityDrawer::add_pickable(int idx_p)
{
std::lock_guard<std::mutex> lock_l(_internal_mutex);
EntityInstance &instance_l = _instances.get(idx_p);
if(instance_l.alt_info.is_valid())
{
return;
}
instance_l.alt_info = alt_infos.recycle_instance();
RenderingInfo &info_l = instance_l.alt_info.get();
// if fresh new animation we set it up
if(instance_l.alt_info.revision() == 0 && _texture_catcher)
{
// set up resources
info_l.rid = RenderingServer::get_singleton()->canvas_item_create();
info_l.material = Ref<ShaderMaterial>(memnew(ShaderMaterial));
info_l.material->set_shader(_alt_shader);
RenderingServer::get_singleton()->canvas_item_set_parent(info_l.rid, _texture_catcher->get_alt_viewport()->get_canvas_item());
RenderingServer::get_singleton()->canvas_item_set_default_texture_filter(info_l.rid, RenderingServer::CANVAS_ITEM_TEXTURE_FILTER_NEAREST);
RenderingServer::get_singleton()->canvas_item_set_material(info_l.rid, info_l.material->get_rid());
}
info_l.material->set_shader_parameter("idx_color", color_from_idx(idx_p));
}
void EntityDrawer::remove_pickable(int idx_p)
{
std::lock_guard<std::mutex> lock_l(_internal_mutex);
EntityInstance &instance_l = _instances.get(idx_p);
if(instance_l.alt_info.is_valid())
{
if(instance_l.alt_info.get().rid.is_valid())
RenderingServer::get_singleton()->canvas_item_clear(instance_l.alt_info.get().rid);
alt_infos.free_instance(instance_l.alt_info);
}
}
void EntityDrawer::set_animation(int idx_p, StringName const ¤t_animation_p, StringName const &next_animation_p)
{
std::lock_guard<std::mutex> lock_l(_internal_mutex);
EntityInstance &instance_l = _instances.get(idx_p);
if(!instance_l.animation.is_valid())
{
return;
}
/// IMPORTANT this has to be done before next_animation = next_animation_p because
/// current_animation_p is a reference to old next_animation therefore updating it break
/// the value
if(instance_l.dir_animation.is_valid())
{
init_animation(instance_l.dir_animation.get(), current_animation_p);
}
instance_l.animation.get().current_animation = current_animation_p;
instance_l.animation.get().next_animation = next_animation_p;
instance_l.animation.get().frame_idx = 0;
instance_l.animation.get().start = _elapsedAllTime;
instance_l.animation.get().one_shot = false;
}
void EntityDrawer::set_proritary_animation(int idx_p, StringName const ¤t_animation_p, StringName const &next_animation_p)
{
std::lock_guard<std::mutex> lock_l(_internal_mutex);
EntityInstance &instance_l = _instances.get(idx_p);
if(!instance_l.animation.is_valid())
{
return;
}
/// IMPORTANT this has to be done before next_animation = next_animation_p because
/// current_animation_p is a reference to old next_animation therefore updating it break
/// the value
if(instance_l.dir_animation.is_valid())
{
init_animation(instance_l.dir_animation.get(), current_animation_p);
}
instance_l.animation.get().current_animation = current_animation_p;
instance_l.animation.get().next_animation = next_animation_p;
instance_l.animation.get().frame_idx = 0;
instance_l.animation.get().start = _elapsedAllTime;
instance_l.animation.get().one_shot = false;
instance_l.animation.get().has_priority = true;
}
void EntityDrawer::set_animation_one_shot(int idx_p, StringName const ¤t_animation_p, bool priority_p)
{
std::lock_guard<std::mutex> lock_l(_internal_mutex);
EntityInstance &instance_l = _instances.get(idx_p);
if(!instance_l.animation.is_valid())
{
return;
}
instance_l.animation.get().current_animation = current_animation_p;
instance_l.animation.get().frame_idx = 0;
instance_l.animation.get().start = _elapsedAllTime;
instance_l.animation.get().one_shot = true;
instance_l.animation.get().has_priority = priority_p;
if(instance_l.dir_animation.is_valid())
{
init_animation(instance_l.dir_animation.get(), current_animation_p);
}
}
StringName const & EntityDrawer::get_animation(int idx_p) const
{
std::lock_guard<std::mutex> lock_l(_internal_mutex);
EntityInstance const &instance_l = _instances.get(idx_p);
if(!instance_l.animation.is_valid())
{
static StringName none_l("");
return none_l;
}
return instance_l.animation.get().current_animation;
}
void EntityDrawer::set_new_pos(int idx_p, Vector2 const &pos_p)
{
size_t const &pos_idx_l = _instances.get(idx_p).pos_idx.get().idx;
_newPos[pos_idx_l] = pos_p;
}
Vector2 const & EntityDrawer::get_old_pos(int idx_p)
{
size_t const &pos_idx_l = _instances.get(idx_p).pos_idx.get().idx;
return _oldPos[pos_idx_l];
}
void EntityDrawer::update_pos()
{
std::lock_guard<std::mutex> lock_l(_internal_mutex);
_elapsedTime = 0.;
// swap positions
std::swap(_oldPos, _newPos);
}
Ref<ShaderMaterial> EntityDrawer::get_shader_material(int idx_p)
{
EntityInstance const &instance_l = _instances.get(idx_p);
if(!instance_l.animation.is_valid())
{
return Ref<ShaderMaterial>();
}
return instance_l.animation.get().info.material;
}
void EntityDrawer::set_shader_bool_param(int idx_p, String const ¶m_p, bool value_p)
{
EntityInstance &instance_l = _instances.get(idx_p);
if(instance_l.animation.is_valid()
&& instance_l.animation.get().info.material.is_valid())
{
instance_l.animation.get().info.material->set_shader_parameter(param_p, value_p);
}
}
void EntityDrawer::set_shader_bool_params(String const ¶m_p, TypedArray<bool> const &values_p)
{
_instances.for_each([&](EntityInstance & instance_l, size_t idx_p) {
if(instance_l.animation.is_valid()
&& instance_l.animation.get().info.material.is_valid())
{
instance_l.animation.get().info.material->set_shader_parameter(param_p, values_p[idx_p]);
}
});
}
void EntityDrawer::set_shader_bool_params_from_indexes(String const ¶m_p, TypedArray<int> const &indexes_p, bool value_indexes_p)
{
// set for indexes
for(size_t i = 0 ; i < indexes_p.size() ; ++ i)
{
int idx_l = indexes_p[i];
if(_instances.is_valid(idx_l)
&& _instances.get(idx_l).animation.is_valid()
&& _instances.get(idx_l).animation.get().info.material.is_valid())
{
_instances.get(idx_l).animation.get().info.material->set_shader_parameter(param_p, value_indexes_p);
}
}
}
void EntityDrawer::set_all_shader_bool_params_from_indexes(String const ¶m_p, TypedArray<int> const &indexes_p, bool value_indexes_p, bool value_others_p)
{
// set all default values
_instances.for_each([&](EntityInstance & instance_l, size_t ) {
if(instance_l.animation.is_valid()
&& instance_l.animation.get().info.material.is_valid())
{
instance_l.animation.get().info.material->set_shader_parameter(param_p, value_others_p);
}
});
// set for indexes
for(size_t i = 0 ; i < indexes_p.size() ; ++ i)
{
int idx_l = indexes_p[i];
if(_instances.is_valid(idx_l)
&& _instances.get(idx_l).animation.is_valid()
&& _instances.get(idx_l).animation.get().info.material.is_valid())
{
_instances.get(idx_l).animation.get().info.material->set_shader_parameter(param_p, value_indexes_p);
}
}
}
TypedArray<int> EntityDrawer::indexes_from_texture(Rect2 const &rect_p) const
{
TypedArray<bool> all_added_l = index_array_from_texture(rect_p);
TypedArray<int> indexes_l;
for(int idx_l = 0 ; idx_l < all_added_l.size() ; ++ idx_l)
{
if (all_added_l[idx_l])
{
indexes_l.append(idx_l);
}
}
return indexes_l;
}
TypedArray<bool> EntityDrawer::index_array_from_texture(Rect2 const &rect_p) const
{
if(!_texture_catcher)
{
return TypedArray<bool>();
}
// scale from texture viewport scale
double scale_l = _texture_catcher->get_scale_viewport();
Rect2i scale_rect_l = Rect2i(rect_p.get_position() / scale_l, rect_p.get_size() / scale_l);
Ref<Image> image_l = _texture_catcher->get_texture()->get_image();
TypedArray<bool> all_added_l;
all_added_l.resize(_instances.size());
all_added_l.fill(false);
for(int32_t x = scale_rect_l.get_position().x ; x <= scale_rect_l.get_position().x + scale_rect_l.get_size().x ; ++ x)
{
for(int32_t y = scale_rect_l.get_position().y ; y <= scale_rect_l.get_position().y + scale_rect_l.get_size().y ; ++ y)
{
int idx_l = idx_from_color(safe_color(x, y, image_l));
if(idx_l >= 0)
{
all_added_l[idx_l] = true;
}
}
}
return all_added_l;
}
int EntityDrawer::index_from_texture(Vector2 const &pos_p) const
{
return index_from_texture_with_tolerance(pos_p, 0);
}
int EntityDrawer::index_from_texture_with_tolerance(Vector2 const &pos_p, int tolerance_p) const
{
if(!_texture_catcher)
{
return -1;
}
// scale from texture viewport scale
double scale_l = _texture_catcher->get_scale_viewport();
Vector2 scale_pos_l = pos_p / scale_l;
Ref<Image> image_l = _texture_catcher->get_texture()->get_image();
int idx_l = idx_from_color(safe_color(scale_pos_l.x, scale_pos_l.y, image_l));
if(idx_l >= 0)
{
return idx_l;
}
for(size_t range_l = 1; range_l < tolerance_p; ++ range_l)
{
for(size_t x = 0 ; x <= range_l ; ++x)
{
for(size_t y = 0 ; y <= range_l ; ++y)
{
if(x+y == 0 || x+y > range_l) { continue; }
idx_l = idx_from_color(safe_color(scale_pos_l.x+x, scale_pos_l.y+y, image_l));
if(idx_l >= 0) { return idx_l; }
idx_l = idx_from_color(safe_color(scale_pos_l.x-x, scale_pos_l.y+y, image_l));
if(idx_l >= 0) { return idx_l; }
idx_l = idx_from_color(safe_color(scale_pos_l.x-x, scale_pos_l.y-y, image_l));
if(idx_l >= 0) { return idx_l; }
idx_l = idx_from_color(safe_color(scale_pos_l.x+x, scale_pos_l.y-y, image_l));
if(idx_l >= 0) { return idx_l; }
}
}
}
return -1;
}
void EntityDrawer::_notification(int p_notification)
{
switch (p_notification) {
case NOTIFICATION_PROCESS: {
_process(get_process_delta_time());
} break;
case NOTIFICATION_PHYSICS_PROCESS: {
_physics_process(get_physics_process_delta_time());
} break;
case NOTIFICATION_DRAW: {
_draw();
} break;
case NOTIFICATION_READY: {
_ready();
set_process(true);
set_physics_process(true);
} break;
}
}
void EntityDrawer::_ready()
{
// Custom shader to display a color per idx
_alt_shader = Ref<Shader>(memnew(Shader));
_alt_shader->set_code("\n\
shader_type canvas_item;\n\
\n\
uniform vec3 idx_color : source_color = vec3(1.0);\n\
\n\
void fragment() {\n\
COLOR.rgb = idx_color;\n\
COLOR.a = round(COLOR.a);\n\
}\n\
"
);
_texture_catcher = memnew(TextureCatcher);
_texture_catcher->set_scale_viewport(_scale_viewport);
if(!_ref_camera_path.is_empty())
{
_texture_catcher->set_ref_camera("../"+_ref_camera_path);
}
add_child(_texture_catcher);
}
StringName const &get_anim(EntityInstance &instance_p)
{
if(!instance_p.dir_handler.is_valid())
{
return instance_p.animation.get().current_animation;
}
DirectionHandler const &handler_l = instance_p.dir_handler.get();
int type_l = handler_l.type;
if(type_l == DirectionHandler::NONE)
{
type_l = DirectionHandler::LEFT;
}
// forced directionl anim
if(instance_p.dir_animation.is_valid()
&& instance_p.dir_animation.get().base_name != StringName("")
&& instance_p.animation.get().has_priority)
{
return instance_p.dir_animation.get().names[type_l];
}
if(instance_p.dyn_animation.is_valid()
&& instance_p.dir_handler.is_valid())
{
if(handler_l.idle)
{
// non-forced directionl anim
if(instance_p.dir_animation.is_valid()
&& instance_p.dir_animation.get().base_name != StringName(""))
{
return instance_p.dir_animation.get().names[type_l];
}
return instance_p.dyn_animation.get().idle.names[type_l];
}
return instance_p.dyn_animation.get().moving.names[type_l];
}
return instance_p.animation.get().current_animation;
}
void EntityDrawer::_draw()
{
std::lock_guard<std::mutex> lock_l(_mutex);
_instances.for_each([&](EntityInstance &instance_p, size_t idx_p) {
StringName cur_anim_l = get_anim(instance_p);
if(!instance_p.animation.is_valid())
{
return;
}
bool is_over_l = false;
AnimationInstance & animation_l = instance_p.animation.get();
if(animation_l.animation.is_valid() && animation_l.animation->get_frame_count(cur_anim_l) > 0)
{
double frameTime_l = animation_l.animation->get_frame_duration(cur_anim_l, animation_l.frame_idx) / animation_l.animation->get_animation_speed(cur_anim_l) ;
double nextFrameTime_l = animation_l.start + frameTime_l;
if(_elapsedAllTime >= nextFrameTime_l)
{
++animation_l.frame_idx;
animation_l.start = _elapsedAllTime;
}
if(animation_l.frame_idx >= animation_l.animation->get_frame_count(cur_anim_l))
{
if(animation_l.one_shot)
{
free_instance(idx_p);
is_over_l = true;
}
else if(animation_l.next_animation != StringName(""))
{
set_animation(idx_p, animation_l.next_animation, StringName(""));
cur_anim_l = get_anim(instance_p);
}
// if dynamic animation and no chaining we reset
else if(instance_p.dyn_animation.is_valid())
{
set_animation(idx_p, StringName(""), StringName(""));
cur_anim_l = get_anim(instance_p);
}
animation_l.frame_idx = 0;
}
// if still enabled
if(!is_over_l)
{
size_t pos_idx_l = instance_p.pos_idx.get().idx;
Vector2 diff_l = _newPos[pos_idx_l] - _oldPos[pos_idx_l];
Vector2 pos_l = (_oldPos[pos_idx_l] + diff_l * std::min<real_t>(1., real_t(_elapsedTime/_timeStep))) * _scale;
// draw animaton
Ref<Texture2D> texture_l = animation_l.animation->get_frame_texture(cur_anim_l, animation_l.frame_idx);
RenderingServer::get_singleton()->canvas_item_set_transform(animation_l.info.rid, Transform2D(0., pos_l));
RenderingServer::get_singleton()->canvas_item_clear(animation_l.info.rid);
// required when empty texture in sprite frame
if(texture_l.is_valid())
{
// classic rendering
texture_l->draw(animation_l.info.rid, animation_l.offset);
// alternate rendering
if(instance_p.alt_info.is_valid()
&& instance_p.alt_info.get().rid.is_valid())
{
RenderingInfo &alt_info_l = instance_p.alt_info.get();
RenderingServer::get_singleton()->canvas_item_set_transform(alt_info_l.rid, Transform2D(0., pos_l));
RenderingServer::get_singleton()->canvas_item_clear(alt_info_l.rid);
texture_l->draw(alt_info_l.rid, animation_l.offset);
}
}
}
}
});
}
void EntityDrawer::_process(double delta_p)
{
std::lock_guard<std::mutex> lock_l(_mutex);
_elapsedTime += delta_p;
_elapsedAllTime += delta_p;
queue_redraw();
}
void EntityDrawer::_physics_process(double delta_p)
{
std::lock_guard<std::mutex> lock_l(_mutex);
dir_handlers.for_each([&](DirectionHandler &handler_p) {
Vector2 dir_l = handler_p.direction;
if(dir_l.length_squared() < ENTITY_DRAWER_EPSILON)
{
dir_l = _newPos[handler_p.pos_idx] - _oldPos[handler_p.pos_idx];
}
int new_type = get_direction(dir_l, handler_p.has_up_down);
if(new_type != DirectionHandler::NONE)
{
handler_p.count_idle = 0;
handler_p.idle = false;
if(new_type != handler_p.count_type)
{
handler_p.count = 0;
handler_p.count_type = new_type;
}
if(handler_p.count < 100)
{
++handler_p.count;
}
}
else
{
++handler_p.count_idle;
handler_p.count = 0;
handler_p.count_type = DirectionHandler::NONE;
}
if(handler_p.count > 5
&& handler_p.count_type != DirectionHandler::NONE
&& handler_p.count_type != handler_p.type)
{
handler_p.type = handler_p.count_type;
}
if(handler_p.count_idle > 5)
{
handler_p.idle = true;
}
});
}
void EntityDrawer::_bind_methods()
{
ClassDB::bind_method(D_METHOD("add_instance", "position", "offset", "animation", "current_animation", "next_animation", "one_shot", "in_front"), &EntityDrawer::add_instance);
ClassDB::bind_method(D_METHOD("add_sub_instance", "idx_ref", "offset", "animation", "current_animation", "next_animation", "one_shot", "in_front", "use_directions"), &EntityDrawer::add_sub_instance);
ClassDB::bind_method(D_METHOD("free_instance", "idx"), &EntityDrawer::free_instance);
ClassDB::bind_method(D_METHOD("update_sprite_frames", "idx", "offset", "animation"), &EntityDrawer::update_sprite_frames);
ClassDB::bind_method(D_METHOD("update_pos"), &EntityDrawer::update_pos);
ClassDB::bind_method(D_METHOD("set_animation", "instance", "current_animation", "next_animation"), &EntityDrawer::set_animation);
ClassDB::bind_method(D_METHOD("set_proritary_animation", "instance", "current_animation", "next_animation"), &EntityDrawer::set_proritary_animation);
ClassDB::bind_method(D_METHOD("set_animation_one_shot", "instance", "current_animation", "priority"), &EntityDrawer::set_animation_one_shot);
ClassDB::bind_method(D_METHOD("set_direction", "instance", "direction", "just_looking"), &EntityDrawer::set_direction);
ClassDB::bind_method(D_METHOD("add_direction_handler", "instance", "has_up_down"), &EntityDrawer::add_direction_handler);
ClassDB::bind_method(D_METHOD("remove_direction_handler", "instance"), &EntityDrawer::remove_direction_handler);
ClassDB::bind_method(D_METHOD("add_dynamic_animation", "instance", "idle_animation", "moving_animation"), &EntityDrawer::add_dynamic_animation);
ClassDB::bind_method(D_METHOD("add_pickable", "instance"), &EntityDrawer::add_pickable);
ClassDB::bind_method(D_METHOD("remove_pickable", "instance"), &EntityDrawer::remove_pickable);
ClassDB::bind_method(D_METHOD("set_new_pos", "instance", "pos"), &EntityDrawer::set_new_pos);
ClassDB::bind_method(D_METHOD("get_old_pos", "instance"), &EntityDrawer::get_old_pos);
ClassDB::bind_method(D_METHOD("get_shader_material", "instance"), &EntityDrawer::get_shader_material);
ClassDB::bind_method(D_METHOD("set_shader_bool_param", "idx", "param", "value"), &EntityDrawer::set_shader_bool_param);
ClassDB::bind_method(D_METHOD("set_shader_bool_params", "param", "values"), &EntityDrawer::set_shader_bool_params);
ClassDB::bind_method(D_METHOD("set_shader_bool_params_from_indexes", "param", "indexes", "value_index"), &EntityDrawer::set_shader_bool_params_from_indexes);
ClassDB::bind_method(D_METHOD("set_all_shader_bool_params_from_indexes", "param", "indexes", "value_index", "value_other"), &EntityDrawer::set_all_shader_bool_params_from_indexes);
ClassDB::bind_method(D_METHOD("set_shader", "material"), &EntityDrawer::set_shader);
ClassDB::bind_method(D_METHOD("set_time_step", "time_step"), &EntityDrawer::set_time_step);
ClassDB::bind_method(D_METHOD("indexes_from_texture", "rect"), &EntityDrawer::indexes_from_texture);
ClassDB::bind_method(D_METHOD("index_array_from_texture", "rect"), &EntityDrawer::index_array_from_texture);
ClassDB::bind_method(D_METHOD("index_from_texture", "position"), &EntityDrawer::index_from_texture);
ClassDB::bind_method(D_METHOD("index_from_texture_with_tolerance", "position", "tolerance"), &EntityDrawer::index_from_texture_with_tolerance);
// properties
ClassDB::bind_method(D_METHOD("get_scale_viewport"), &EntityDrawer::get_scale_viewport);
ClassDB::bind_method(D_METHOD("set_scale_viewport", "scale_viewport"), &EntityDrawer::set_scale_viewport);
ClassDB::add_property("EntityDrawer", PropertyInfo(Variant::FLOAT, "scale_viewport"), "set_scale_viewport", "get_scale_viewport");
ClassDB::bind_method(D_METHOD("get_ref_camera"), &EntityDrawer::get_ref_camera);
ClassDB::bind_method(D_METHOD("set_ref_camera", "ref_camera"), &EntityDrawer::set_ref_camera);
ClassDB::add_property("EntityDrawer", PropertyInfo(Variant::NODE_PATH, "ref_camera", PROPERTY_HINT_NODE_PATH_VALID_TYPES, "Camera2D"), "set_ref_camera", "get_ref_camera");
ClassDB::bind_method(D_METHOD("set_debug", "debug"), &EntityDrawer::set_debug);
ClassDB::bind_method(D_METHOD("is_debug"), &EntityDrawer::is_debug);
ClassDB::add_property("EntityDrawer", PropertyInfo(Variant::BOOL, "debug"), "set_debug", "is_debug");
ADD_GROUP("EntityDrawer", "EntityDrawer_");
}
void EntityDrawer::setup_payload(AbstractEntityPayload * payload_hanlder_p)
{
if(_instances.size() > 0)
{
return;
}
delete _payload_handler;
_payload_handler = payload_hanlder_p;
}
void EntityDrawer::set_debug(bool debug_p) { if(_texture_catcher) _texture_catcher->set_debug(debug_p); }
bool EntityDrawer::is_debug() const { if(_texture_catcher) return _texture_catcher->is_debug(); else return false; }
} // namespace godot