| 115 | entity, pos, rot) { } |
| 116 | |
| 117 | bool Unit::init() { |
| 118 | if (!Body::init()) return false; |
| 119 | auto defaultFaceRight = _unitDef->get(Def::DefaultFaceRight, true); |
| 120 | auto detectDistance = _unitDef->get(Def::DetectDistance, 0.0f); |
| 121 | auto attackRange = _unitDef->get(Def::AttackRange, Size::zero); |
| 122 | auto tag = _unitDef->get(Def::Tag, Slice::Empty); |
| 123 | auto playableStr = _unitDef->get(Def::Playable, Slice::Empty); |
| 124 | auto scale = _unitDef->get(Def::Scale, 1.0f); |
| 125 | auto actions = _unitDef->get(Def::Actions, s_cast<Array*>(nullptr)); |
| 126 | _flags.set(Unit::DefaultFaceRight, defaultFaceRight); |
| 127 | Unit::setDetectDistance(detectDistance); |
| 128 | Unit::setAttackRange(attackRange); |
| 129 | Unit::setTag(tag); |
| 130 | _groundSensor = Body::getSensorByTag(Unit::GroundSensorTag); |
| 131 | Playable* playable = Playable::create(playableStr); |
| 132 | if (!playable) { |
| 133 | Error("failed to load playable \"{}\" for an new unit.", playableStr); |
| 134 | Unit::cleanup(); |
| 135 | return false; |
| 136 | } |
| 137 | playable->setScaleX(scale); |
| 138 | playable->setScaleY(scale); |
| 139 | Unit::setPlayable(playable); |
| 140 | Unit::setFaceRight(true); |
| 141 | Body::setOwner(this); |
| 142 | ARRAY_START_VAL(std::string, action, actions) { |
| 143 | Unit::attachAction(action); |
| 144 | } |
| 145 | ARRAY_END |
| 146 | _decisionTreeName = _unitDef->get(Def::DecisionTree, Slice::Empty); |
| 147 | _entity->set("unit"_slice, s_cast<Object*>(this)); |
| 148 | this->scheduleUpdate(); |
| 149 | return true; |
| 150 | } |
| 151 | |
| 152 | void Unit::onEnter() { |
| 153 | Body::onEnter(); |